0.0.1 • Published 6 years ago

fast-json-validator v0.0.1

Weekly downloads
1
License
MIT
Repository
github
Last release
6 years ago

Fast JSON Validator

A 10x faster way to validate json objects with a simple ast. This library is intended to be used as a transpilation target.

LibraryTest NameSingle RunBenchmark
ajvfstab22.926ms17,550 ops/sec
fast-json-validatorfstab0.67ms149,795 ops/sec

Installation

# npm
npm install --save fast-json-validator

# yarn
yarn add fast-json-validator

Usage

Although one can construct the schema by hand, it is not recommended. This library is more intended as a transpile target from other languages (think GraphQL, TypeScript, Flow,...).

import * as v from "fast-json-validate";

const data = {
  "/": "bar",
  "/bar/bob": {
    type: "ext4"
  },
}

// This should be generated by your library
const schema = v.record(
  v.union([
    v.string(),
    v.object(
      {
        type: v.enum(["ext4", "ext4"])
      },
      { required: ["type"]}
    )
  ]),
  { type: v.regexType(/^\//})
);

v.validate(data, schema);
// =>
// { error: undefined, valid: true}

FAQ

Q: Why another tool to validate json data?

This project was born because I got frustrated with creating automatic mocks out of json-schema definitions. The json-schema grammar allows to create conflicting schemas that never validate, is slow to verify (the validator has to make a lot of educated guesses), validation libraries tend to be a lot bigger than necessary, and are difficult to parse.

License

MIT, see License file.