How to add signup validation on backend
I have added new validation fields and rewrite the frontend version of signup proccess and now I am trying to add validation on backend in
defineUserSignupFields
but it does not work, how can I achieve validation on backend? Maybe there is a way overwrite just ensureValidArgs
function?
5 Replies
Hi @abehod. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
----------------------
In Wasp, you can add custom validation to the signup process by defining extra fields in the
userSignupFields
object. This object is where the keys represent the field name, and the values are functions that receive the data sent from the client and return the value of the field. If the value that the function received is invalid, the function should throw an error.
Here is an example of how you can use a validation library like zod
to validate the fields:
In this example, the address
field is validated to be a string and at least 10 characters long. If the validation fails, an error is thrown.
Regarding the ensureValidArgs
function, it seems like it's a custom function that you've written. The ensureValidEmail
, ensurePasswordIsPresent
, and ensureValidPassword
functions are part of Wasp's auth backend and they check if the email is valid, if the password is present, and if the password is valid, respectively. If any of these checks fail, they throw an error.
If you want to add more checks to the ensureValidArgs
function, you can do so by adding more function calls inside it. However, keep in mind that these functions need to throw an error if the validation fails, similar to the ensureValidEmail
, ensurePasswordIsPresent
, and ensureValidPassword
functions.
Sources:
- Customizing the Signup Process (https://wasp-lang.dev/docs/auth/overview#1-defining-extra-fields)
- Signup Fields Customization (https://wasp-lang.dev/docs/auth/overview#signup-fields-customization)
- Creating a Custom Sign-up Action (https://wasp-lang.dev/docs/auth/email#creating-a-custom-sign-up-action)
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------
Sorry @abehod, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist:
Perhaps try again in 15mins?@miho pls take a look
Customisation of the existing validation rules is not possible right now. https://github.com/wasp-lang/wasp/issues/1571
We had a different system before, but during the auth rewrite we lost it.
GitHub
Enable auth validation customisation · Issue #1571 · wasp-lang/wasp
We should add an API in the Wasp file for providing custom auth field validations. It should be front and centar and easy as e.g. Signup Field Customisation
But you are not trying to customize, but rather validate extra fields? Then you can throw an error when some field doesn't meet the criteria and the error message will be visible on the client.
Here are the docs on the topic: https://wasp-lang.dev/docs/auth/overview#1-defining-extra-fields 🙂
Overview | Wasp
Auth is an essential piece of any serious application. That's why Wasp provides authentication and authorization support out of the box.
Thank you for your answer!