Advanced JSON Schema Validator
DEPRECATED
in favour of https://github.com/rainder/node-skerla-json-schema
Installation
$ npm install json-schema-validation-pipeline
Usage
var ValidationPipeline = ;var V = ValidationPipelineV; var validate = ; var result = ; resultisValid;resulterrors;
Example
var ValidationPipeline = ;var V = ValidationPipelineV; var objectToValidate = id: 1 name: 'Andrius' surname: 'Skerla' age: 26 email: 'andrius@skerla.com' role: 'Developer' address: country: 'UK' //city: 'London', post: 'W12 11X' ; var validate = ; var validationResult = ;console; // falseconsole;
Advanced Example
var validate = ; var result = ; // result.isValid === false// result.errors['o.b'] = 'Well, array at path `o.b` cannot contain string "Skerla".';
Interface
ValidationPipeline(pipeline) : ValidationFunction
Package exports Function
which accepts one argument validation pipeline Array
. Pipeline argument can be array
or object
. If object
is specified it is treated as $schema
pipeline argument.
; //equals to ;
ValidationFunction
accepts one argument which must be target object for validation. Returns object(errors: {}, isValid: Boolean)
.
Available pipeline methods
$schema
Checks object for property types and values.
Accepts Object(propertyPath: SchemaType)
. By default all SchemaTypes
are optional unless .required()
is called.
$take
Array key intersection. Removes keys from the object which are missing in the array.
{$take: ['first_name', 'last_name']}
$trim
Invokes .trim()
on every specified property
{$trim: ['name.first', 'last_name']}
$or
Checks if one and only one property is defined in the object.
Accepts Array(String)
.
$and
Checks if all or none of the properties are defined in the object.
Accepts Array(String)
.
$dependency
Checks if all dependencies are met.
Accepts Object(propertyPath: [String])
$cast
Casts number to string or string to number.
{$cast: { age: Number }}
Available SchemaTypes
String
Applicable methods:
- required
- len
- min
- max
- regexp
- oneOf
- fn
Number
Applicable methods:
- required
- min
- max
- oneOf
- fn
Boolean
Applicable methods:
- required
- fn
Function
Applicable methods:
- required
- fn
Object
Applicable methods:
- required
- fn
Array
Applicable methods:
- required
- len
- min
- max
- oneOf
- typeOf
- fn
SchemaType Methods
required()
Specifies that property is required in the JSON object
var result = name: 'Andrius'; // result.isValid == true
min(int)
Specifies min value of Number
or min length of the String
or min length of an Array
var result = bid: 10; // result.isValid == true
max(int)
Specifies max value of Number
or max length of the String
or max length of an Array
len(int)
Specifies the length of the string or array
regexp(RegExp)
Specifies regexp validation pattern for the property
oneOf(array)
Specifies possible values for the property
vegetable: 'tomato' fruits: 'apple' 'orange';
fn(Function(value, key, object))
Specifies custom validation function. Must not return a value if validation is successful.
var objectToValidate = values: 1 2 3; objectToValidate;
typeOf(SchemaType)
Specifies the type on the element inside the array.
var objectToValidate = values: 1 2 3 '4'; var result = objectToValidate; // result.errors['value.3'] = 'must be a number';
Running tests
$ npm test
License
MIT