Code Monkey home page Code Monkey logo

json-schema-validator's Introduction

json-schema-validator

CI

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",
    "dataPointer": {
      "key": { "line": 5, "column": 6, "pos": 97 },
      "keyEnd": { "line": 5, "column": 12, "pos": 103 },
      "value": { "line": 5, "column": 14, "pos": 105 },
      "valueEnd": { "line": 5, "column": 19, "pos": 110 }
    },
    "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

json-schema-validator's People

Contributors

barmac avatar nikku avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

json-schema-validator's Issues

Investigate different Validator provider

What should we do?

Currently, we use ajv as the underlying provider to validate objects against a schema (cf. decision was made here: #1). ajv gives us the possibility to provide custom errorMessage properties in a flexible manner via ajv-errors, especially to make them human-readable.

However ajv comes with a big bundle size. In the meantime, there are alternatives who might fit better in this regard. On the other hand, ajv is forcing us a bit into their way of doing stuff (cf. #4, migration to v7 does not work well on node@14).

Why should we do it?

We could investigate a lightweight alternative for ajv to drop the downsides described above. The following criteria have to be considered

Resources

Adapted JSON Schema Validators for Javascript

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.