So, slowly but steadily, this application is taking shape, but there are still some
tweaks I want to make to this code we already have here.
For example, when creating a user, I want to validate the received data.
I don't want to store it blindly trusting the user to have submitted valid data.
I want to make sure that we got a valid email, that it's not an email address we already
have in our database, and that we have a valid password, which, for example, has a certain
minimum length.
Again, we could use the composer for that, we could also use the chat for that, but since
I'll mostly work in one file, I'll use the inline chat functionality.
Now I could add the validation here to this createUser function, but I'll actually go
for the user's controller file.
Here where I'm extracting that information from the request body.
Because in here I already got some very basic validation code.
But here I just check whether I got some email and some password without performing any further
checks.
For example, currently I would accept an email as valid if it were just a bunch of
blank spaces, which is of course not valid, and the same for the password.
So I'll highlight this entire function and open this inline chat here with the appropriate
shortcut to provide some editing instructions to Cursor.
And I'll tell Cursor to apply better validation for both the email and password.
And I'll then provide some details regarding how I want to validate these two data points
or data fields.
The email must be a valid email address and it must not be taken yet, i.e. not exist in
the database yet.
The password must be at least six characters long.
Both fields must not be empty strings or just a bunch of blanks.
So let's submit this and let's see what we'll get.
So it of course starts editing the code and for example here for the email it checks whether
it's empty after trimming it.
Trim is a built-in method you can call on strings in JavaScript to remove excess whitespace
at the beginning or end.
And it does the same for the password.
And then it also performs some extra checks here using a regular expression.
By the way, AI models are really great for generating regular expressions.
That's one of the best use cases in my experience.
So it does that here and then also uses this regular expression to check whether the email
is an email, sending back an error code if it's not.
And then it checks the password length.
It also checks whether we have a user with that email address already and if that's
the case it also sends back an error status code.
So I can accept this and with that this controller function, this signup function has been improved
and we got better validation in place here.
Now of course there still would be different ways of performing this validation.
You could still tweak that code and you can do this with or without AI.
If you know what you want to do you might be quicker without AI.
But here I'm pretty happy with what I got here.
So that's looking good to me and I'm ready to test this soon enough.
I also want to tweak the validation here though.
I don't need detailed validation when logging users in because I'll validate their credentials
anyways.
At least I'll soon do that.
But I want to make sure that email and password are not strings full of blanks.
So I'll tweak this part here and say make sure that email and password are not just
strings full of blanks and that should make sure that this trim method gets added.
Though arguably we could have also done this without AI and we might have been faster.
So yeah it's actually a good example for slowly falling into the trap of using AI for
everything.
You really want to be careful regarding that.
So that's that.
Now I'm not fully finished yet.
For example here when checking the password right now I'm assuming that the password is
stored as plain text and I am indeed storing it as plain text here.
And that's not the way it should work.
But it's a first step and it is something we can test before we then refine this application.