Have ideas to improve npm?Join in the discussion! »

@bpmn-io/json-schema-validator

0.1.0 • Public • Published

json-schema-validator

Build Status

Validate JSON based on JSON Schema files.

Installation

$ npm i --save @bpmn-io/json-schema-validator

Usage

Given

import { Validator } from '@bpmn-io/json-schema-validator';
 
import schema from './test/fixtures/json-schema/schema.json';
 
import sample from './test/fixtures/samples/invalid-name.json';
 
const validator = new Validator({
  schema: schema
});
 
const {
  valid,
  errors 
= validator.validate(sample);
 
if (!valid) {
  console.error('Invalid JSON detected:', errors);
}
 

This will print detailed information about errors inside the sample

[
  {
    "message": "must start with <number_>",
    "keyword": "errorMessage",
    "dataPath": "/properties/0/name",
    "schemaPath": "#/properties/properties/items/allOf/0/then/properties/name/errorMessage",
    "params": {
      "rawErrors": [
        {
          "keyword": "pattern",
          "dataPath": "/properties/0/name",
          "schemaPath": "#/properties/properties/items/allOf/0/then/properties/name/pattern",
          "params": {
            "pattern": "^(number_)"
          },
          "message": "should match pattern \"^(number_)\""
        }
      ]
    }
  }
]

It's also possible to validate multiple objects at once

import Validator from '@bpmn-io/json-schema-validator';
 
import schema from './test/fixtures/json-schema/schema.json';
 
import samples from './test/fixtures/samples/multiple-samples.json';
 
const validator = new Validator({
  schema: schema
});
 
const {
  valid,
  results 
= validator.validateAll(samples);
 
if (!valid) {
  console.error('Invalid JSON objects detected:', results.filter(r => !r.valid));
}
 

Example JSON Schemas

License

MIT

Install

npm i @bpmn-io/json-schema-validator

DownloadsWeekly Downloads

1

Version

0.1.0

License

MIT

Unpacked Size

10 kB

Total Files

6

Last publish

Collaborators

  • avatar
  • avatar
  • avatar
  • avatar
  • avatar
  • avatar
  • avatar