Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/marvinhagemeister/fast-json-validation

A faster way to validate json objects
https://github.com/marvinhagemeister/fast-json-validation

json json-schema schema validation

Last synced: 28 days ago
JSON representation

A faster way to validate json objects

Awesome Lists containing this project

README

        

# 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.

| Library | Test Name | Single Run | Benchmark | Size (gzip) |
|---|---|---|---|---|
| [ajv](https://github.com/epoberezkin/ajv) | fstab | 22.926ms | 17,550 ops/sec | 2.6kb |
| fast-json-validator | fstab | **0.67ms** | **149,795 ops/sec** | **1.6kb** |

## Installation

```bash
# 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,...).

```js
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](LICENSE.md).