jschemator

2.0.0 • Public • Published

jschemator

Simple npm package to validate an object using JSON Schema, and get errors in friendly form, useful to validate on frontend and backend!

Install

$ npm install jschemator

Options

Parameter Default Description
schema None The JSON schema to validate, this plugin uses ajv check the docs to create your schemas
locale 'en' Locale to error messages, this plugin uses ajv-i18n, see the doc to all available languages
options {allErrors: true, flat: false} ajv options, this plugin uses ajv see the doc to all options

Usage

Simple

const jschemator = require('jschemator')
 
const schema = {
  type: 'object',
  properties: {
    email: {
      type: 'string',
      format: 'email'
    }
  },
  required: ['email']
 };
 
 const model = {};
 let validator = jschemator(schema, 'en');
 
 const valid = validator.validate(model);
 // => false
 
 validator.errors;
 // => { email: { required: 'should have required property email' } }
 validator.paths;
 // => ['email.required']
 
 validator = jschemator(schema, 'en', {flat: true});
 
 const valid = validator.validate(model);
 
 validator.errors;
 // => { 'email..required': 'should have required property email' }
 validator.paths;
 // => ['email.required']
 

Package Sidebar

Install

npm i jschemator

Weekly Downloads

13

Version

2.0.0

License

Apache-2.0

Unpacked Size

301 kB

Total Files

10

Last publish

Collaborators

  • vhuerta