JSPM

  • Created
  • Published
  • Downloads 63144
  • Score
    100M100P100Q161960F
  • License MIT

dynamic json-schema validator

Package Exports

  • djv

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (djv) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Build Status Join the chat at https://gitter.im/korzio/djv

djv

Dynamic JSON Schema Validator

Current package supports JSON Schema v4 and it contains utils for validating objects against schemas. This is a part of djv packages aimed to work with json-schema.

  • djv validate object against schemas
  • djvi instantiate objects by schema definition
  • jvu utilities for declarative, FP usage

Installation

npm install djv

Usage

var env = new djv();
var jsonSchema = {
    "common": {
        "properties": {
            "type": {
                "enum": ["common"]
            }
        },
        "required": [
            "type"
        ]
    }
};

// Use `addSchema` to add json schema
env.addSchema('test', jsonSchema);
env.validate('test#/common', { type: 'common' });
// => undefined

env.validate('test#/common', { type: 'custom' });
// => 'required: data'

API

addSchema(name: String, schema: Object) -> resolved: Object

Add a schema to a current djv environment,

env.addSchema('test', jsonSchema);
/* => {
    fn: function f0(data){...}
    name: 'test'
    schema: ...
} */

validate(name: String, object: Object) -> error: String

Check if object is valid against the schema

env.validate('test#/common', { type: 'common' });
// => undefined

env.validate('test#/common', { type: 'custom' });
// => 'required: data'

where

  • name - schema path in current environment
  • object - object to validate
  • error - undefined if it is valid

removeSchema(name: String)

Remove a schema or the whole structure from the djv environment

env.removeSchema('test');

resolve(name: String)

Resolve the name by existing environment

env.resolve('test');
// => { name: 'test', schema: {} }, fn: ... }

export(name: String?) -> state: Object

Exports the whole structure object from environment or resolved by a given name

env.export();
// => { test: { name: 'test', schema: {}, ... } }

where state is an internal structure or only resolved schema object

import(config: Object)

Imports all found structure objects to internal environment structure

env.import(config);

Tests

npm test