NOTE: README is out-of-date as things get rebuilt for v3. Please see v2.5.0 README for production usage.
valida2 - A lightweight sanitizer and validator library for Node.js.
This document describes how the valida2 library works and which features it offers. Each section of this document includes usage examples. You can find additional examples at examples folder.
npm intall valida2
const valida = require('valida2');
var schema = {
id: [
{ sanitizer: Valida.Sanitizer.toInt },
{ validator: Valida.Validator.required }
],
age: [
{ sanitizer: Valida.Sanitizer.toInt },
{ validator: Valida.Validator.required, groups: ['create'] }
],
name: [
{ validator: Valida.Validator.required, groups: ['update'] }
],
answers: [
{ validator: Valida.Validator.array },
{ validator: Valida.Validator.len, min: 3, max: 10 },
]
};
var person = { age: '10', answers: ['A', 'D'] };
Valida.process(person, schema, function(err, ctx) {
if (err) return console.log(err);
if (!ctx.isValid()) return console.log(ctx.errors());
console.log('valid', person);
}, ['create']);
- Sanitization
- Synchronous and asynchronous validation
- Groups
- Extensible
All the features are applied through the process
function.
Valida.process(
@data,
@schema,
@callback,
@group
);
options:
@data
is the object to be applied the sanitization and validation@schema
is an object describing to valida2 how to process it@callback
is a function that is going to be called after processing the data@group
is a string or array describing which groups must be applied in this process (optional)
valida2 supports synchronous sanitization.
toInt
toFloat
toDate
trim
string
lowerCase
titleCase
upperCaseFirst
upperCase
toBool
options:
radix
(optional, default 10)
var schema = {
age: [{ sanitizer: Valida.Sanitizer.toInt }]
};
options:
precision
(optional)
var schema = {
salary: [{ sanitizer: Valida.Sanitizer.toFloat }]
};
var schema = {
birthday: [{ sanitizer: Valida.Sanitizer.toDate }]
};
options:
chars
(optional)
var schema = {
name: [{ sanitizer: Valida.Sanitizer.trim }]
};
var schema = {
name: [{ sanitizer: Valida.Sanitizer.string }]
};
var schema = {
name: [{ sanitizer: Valida.Sanitizer.lowerCase }]
};
var schema = {
name: [{ sanitizer: Valida.Sanitizer.titleCase }]
};
var schema = {
name: [{ sanitizer: Valida.Sanitizer.upperCaseFirst }]
};
var schema = {
name: [{ sanitizer: Valida.Sanitizer.upperCase }]
};
var schema = {
published: [{ sanitizer: Valida.Sanitizer.toBool }]
};
Valida supports both synchronous and asynchronous validation.
required
empty
regex
len
array
plainObject
date
integer
enum
bool
float
range
custom
Field is required.
var schema = {
age: [{ validator: Valida.Validator.required }]
};
Field must be not empty.
var schema = {
description: [{ validator: Valida.Validator.empty }]
};
Validation based in a regex.
options:
pattern
: regex patternmodifiers
: regex modifier (optional)
var schema = {
name: [{ validator: Valida.Validator.regex, pattern: '[A-Z]', modifiers: 'i' }]
};
Validation based in the size of an array or in the number of chars of a non-array.
options:
min
max
var schema = {
products: [{ validator: Valida.Validator.len, min: 2, max: 10 }]
};
Field must be an array.
var schema = {
products: [{ validator: Valida.Validator.array }]
};
Field must be a plain object.
var schema = {
person: [{ validator: Valida.Validator.plainObject }]
};
Field must be a date.
var schema = {
createdAt: [{ validator: Valida.Validator.date }]
};
Field must be a integer.
var schema = {
createdAt: [{ validator: Valida.Validator.integer }]
};
Field value must be list of valid values.
options:
items
: an array with the valid values
var schema = {
color: [{ validator: Valida.Validator.enum, items: ['blue', 'black', 'white'] }]
};
Field must be a bool.
options:
default
var schema = {
published: [{ validator: Valida.Validator.bool, default: false }]
};
Field must be a float.
var schema = {
salary: [{ validator: Valida.Validator.float }]
};
Field value must be between a min and/or max value.
options:
min
: The minimum value of the rangemax
: The maximum value of the range
var schema = {
code: [{ validator: Valida.Validator.range, min: 0, max: 10 }]
};
Allows reuse the same schema validation for multiple actions. For example on creating an item a specific field is required. But on updating it that field is optional.
var schema = {
id: [{ validator: Valida.Validator.required, groups: ['update'] }]
products: [{ validator: Valida.Validator.array, groups: ['create'] }]
};
Valida.process(data, schema, function(err, ctx) {
console.log('create', create);
}, 'create');