Skip to content

json-schema-tools/validator

Repository files navigation

JSON Schema Validator

CircleCI branch npm GitHub release GitHub commits since latest release

This package implements a JSON Schema validator.

Features

  • dead simple
  • faster than sonic
  • completely synchronous
  • circular reference detection & handling
  • errors if it finds any $refs - use the dereferencer before using the validator.
  • you get the json schema draft based on meta-schema.

Getting Started

npm install @json-schema-tools/validator
import Validator, { ValidatorErrors, ReferenceError } from "@json-schema-tools/validator"
import { JSONSchema } from "@json-schema-tools/meta-schema";

const mySchema: JSONSchema = {
  title: "baz",
  type: "object",
  properties: {
    foo: {
      title: "foo",
      type: "array",
      items: { type: "string" }
    },
    bar: {
      title: "bar",
      anyOf: [
        { title: "stringerific", type: "string" },
        { title: "numberoo", type: "number" }
      ]
    }
  }
};

const valid = validator(mySchema, { foo: ["hello", "world" ], bar: 1 });
// true

const invalid = validator(mySchema, { foo: ["hello", "world" ], bar: true });
// invalid instanceof ValidationErrors === true
// invalid.errors is an array of the error types

See https://json-schema-tools.github.io/validator/ for typedocs

Contributing

How to contribute, build and release are outlined in CONTRIBUTING.md, BUILDING.md and RELEASING.md respectively. Commits in this repository follow the CONVENTIONAL_COMMITS.md specification.