1.0.1 • Published 7 years ago

json-schema-util v1.0.1

Weekly downloads
16
License
MIT
Repository
github
Last release
7 years ago

JSON schema util

This is a utility to validate data against a JSON schema.

example

const {validate} = require('json-schema-util');

// set a json schema
const schema = {
  type: "object",
  properties: {
    testNum: {
      type: "number"
    },
    testString: {
      type: "string"
    }
  }
};

Validation success:

const data = { testNum: 1 };
const res = validate(data, schema);

console.log(res.isValid) // true

Validation error:

const data = { testNum: 'a', testString: 1 };
const res = validate(data, schema);

console.log(res.isValid) // false
console.log(res.errors)  // {isValid: false, errors: {{ value: 'a', property: 'tesnNum', message: '...'}}};