easy-js-validator
v1.0.35
Published
A javascript validation package motivated by laravel default validator.
Readme
EASY-JS-VALIDATOR
This JavaScript validator package is inspired by Laravel's default validator.
How to use the Validator
const { Validator } = require('easy-js-validator');
const validator = new Validator({
name: "Alice",
age: "22",
isMarried: false
});
const rules = {
name: 'required|between:5,50|alpha',
age: 'between:20,30',
isMarried: 'boolean'
};
const errors = validator.validate(rules);How to pass custom validation error messages
const messages = {
name: {
required: 'Please enter :attribute, it is required.',
between: 'The lenght should be between 20 to 30 characters.',
}
};
const errors = validator.validate(rules,messages);Note: ":attribute" will be replace by the validating input name.
Available validation rules
required
The field under validation must be present in the input data and not empty. A field is "empty" if it meets one of the following criteria:
- The value is null.
- The value is an empty string.
- The value is an empty array or empty Countable object.
- The value is an uploaded file with no path.integer
The field under validation must be an integer.
decimal:minDecimalPlaces,maxDecimalPlaces
The field under validation must be an decimal number. Both minDecimalPlaces and maxDecimalPlaces are optional fields. If neither is provided, the function will only check if the given value is a decimal. If only minDecimalPlaces is provided, it will validate that the value has exactly the specified number of decimal places. If both minDecimalPlaces and maxDecimalPlaces are provided, it will ensure the value has a number of decimal places within the specified range.
min:value
The field under validation must have a minimum value. Strings, numerics, arrays, and files are evaluated in the same fashion as the size rule.
max:value
The field under validation must have a maximum value. Strings, numerics, arrays, and files are evaluated in the same fashion as the size rule.
between
The field under validation must have a size between the given min and max (inclusive). Strings, numerics, arrays, and files are evaluated in the same fashion as the size rule.
The field under validation must be formatted as an email address.
boolean
The field under validation must be able to be cast as a boolean. Accepted input are true, false, 1, 0, "1", and "0".
date
The field under validation must be a valid.
in:option1,option2,...
The field under validation must be included in the given list of values.
not_in:option1,option2,...
The field under validation must not be included in the given list of values.
url
The field under validation must not be a start with either http:// or https://.
alpha
The field under validation must contains only English lowercase(a-z) and upprcase(A-Z) alphabets.
alpha_num
The field under validation must contains only English lowercase(a-z), upprcase(A-Z) alphabets and 0-9.
alpha_dash
The field under validation must contains only English lowercase(a-z), upprcase(A-Z) alphabets and '_'.
alpha_num_dash
The field under validation must contains only English lowercase(a-z), upprcase(A-Z) alphabets, 0-9 and '_'.
same:field
The given field must match the field under validation.
regex:pattern
The field under validation must match the given regular expression. Eg. regex:^[a-zA-Z]+$ //It will check if given iput contains lettrs between a-z and A-Z.
starts_with:option1,option2,...
The field under validation must start with one of the given values.
ends_with:option1,option2,...
The field under validation must end with one of the given values.
