Welcome to your challenge video.
As promised.
In this one it's going to be up to you to use what you've learned about Mongoose so far to improve both
the user and task model.
There are actually two challenges here.
The first one is going to require you to add a new field to user.
And the second is going to require you to customize the task model.
Let's go ahead and start with the user challenge since the user code is already on commented.
Any task code is currently commented out so up above.
I do indeed have some challenge comments for this one.
The big picture goal is to add a password field to user.
Now for the moment you're just going to store that password in whatever form it comes in.
So if someone types out my pass one two three as their password that's what you're going to save.
To the database.
Now this is known as storing a plain text password and it's very bad to do in terms of security.
We are going to explore how we can correctly store our passwords and hash them later on in the class.
But for now we're not going to worry about that.
For now it's OK to store those plain text passwords in the database.
So step number one we want to set up this new password field as a required string.
We also want to ensure that the length is greater than six so seven or more characters the more characters
the more secure the password.
Next up we're going to trim the password removing any spaces.
And finally we're gonna use a custom validator to ensure that the password doesn't contain the string
password.
Now to get this done you can use the includes method which is available on all strings in JavaScript.
If you haven't used that before you can google javascript string includes to pull up the documentation
and learn how it works.
So for step number four you want to use a custom validator now a four step number two.
You could use a custom one.
You could also use the built in one.
Either way gets the job done from there.
You want to test your work create a user with valid data.
Create another one with invalid password data and make sure you're getting the expected results.
All right take some time to knock that one out.
Test your work and when you're done come back and click play.
How did that go.
Let's go ahead and kick things off down below.
By setting up the password to be a required string.
So I'll put that right here after e-mail.
Though the order isn't particularly important and I'm going to go ahead and set it up as an object.
Putting a comma afterwards so the age property gets set up correctly as well.
From here we'll start off with the type definition.
Now once again the order we put things even inside of the object in here is not important.
I just typically follow a pattern where type comes first.
So type is gonna be string and I mentioned that I wanted to make this a required field so it required
is going to get set equal to true.
Perfect.
Now from there we can move on to the other steps.
I want to make sure the length is greater than six.
And I also want to ensure that I'm trimming that password now for the length you could have either used
a custom validator or you could have used the built in mean length validator which we explored in the
documentation.
And I can set this to 7 to ensure that the minimum length acceptable is seven.
From there we'll go ahead and set up trim by setting trim equal to true.
Now that brings us to the last change we need to make for a password which is to ensure that no password
doesn't contain the string password.
We can do that with a custom validator like we did right here for email validation.
So I'll set that up by first configuring the function validate setting it up as a method using that
yes sixth method definition syntax.
We know we get the value right here and then we can throw an error if something has gone wrong if the
password is not valid.
Now in our case what we're gonna do is once again set up an if statement that triggers when the password
is invalid and we're going to throw a new error with our error message and we can say something like
password cannot contain inside of quotes.
Password.
So giving them very clear instructions as to why things have failed.
Now for that if statement what do we want to put inside of here.
I want the code below to run when the value includes using the includes method the following the string
password.
Now if you looked at the documentation you would have found out that includes returns true or false
it returns true if this is included and false otherwise.
Now this is completely acceptable for the purposes of the challenge.
One thing you'll note though is that this is not going to work.
If they type in password with a capital P we can always temporarily convert the value to lower case
to do a case insensitive comparison right here value dot to lower case.
So we're going to take the lower case version.
We're going to compare that with password and that's going to allow us to have a more reliable solution
once again using this was not part of the challenge.
So from here we're actually all done in terms of configuring the field and we can go ahead and test
things out down below with our user setup.
So we're creating the new user and saving them.
Let's bring password into the mix.
I'm going to get things started by adding that on password and let's go ahead and start with a password
that is too short.
So right here what I'm gonna do is have a bunch of spaces followed by a couple of letters.
Let's use R E 3 2 followed by a bunch more spaces.
So after this is getting trimmed the length is indeed going to be too short.
Right now the length would be fine but those spaces are gonna go away because of that trim setting from
the terminal down below we can run the file node source D.B. in there.
We have Mongoose dot J S I'm going to run it and we're going to get one of those really long error messages
just up above we have the actual error message.
Now this is something we'll learn how to customize a bit later.
For now it is path password and then it shows the value is shorter than the minimum allowed a length
which is seven.
So everything is working even though the error message isn't the most useful piece of information.
Now that we have this test in place let's go ahead and test some of the other stuff we set up so we
know trim is working.
We know man length is working.
Let's go ahead and make sure that our validation is working by using password in the password right
here.
I'm gonna use capital P password 1 2 3 and make sure that still fails down below I can use control C
to shut things down up and enter to start them up once again.
And what do I get.
I get a validation error which is expected scrolling through that error.
I'll be brought to the message.
Password can not contain password.
Perfect from here.
Let's go ahead and try something completely valid.
I'll use something like phone 0 9 8 exclamation mark that meets all the criteria we've set up from the
terminal down below.
I'll shut things down I'll restart them once again and this time around I can see that things were indeed
saved to the database correctly.
Now once again I am storing the plain text password exactly as the user provided it.
That is just temporary.
We'll talk about password hashing shortly.
Now that we have this in place the first challenge is all done from the terminal.
I'm going to clear the terminal output and now we're going to dive in to the second challenge for the
video.
The second challenge is going to require you to customize that task model right here.
What I'm going to do is comment out the new user as well as the user save call.
And down below we're gonna go ahead and uncommon the calls to create the new task and to save it as
we'll use those to test our work for this challenge.
Right here I have some challenge comments outlining what I'd like you to do.
Your goal is to add some validation and sanitization to the task.
So what do we want to do.
Well we want to trim the description and make it required.
So we want to make sure there's not a bunch of spaces inside of there and we also want to make sure
it's always provided.
That makes no sense to create a task.
If there is no description.
Next up what do we want to do.
We want to make completed optional and if it's not provided we'll set a default value for it to false.
So you don't have to tell us the completed value in creating a task.
And if you don't we'll assume it's not done.
That's why you're creating it.
Next up you want to test your work and make sure that everything up above is working as expected.
So take some time to knock this one out test your work and when you're done come back and click play
how would you do.
Let's go ahead and kick things off with step number one.
We're going to customize that description right here.
I'll be setting required equal to true making sure the description is always provided.
And I'm also going to set trim equal to true to make sure we're trimming those leftover spaces down
below for completed what we want to do is leave it as optional.
You do not need to provide it but if you don't provide it we do want to store a boolean value in the
database.
Now out of our options true and false I think false makes the most sense.
So right here the default value will indeed be false.
Now let's go ahead and make sure all of this is working.
What I'm going to do is remove the challenge comments and customize things down below.
I forgot if I remove the challenge comments from up above and I didn't.
So I'll take a quick moment to remove those as well.
Now let's verify that the changes to task are actually taking effect to start what I'm going to do is
provide nothing.
I'm going to leave off description and I'm going to leave off completed to see what happens.
I'll save the file I'll run things down below from the terminal by using the up arrow key twice to cycle
past clear to our node command and then I'll hit enter to run the script and right here I am indeed
seeing that the path description is required which is good.
So let's add it right here.
Description I'll set it equal to a string.
I'll toss some spaces up front and then I'll have my description be something like eat lunch which is
currently something I'm putting off to finish this video.
So right here.
This should be valid because we do provide the description and we'll see it get trimmed and there's
no need to provide completed.
And we'll see the default value of false hopefully down below we can shut things down for the final
time and start them up once again.
And what do I get I get my task showing up trim and did work because I'm no longer seeing those extra
spaces and the completed value was automatically set to false.
So with Mongoose we can customize our models to fit our needs making it really easy to add validation
and sanitization into the mix and those are two essential topics when it comes to accepting input from
the user.
Now that we have the basics in place we're going to continue on in the next video.
So stay tuned and I'll see you then.