ajv-keywords
Custom JSON-Schema keywords for ajv validator
Install
npm install ajv-keywords
Usage
To add all available keywords:
var Ajv = require('ajv');
var ajv = new Ajv;
require('ajv-keywords')(ajv);
ajv.validate({ instanceof: 'RegExp' }, /.*/);
ajv.validate({ instanceof: 'RegExp' }, '.*');
To add a single keyword:
require('ajv-keywords')(ajv, 'instanceof');
To add multiple keywords:
require('ajv-keywords')(ajv, ['typeof', 'instanceof']);
To add a single keyword in browser (to avoid adding unused code):
require('ajv-keywords/keywords/instanceof')(ajv);
Keywords
typeof
Based on JavaScript typeof
operation.
The value of the keyword should be a string ("undefined"
, "string"
, "number"
, "object"
, "function"
, "boolean"
or "symbol"
) or array of strings.
To pass validation the result of typeof
operation on the value should be equal to the string (or one of the strings in the array).
ajv.validate({ typeof: 'undefined' }, undefined); // true
ajv.validate({ typeof: 'undefined' }, null); // false
ajv.validate({ typeof: ['undefined', 'object'] }, null); // true
instanceof
Based on JavaScript typeof
operation.
The value of the keyword should be a string ("Object"
, "Array"
, "Function"
, "Number"
, "String"
, "Date"
, "RegExp"
or "Buffer"
) or array of strings.
To pass validation the result of data instanceof ...
operation on the value should be true:
ajv.validate({ instanceof: 'Array' }, []); // true
ajv.validate({ instanceof: 'Array' }, {}); // false
ajv.validate({ instanceof: ['Array', 'Function'] }, funciton(){}); // true
You can add your own constructor function to be recognised by this keyword:
function MyClass() {}
var instanceofDefinition = require('ajv-keywords').get('instanceof').definition;
instanceofDefinition.CONSTRUCTORS.MyClass = MyClass;
ajv.validate({ instanceof: 'MyClass' }, new MyClass);
range
and exclusiveRange
Syntax sugar for the combination of minimum and maximum keywords, also fails schema compilation if there are no numbers in the range.
The value of this keyword must be the array consisting of two numbers, the second must be greater or equal than the first one.
If the validated value is not a number the validation passes, otherwise to pas validation the value should be greater (or equal) than the first number and smaller (or equal) than the second number in the array. If exclusiveRange
keyword is present in the same schema and its value is true, the validated value must not be equal to the range boundaries.
var schema = { range: [1, 3] };
ajv.validate(schema, 1);
ajv.validate(schema, 2);
ajv.validate(schema, 3);
ajv.validate(schema, 0.99);
ajv.validate(schema, 3.01);
var schema = { range: [1, 3], exclusiveRange: true };
ajv.validate(schema, 1.01);
ajv.validate(schema, 2);
ajv.validate(schema, 2.99);
ajv.validate(schema, 1);
ajv.validate(schema, 3);
propertyNames
This keyword allows to define the schema for the property names of the object. The value of this keyword should be a valid JSON schema (v5 schemas are supported with Ajv option {v5: true}
).
var schema = {
type: 'object'
propertyNames: {
anyOf: [
{ format: ipv4 },
{ format: hostname }
]
}
};
var validData = {
'192.128.0.1': {},
'test.example.com': {}
};
var invalidData = {
'1.2.3': {}
};
ajv.validate(schema, validData);
ajv.validate(schema, invalidData);
License
MIT