2.7: Validating User Submitted Data

Right now we have a problem - we have no validation for what the user submits. A user could create a new account with a blank username and password:

BlankRegister
BlankDatabase

Add validation attributes to the UserForRegister class:

Check validation in the controller (whether or not the ModelState is valid):

Let's test to see what we get if we try to submit blank user data now:

BlankRegister2

Cool! We're getting a 400 Bad Request along with the ModelState - providing us with a JSON object of the errors.

However, remember our check to see if a user already exists? We weren't adding that to the ModelState - we were just passing along a BadRequest with that one error. Let's improve that by rearranging our code by adding the 'user already exists' error to the ModelState.

We'll also add a null check to make sure the user isn't sending along an empty username.

Let's check Postman again by sending another request with an already created user:

UserExistsModelState

Last updated