0.1.0 • Published 7 years ago

@calmdownval/json-schema v0.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
7 years ago

JSON Schema Validator

NOTICE: This module uses the ECMAScript import/export mechanism and requires Node v10+. Please refer to Node's documentation to read more on how to enable this functionality in your environment.

Aimed to avoid generating a validator function as string and using eval or new Function to convert it to an actual function. Object oriented design allows for multiple draft version support as well as custom rule support. The library has no dependencies (no Node-native modules either) and works in browsers out of the box.

Currently the library fully supports JSON Schema draft-07 (older drafts are not implemented) and runs significantly slower compared to other JSON Schema libraries (i.e. ~3% relative to djv, see the json-schema-benchmark). Performance tweaks and optimizations are currently a work in progress.

Installation

npm install @calmdownval/json-schema

Usage

import { Validator } from '@calmdownval/json-schema';


// creates a new Validator instance initialized
// uses draft-07 rules by default
const validator = new Validator();


// compiles the schema to a new CompiledSchema instance
// which can be used (repeatedly) to validate objects
const schema = validator.compile(
    {
        type : 'object',
        properties :
        {
            foo : { type : 'number' },
            bar : { type : 'string' }
        }
    });

// validates the object against the compiled schema
// runs in BOOLEAN mode by default
const valid = schema.validate(
    {
        foo : 123,
        bar : 'hi!'
    });

// will print out 'true'
console.log(valid);