JSPM

  • Created
  • Published
  • Downloads 63888240
  • Score
    100M100P100Q250934F
  • License MIT

Custom JSON-Schema keywords for ajv validator

Package Exports

  • ajv-keywords
  • ajv-keywords/keywords/instanceof
  • ajv-keywords/keywords/typeof

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (ajv-keywords) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

ajv-keywords

Custom JSON-Schema keywords for ajv validator

Build Status npm version

Install

npm install ajv-keywords

Usage

var Ajv = require('ajv');
var defineKeywords = require('ajv-keywords');

var ajv = new Ajv;
defineKeywords(ajv, 'instanceof');

ajv.validate({ instanceof: 'RegExp' }, /.*/); // true
ajv.validate({ instanceof: 'RegExp' }, '.*'); // false

or if using in browser (to avoid adding unused code):

var Ajv = require('ajv');
var ajv = new Ajv;
var instanceofDefinition = require('ajv-keywords/keywords/instanceof')
ajv.addKeyword('instanceof', instanceofDefinition);

Keywords

typeof

Based on JavaScript typeof operation.

The value of the keyword should be a string: "undefined", "string", "number", "object", "function", "boolean" or "symbol".

To pass validation the result of typeof operation on the value should be equal to the string.

ajv.validate({ typeof: 'undefined' }, undefined); // true
ajv.validate({ typeof: 'undefined' }, null); // false

instanceof

Based on JavaScript typeof operation.

The value of the keyword should be a string: "Object", "Array", "Function", "Number", "String", "Date", "RegExp" or "Buffer".

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

You can add your own constructor function to be recognised by this keyword:

function MyClass() {}
instanceofDefinition.CONSTRUCTORS.MyClass = MyClass;

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

License

MIT