All Projects → korzio → Djv

korzio / Djv

Licence: mit
Dynamic JSON Schema Validator - Supports draft-04/06

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Djv

Ajv I18n
Internationalised error messages for Ajv JSON-Schema validator
Stars: ✭ 98 (-65.12%)
Mutual labels:  json-schema, validator
Jsonschema
JSONSchema (draft04, draft06, draft07) Validation using Go
Stars: ✭ 261 (-7.12%)
Mutual labels:  json-schema, validator
Ajv Cli
Use 'Another Json Validator' (ajv) from the command line
Stars: ✭ 148 (-47.33%)
Mutual labels:  json-schema, validator
Swagger Parser
Swagger 2.0 and OpenAPI 3.0 parser/validator
Stars: ✭ 710 (+152.67%)
Mutual labels:  json-schema, validator
schema
SpaceAPI JSON schema files.
Stars: ✭ 20 (-92.88%)
Mutual labels:  json-schema, validator
Schemasafe
A reasonably safe JSON Schema validator with draft-04/06/07/2019-09 support.
Stars: ✭ 67 (-76.16%)
Mutual labels:  json-schema, validator
Ajv Keywords
Custom JSON-Schema keywords for Ajv validator
Stars: ✭ 186 (-33.81%)
Mutual labels:  json-schema, validator
Ajv Errors
Custom error messages in JSON-Schema for Ajv
Stars: ✭ 185 (-34.16%)
Mutual labels:  json-schema, validator
finspec-spec
Multi-protocol, machine-readable specifications for financial services
Stars: ✭ 18 (-93.59%)
Mutual labels:  json-schema, validator
another-json-schema
Another JSON Schema validator, simple & flexible & intuitive.
Stars: ✭ 48 (-82.92%)
Mutual labels:  json-schema, validator
Formily
Alibaba Group Unified Form Solution -- Support React/ReactNative/Vue2/Vue3
Stars: ✭ 6,554 (+2232.38%)
Mutual labels:  json-schema, validator
Maat
Validation and transformation library powered by deductive ascending parser. Made to be extended for any kind of project.
Stars: ✭ 27 (-90.39%)
Mutual labels:  json-schema, validator
Swagger Cli
Swagger 2.0 and OpenAPI 3.0 command-line tool
Stars: ✭ 321 (+14.23%)
Mutual labels:  json-schema, validator
Ajv
The fastest JSON schema Validator. Supports JSON Schema draft-04/06/07/2019-09/2020-12 and JSON Type Definition (RFC8927)
Stars: ✭ 10,340 (+3579.72%)
Mutual labels:  json-schema, validator
Validr
A simple, fast, extensible python library for data validation.
Stars: ✭ 205 (-27.05%)
Mutual labels:  json-schema, validator
openui5-validator
A library to validate OpenUI5 fields
Stars: ✭ 17 (-93.95%)
Mutual labels:  json-schema, validator
ty
Here is a schema checker which can return well typed results. Tell your friends!
Stars: ✭ 21 (-92.53%)
Mutual labels:  json-schema, validator
apispec
A Common Lisp library for handling Web API requests and responses.
Stars: ✭ 26 (-90.75%)
Mutual labels:  validator
ts-json-validator
JSON Validator for TypeScript - Safer JSON.parse() validating by TypeScript types
Stars: ✭ 23 (-91.81%)
Mutual labels:  validator
Argus
Builds models from JSON Schemas
Stars: ✭ 106 (-62.28%)
Mutual labels:  json-schema

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

djv

Dynamic JSON-Schema Validator

Official Documentation

Current package supports JSON-Schema v6 and v4. It contains utils to validate 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
  • @djv/draft-04 environment updates to support draft-04

Any contributions are welcome. Check the contribution guide. Since version 1.2.0 djv package supports draft-06. Version 2.0.0 makes draft-06 the default schema version. To use other versions check the environment section.

Table of contents

Install

npm install djv

or

<script src="djv.js"></script>

There are 2 versions of validator

  • ./lib/djv.js a default one, not uglified and not transpiled
  • ./djv.js a built one with a webpack, babel and uglify (preferable for frontend)

Usage

const env = new djv();
const 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

Environment

To instantiate djv environment

const djv = require('djv');
const env = djv({
  version: 'draft-06', // use json-schema draft-06
  formats: { /*...*/ }, // custom formats @see #addFormat
  errorHandler: () => { /*...*/ }, // custom error handler, @see #setErrorHandler
});

To use a previous version of JSON-Schema draft, use a draft-04 plugin, specified in optionalDependencies of djv package.

const env = new djv({ version: 'draft-04' });

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

Export 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)

Import all found structure objects to internal environment structure

env.import(config);

addFormat(name: string, formatter: string/function)

Add formatter to djv environment. When a string is passed it is interpreted as an expression which when returns true goes with an error, when returns false then a property is valid. When a function is passed it will be executed during schema compilation with a current schema and template helper arguments.

env.addFormat('UpperCase', '%s !== %s.toUpperCase()');
// or
env.addFormat('isOk', function(schema, tpl){
  return `!${schema.isOk}`;
});
env.validate('ok', 'valid') // => undefined if schema contains isOk property

setErrorHandler(errorHandler: function)

Specify custom error handler which will be used in generated functions when problem found. The function should return a string expression, which will be executed when generated validator function is executed. The simplist use case is the default one @see template/defaultErrorHandler

 function defaultErrorHandler(errorType) {
   return `return "${errorType}: ${tpl.data}";`;
 }

It returns an expression 'return ...', so the output is an error string.

djv({ errorHandler: () => 'return { error: true };' }) // => returns an object
djv({
  errorHandler(type) {
    return `errors.push({
      type: '${type}',
      schema: '${this.schema[this.schema.length - 1]}',
      data: '${this.data[this.data.length - 1]}'
    });`;
  }
});

When a custom error handler is used, the template body function adds a error variable inside a generated validator, which can be used to put error information. errorType is always passed to error handler function. Some validate utilities put extra argument, like f.e. currently processed property value. Inside the handler context is a templater instance, which contains this.schema, this.data paths arrays to identify validator position. @see test/index/setErrorHandler for more examples

useVersion(version: string, configure: function)

To customize environment provide a configure function which will update configuration for djv instance.

env.useVersion('draft-04')
// or
env.useVersion('custom', configure)

Configure will get internal properties as an argument. Check the @korzio/djv-draft-04 code.

exposed = {
  properties,
  keywords,
  validators,
  formats,
  keys,
  transformation,
}

!Important Modifying them will affect all djv instances in an application.

Tests

npm test

References

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].