You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 
Rich Trott 2981f24f92 tools: update ESLint to v3.8.0 8 years ago
..
keywords tools: update ESLint to v3.8.0 8 years ago
.eslintrc.yml tools: update ESLint to v3.8.0 8 years ago
LICENSE tools: update ESLint to v3.8.0 8 years ago
README.md tools: update ESLint to v3.8.0 8 years ago
index.js tools: update ESLint to v3.8.0 8 years ago
package.json tools: update ESLint to v3.8.0 8 years ago

README.md

ajv-keywords

Custom JSON-Schema keywords for ajv validator

Build Status npm version Coverage Status

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' }, /.*/); // true
ajv.validate({ instanceof: 'RegExp' }, '.*'); // false

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;
// or require('ajv-keywords/keywords/instanceof').definition;
instanceofDefinition.CONSTRUCTORS.MyClass = MyClass;

ajv.validate({ instanceof: 'MyClass' }, new MyClass); // true

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); // true
ajv.validate(schema, 2); // true
ajv.validate(schema, 3); // true
ajv.validate(schema, 0.99); // false
ajv.validate(schema, 3.01); // false

var schema = { range: [1, 3], exclusiveRange: true };
ajv.validate(schema, 1.01); // true
ajv.validate(schema, 2); // true
ajv.validate(schema, 2.99); // true
ajv.validate(schema, 1); // false
ajv.validate(schema, 3); // false

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); // true
ajv.validate(schema, invalidData); // false

regexp

This keyword allows to use regular expressions with flags in schemas (the standard pattern keyword does not support flags). The value of this keyword can be either a string (the result of regexp.toString()) or an object with the properties pattern and flags (the same strings that should be passed to RegExp constructor).

var schema = {
  type: 'object',
  properties: {
    foo: { regexp: '/foo/i' },
    bar: { regexp: { pattern: 'bar', flags: 'i' } }
  }
};

var validData = {
  foo: 'Food',
  bar: 'Barmen'
};

var invalidData = {
  foo: 'fog',
  bar: 'bad'
};

License

MIT