fluent-validator
Fluent validator that enables validation on multiple parameters at once.
Chained validation
Express paginated endpoint
Example with express paginated endpoint. Both query parameters (page and size) must be positive integers or empty values.
var validator = ;app;
Separating validation rules
OR Separator
// Example: chain1.or.chain2or;
- if
value
passes validationchain1
thanchain2
is omitted and validation passes successfully. - if
value
does not pass validationchain1
thanchain2
is checked. - if
value
does not any of validation chains then errors are produced fromchain1
.
AND Separator
// Example: chain1.and.chain2and;
- if
value
passes validationchain1
thanchain2
is checked as well. - if
value
does not pass validationchain1
thanchain2
is omitted and errors are produced fromchain1
.
Result checking
You can react on validation result in multiple ways:
var validation =andorandor;validation; // Returns array of validation errorsvalidation; // Returns true if there are validation errorsvalidation; // Returns true if there are no validation errorsvalidation; // Throws error if there are validation errors.
Simple shortcut
You can shorten validation chain:
var validation1 = andor;var validation2 = andor;// validation2 is just a shorter version of validation1
Simple validation
Validation without chaining.
var validator = ;// isPositive: just executes checks if input > 0validator; // truevalidator; // falsevalidator; // truevalidator; // falsevalidator; // truevalidator; // falsevalidator; // truevalidator; // falsevalidator; // falsevalidator; // false
Configuration
Customizing the validator.
var validator = ;// Adding custom validationsvalidator;validator;// Adding custom error thrower used in validation.throwOnError()validator {'Validation error. ' + errors;});
Validations
List of available validations.
Comparisons
- isIn(value, arr) - check if value is in array
- isPositive(value) - check if
value > 0
- isNegative(value) - check if
value < 0
- isNonNegative(value) - check if
value >= 0
- isNonPositive(value) - check if
value <= 0
- isLower(value, bound) - check if
value < bound
- isLowerOrEql(value, bound) - check if
value <= bound
- isGreater(value, bound) - check if
value > bound
- isGreaterOrEql(value, bound) - check if
value >= bound
- isInRange(value, min, max) - check if
value > min && value < max
- isInRangeOrEql(value, min, max) - check if
value >= min && value <= max
Dates
- isDate(value) - check if value is of type
Date
or can be parsed withDate.parse()
- isAfter(value, min) - check if
value > min
- isAfterOrEql(value, min) - check if
value >= min
- isBefore(value, max) - check if
value < max
- isBeforeOrEql(value, max) - check if
value <= max
Numbers
- isInt(value) - check if value is an numerical or textual representation of an integer
- isFloat(value) - check if value is an numerical or textual representation of a float
- isNumber(value) - check if value is an numerical or textual representation of a number
- isHexadecimal(value) - check if value is an numerical or textual representation of a hexadecimal number
- isDivisibleBy(value, x) - check if value is an numerical or textual representation of a number that is divisible by
x
String
- contains(value, text) (alias: isIn) - check if value is in text
- isLength(value, length) - check if value is of given length
- matches(value, regexp) - check if
regexp
matchesvalue
- isAlpha(value) - check if value is contains only
[a-zA-Z]
- isNumeric(value) - check if value is contains only
[0-9]
- isAlphanumeric(value) - check if value is contains only
[a-zA-Z0-9]
- isLowercase(value) - check if value is lowercased
- isUppercase(value) - check if value is uppercased
Other
- isAscii(value) - check if value is contains only ASCII characters
- isEmail(value) - email RegExp validation
- isURL(value) - URL RegExp validation
- isIP(value) - IP RegExp validation
- isBase64(value) - Base64 RegExp validation
- isHexColor(value) - Base64 RegExp validation
- isUUID(value) - UUID RegExp validation
- isJSON(value) - UUID RegExp validation
- isCreditCard(value) - CreditCard RegExp validation
- isISBN(value) - ISBN RegExp validation
- isMongoObjectId(value) - MongoObjectId RegExp validation
- isNull(value) - checks if
value === null
- isNotNull(value) - checks if
value !== null
- isUndefined(value) - checks if
value === undefined
- isNotUndefined(value) - checks if
value !== undefined
- isNullOrUndefined(value) - checks if
value === undefined || value === null
- isNotNullOrUndefined(value) - checks if
value !== undefined && value !== null
- isEmpty(value) - checks if value is defined and is a non empty array or non empty object or non empty string
- isNotEmpty(value) - negation of
isEmpty
Custom validation
- passes(value, check, message) - checks if
value
passescheck
function. In case of validation errormessage
parameter is used.