Hello everyone and welcome to this new lecture about this validation.
So far we've seen how important it is to call the is valid method during the data.
This sterilization step before access in their validated data dictionary or saving a new instance if
any errors occur.
The error property will contain a dictionary with a corresponding error messages for example.
Here we see that I am instantiating a new sterilizer class based on their contact serialize their class
and we can see day to day contact the serialize their class I'm feeling a dictionary data which contains
a body some text.
And any email address that as you can see unfortunately is badly formatted.
Therefore calling there is valid method on this utilize every instance that returns false and we can
read the corresponding error messages.
My access in the editor's property of the serialize it learning about validation.
In this lesson we're going to see how to specify customer validation checks for our models and realize
it's using both field level validation and object level validation.
Let's get started before we start writing the code for this lesson.
I want to talk about validators briefly from the perspective of the official documentation.
You can easily access this specific page of the documentation from EPA guide validators.
So we've seen already why write in our article serialize that class so that we can in fact specify different
parameters for our serialized their fields such as for example.
Read only parameter Django This framework provides several Bill teen validators that we can basically
use the same way we would specify the normal parameters such as the read only parameter.
You can see here from the menu on the left that we've got for example a unique validator unique together
by later for date for the month for year.
And it's actually pretty easy to specify that we want to use these kind of validators to see for example
we a slug field.
We can specify to validators parameter where we can pass to eat a list of the validators see that we
want to use.
And another very practical way to specify that you want to use the built in validators is to define
the class matter within your sterilizer class and within a matter you can then define a list of validators
containing you see all the built in validators.
Did you intend to use however clearly for as useful as they can be.
They cannot cover all the possible scenarios.
So let's jump to vs west to your code.
Let's see how to build our own validators.
Okay.
If we are invisible is that your code and as you can see I opened this serialize that dot by 5.
As a matter of fact we are going to define both feel the level of validation and object level validation
we feel I don't see realized that a class using specific methods that we're going to build first of
all let's define what object level validation and field level validation mean object level validation
as the name suggests means that we want to make a validation check using multiple fields.
Basically we're working on the object level.
An example of an object level validation check could be for example checking if title and description
are the same.
Basically you're using the same string and in that case according to our web apps policy we might want
to raise a validation error filter level validation instead as the name suggests is that kind of check
that you perform on a single field within our object.
For example we might want to check that specific words have not been used let's say in the body field
and in case they've been used we might want to raise a validation error.
We're going to start by setting up an object level validation check to be sure that title and description
are in fact different from one another.
And so in order to do so we can define the validate method within a certain asset class and validate
method accepts two parameters.
First of all clearly self and then data so we can also write a duck string check that description and
title are different.
So clearly data represents the data of our serialized.
Therefore we can perform the check like so if data and we check title so we win data.
We want to check a value that is associated with title and we want to check this against data description
so we are comparing both values and in case the if statement returns TRUE WE RAISE A validation error.
So please realize that's not validation error and 2 validation error.
We passed the error message that we want to show.
In this case for example title and description.
Must be different from one another than if everything's all right.
Instead we just return data.
So let's start our development server and let's see how all of this works from the browser API.
So the server is up and running.
Let's go back to Chrome.
And so here I'm now going to try to create in your instance right here below
that we do not need I.D..
So the title you can set checking validation or rather checking objective level validation and I'm going
to set a description as checking objective level evaluation just as well.
Let's see what happens.
So let's try to post the data and entities are a validation error.
We can see first of all this data scored a CTP 400 bed request then we can read the message title and
description must be different from one another.
Perfect.
So let's not try to create the same article this time changing for example the description.
Like for example just a different description like so.
So let's post our data and entities our beautiful new instance.
Perfect let's not make an example of field level validation for that article sterilizer.
Let's say that we want to be sure that each new article has a title that is at least 60 characters long.
We can easily do that like so we can define validate underscore the name of a field that we actually
want to check.
So title and his method is going to accept two parameters.
Clearly self and value which represents the value of a field we want to check.
So if land value is less than 60 raise see that ISIS dot validation error.
And yet we pass the message.
So the title is to be at least 60 characters long into and remember to return the value.
So let's now check these new validated title methods.
I'm going to restart the development server.
Everything works as expected.
Let us go back to Chrome and here.
Let's just create a new record.
Let's try and create a new article so going to delete an idea as well.
And for the title we can just write something like less than 60 characters.
Let's see what happens.
Post we get a bed request status code and an error message.
Title the title is to be at least 60 characters long.
Perfect.
So let's now write a proper article.
So I'm going to maybe 2 3 4 times just to be sure so post entities is our new article success.
OK so that was it for this lecture about serialize this validation in the next lecture we're going to
start talking about another very important class the model serialized class.