validation package for express framework inspired from Laravel. the main reason for it, is to simplify the way validation rules are written.
- Writing The Validation Logic
- Working With Error Messages
- Available Validation Rules
- Validating Arrays
validation logic can be written anywhere as long as req
and res
objects are provided with the fields
object.
const {validate} = require('validte_l');
async (req, res, next) => {
let response = await validate(req, res, {
name: ['required', 'string', 'max:255'],
email: ['required', 'email'],
birthdate: ['required', 'date']
});
}
Warning
req
should contain a "body" which should contain the request body.
You may customize the error messages used for specified attribute and rule
let fields = {
name: ['required', 'string', 'max:255'],
email: ['required', 'email'],
birthdate: ['required', 'date']
};
let customMessages = {
'name.required': ':item need to be existed in the request to register the person.'
}
let response = await validate(req, res, fields, customMessages);
now the message for the name will be changed if the name attribute in not found in the request body.
:item will be replaced with field name, so the message will be "name need to be existed in the request to register the person".
After (Date) Alpha Alpha Numeric Array Before (Date) Boolean Confirmed Date Email Ends With In Max Min Missing Missing If Missing Unless Missing With Missing With All Not In Nullable Number Regular Expression Required Required If Required Unless Required With Required With All Required Without Required Without All Starts With URL
The field under validation must be a value after a given date. instance:
'start_date': ['required', date', after:2022/01/01']
The field under validation must be entirely Unicode alphabetic characters contained in \p{L}
and \p{M}
.
'username': ['alpha']
The field under validation must be entirely Unicode alpha-numeric characters contained in \p{L}
, \p{M}
, and \p{N}
.
'username': ['alpha_num']
The field under validation must be a VALID array
.
'user': ['array']
The field under validation must be a value preceding the given date.
'birthdate': ['date', 'before:2000/01/01']
The field under validation must be able to be cast as a boolean. Accepted input are true
, false
, 1
, 0
, "1"
, and "0"
.
'accepted': ['boolean']
The field under validation must have a matching field of {field}_confirmation
. For example, if the field under validation is password
, a matching password_confirmation
field must be present in the input.
'password': ['confirmed']
The field under validation must be a valid Date
'birthdate': ['date']
The field under validation must be formatted as an email address.
'email': ['email']
The field under validation must end with one of the given values.
'email': ['ends_with:.com,.net']
The field under validation must be included in the given list of values.
'user_type': ['in:admin,supervisor']
The field under validation must be less than or equal to a maximum value. Strings, numerics and array.
'username': ['required', 'string' 'max:20'] // username should not exeed 20 chars as length
'age': ['required', 'numeric', 'max:20'] // age should not be grater than 20
The field under validation must have a minimum value. Strings, numerics and arrays.
'username': ['required', 'string' 'min:20'] // username should not be lss than 20 chars as length
'age': ['required', 'numeric', 'min:20'] // age should not be less than 20
The field under validation must not be present in the input data.
'age': ['missing']
The field under validation must not be present if the anotherfield field is equal to any value.
'age': ['missing_if:birthdate']
The field under validation must not be present unless the anotherfield field is equal to any value.
'age': ['missing_unless:name']
The field under validation must not be present only if any of the other specified fields are present.
'age': ['missing_with:name,birthdate']
The field under validation must not be present only if all of the other specified fields are present.
'age': ['missing_with_all:name,birthdate']
The field under validation must not be included in the given list of values.
'user_type': ['not_in:admin,supervisor']
The field under validation may be null
or not existed in the request body.
'description': ['nullable']
The field under validation must be a valid number.
'age': ['required', 'number', 'min:20']
The field under validation must match the given regular expression.
'formula': ['required', 'regex:[1-9]']
The field under validation must be present in the input data and not empty.
'age': ['required']
The field under validation must be present and not empty if the anotherfield field is equal to any value.
// age is required if name field is existed and not empty
'age': ['required_if:name']
// age is required if name field is existed and has value of 'jane'
'age': ['required_if:name,jane']
The field under validation must be present and not empty unless the anotherfield field is equal to any value. This also means anotherfield must be present in the request data unless value is null
. If value is null
(required_unless:name,null
), the field under validation will be required unless the comparison field is null
or the comparison field is missing from the request data.
// age is required unless name field is existed and not empty
'age': ['required_if:name']
// age is required unless name field is existed and has value of 'jane'
'age': ['required_if:name,jane']
The field under validation must be present and not empty only if any of the other specified fields are present and not empty.
'age': ['required_if:name,email']
The field under validation must be present and not empty only if all of the other specified fields are present and not empty.
'age': ['required_if:name,email,password']
The field under validation must be present and not empty only when any of the other specified fields are empty or not present.
age': ['required_if:birthdate']
The field under validation must be present and not empty only when all of the other specified fields are empty or not present.
age': ['required_if:birthdate,year_of_birth']
The field under validation must start with one of the given values.
'url': ['strats_with:www,api']
The field under validation must be a string.
'username': ['required', 'string']
The field under validation must be a valid URL.
'url': ['url', strats_with:www,api']