@vitalets/micro-schema
v0.1.3
Published
JavaScript implementation of json-micro-schema validation format
Downloads
3
Readme
micro-schema
JavaScript implementation of JSON micro schema validation format.
Contents
Installation
npm install @vitalets/micro-schema
Usage
Require
validate
function:const { validate } = require('@vitalets/micro-schema');
Define schema:
const schema = { productId: { $type: 'number', $required: true }, productName: { $type: 'string', $required: true, $maxLength: 255 }, tags: [{ $type: 'string' }] };
Validate object:
const object = { productId: '1', productName: undefined, tags: [42] }; const errors = validate(schema, object);
Handle validation errors:
[ { "validator": "$type", "path": "productId", "expectedType": "number", "actualType": "string" }, { "validator": "$required", "path": "productName" }, { "validator": "$type", "path": "tags.0", "expectedType": "string", "actualType": "number" } ]
Docs
Please see JSON micro schema docs for all available validators.
License
MIT @ Vitaliy Potapov