Validators come to action when you are working with the user input. Silvie has a built-in
Validator helper class to
handle the validations in your application. The main idea is to validate an object containing various kinds of data. The
technique used in this class was inspired from the Validation from Laravel Framework.
The validator class can be imported from
silvie/validator. You need to create an instance of this object and specify
your data and validation rules in the constructor.
data should be an object containing the data that needs to be validated.
rules parameter should be an object containing the path to each piece of data with its corresponding validation
rule string. This will be explained in detail in Rules Object section.
messages parameter should be an object of error messages that need to be shown when a validation rule fails. The
keys of this object are data paths, and their values are Validation Message strings. This parameter
should be used in order to define custom messages, It will try to use default error messages if you don't specify this
If you set the
generateNestedErrors parameter to
true, It will create an object with the same structure of the
original data, and puts the error messages on their exact place in the object structure. Otherwise, it will return a
simple object with data paths as keys and their errors as their values.
Saying we have the following object as the user input, and we want to validate it.
Then it will parse the rules and validation messages and tries to validate all rules over their corresponding data. Then
it will generate an error object which is accessible with
.errors on the validator instance. All of these steps will
happen in the constructor.
The rules object is the object that defines what needs to be validated, and how it is going to be validated. You should define the path of the data as keys of the rules object. The values of those keys are rule strings as described below:
A rule string is a string with rule names separated with a pipe line character
|. If a rule can accept parameters,
they can be specified by separating the parameters from the rule name with a colon character
:. Multiple parameters
can also be separated with a comma character
You can find a full list of available validation rules in Validation Rules section.
Validation messages will be emitted into the errors object of the validator instance whenever a rule fails in validation. There is a default validation message for all validation rules. If you don't want to get the default validation messages, you can pass a third parameter to the validator constructor to use those validation messages for the failed rules.
The keys of this object should be path strings with a little difference at the end. You need to specify
the rule name separated by a colon character
: at the end the path.
Validation messages should tell the issues about the data. It might be better to include some extra info into the error message. There are a few placeholders defined which can be replaced in a message.
- :path This will be the exact path of the matching data
- :field This will be the path of the matching data
- :name This will be last part of the path being the exact field name
- :params This will be the array of params joined by a comma, and a white space
- :[index] This will point to an index of parameters
Data paths are
strings that are defined as object keys in the validator
messages objects. A path is a
string separated with dots. Each part can be one of the following:
- key Key to reach members of an object like data
- index Key to reach members of an array like data
- wildcard Match everything in that path
All routes will be searched for from the root level of the main data object. Take a look at the following examples:
The first one reaches the
parents member and tries to find a
father property on it, then it will look for the
The second one will reach the
parents property, and iterates over all its members, and looks for the
on each one of those members.
The third one will just look for a member called
friends in the root object.
The fourth one will reach the
friends property in the root object, then it will check for the
age on the first entry
In all these three steps, searching will be cancelled if something is missing along the path.
After you've created a validator instance, the data will be validated against the rules object. If anything fails during
the validation, an error message will be added to the
errors parameter of the validator instance. Also, another
hasErrors which will be
true indicating if anything failed in validation.
errors parameter is an object with the keys that matches the data names, and their values will be a
in case a data had more than one issue.