Wino validator is a template based validation framework for Javascript that allows you to validate named data and return errors in a nice organized format.
Wino validator is inspired by PHP's Framework laravel validation
To install run npm install wino-validator
. Here is an example of how to use it
const validator = require('wino-validator')
let validation_errors = validator.validate([
['ip_address-> required|ip:4', '123.257.123.25']
])
console.log(validation_errors)
The output should be
[ { field: 'ip', message: [ 'the \'ip\' field should be a version 4 ip address' ] } ]
because the ip address is not valid
Validation is easy. There are two validation options
Here is an example of an object array validation type:
let errors = validator.validate([
['email-> required|char_between: 50, 52|email', undefined]
])
console.log(errors)
Output:
[{
field: 'email',
message:[
'the \'email\' field is required',
'the \'email\' field should fall between 50 and 52 characters',
'the \'email\' field is not a valid email'
]
}]
Here is an example of an object array validation type:
let errors = validator.validateObject([
{
field: 'email',
data: 'company-a@gmail',
rules: 'required|char_between: 50, 52|email'
}
]);
console.log(errors)
Output:
[{ field: 'email',
message: [
'the \'email\' field should fall between 50 and 52 characters',
'the \'email\' field is not a valid email'
]
}]
Validation can be for more than one field as in the example below
let errors = validator.validate([
['email-> required|char_between: 50, 52|email', undefined],
['mobile_number-> required|min_char: 10|max_char: 10|numeric', '072398760']
])
console.log(errors)
Output:
[
{
field: 'email',
message:[
'the \'email\' field is required',
'the \'email\' field should fall between 50 and 52 characters',
'the \'email\' field is not a valid email'
]
},
{
field: 'mobile_number',
message:[
'the \'mobile_number\' field should be greater than or equal to 10 characters'
]
}
]
wino-validate supports a number of validation rules, below is a list of all the rules supported
- required
- contains
- equals
- alpha
- alpha_numeric
- numeric
- ascii
- base_64
- boolean
- bytes
- credit_card
- uri
- decimal
- decimal_digits
- bytes_between
- divisible_by
- fqdn
- float
- float_between
- full_width
- half_width
- hash
- hex_color
- hexadecimal
- ip
- isbn
- isin
- issn
- iso_8601_date
- rfc_3339
- iso_31661_alpha_2
- iso_31661_alpha_3
- isrc
- json
- lat_lng
- lowercase
- mac_address
- md5
- mobile_phone
- mime_type
- mongo_id
- multibyte
- port
- poastal_code
- surrogate_pair
- url
- uuid
- uppercase
- variable_width
- matches
- min_char
- max_char
- char_between
- digits
- in
- not_in
- date_equal
- date_before
- date_after
Validates that a field is not empty. A field is considered "empty" if one of the following conditions are true:
- The value is undefined.
- The value is null.
- The value is an empty string.
As of September 29, 2019 wino-validator is licensed under the GPLv3+