vivocha/jsonpolice

Name: jsonpolice

Owner: Vivocha

Description: A Javascript library implementing JSON Schema draft 4.

Created: 2016-01-19 12:34:09.0

Updated: 2016-10-31 18:50:48.0

Pushed: 2017-11-10 11:19:09.0

Homepage:

Size: 176

Language: JavaScript

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

jsonpolice

A Javascript library implementing the JSON Schema specifications. Version 4 (draft) of the specification is supported by default, additional versions can be registered via the addVersion function.

The library decorates parsed objects in order to have them return default values defined the in the schema, for undefined properties.

travis build codecov coverage npm version

Install
m install jsonpolice
create(dataOrUri [, options])

Create a new instance of schema validator.

The function returns a Promise resolving to a new instance of Schema. Once created, a schema instance can be used repeatedly to validate data, calling the method Schema.validate.

Example
jsonpolice = require('jsonpolice');

police.create({
pe: 'object',
operties: {
d: {
  type: 'string',
  format: 'date-time'
},
i: {
  type: 'integer'
},
b: {
  type: [ 'boolean', 'number' ]
},
c: {
  default: 5
}

hen(function(schema) {
nsole.log(schema.validate({
d: (new Date()).toISOString(),
i: 6,
b: true
);

Schema.validate(data)

Validates the input data

Returns a decorated version of data, that returns default values of undefined properties, according to the schema used to validate the data. Throws a ValidationError exception in case an error is encountered.

Additionally, type coercion is applied when possible and needed, as described in the following table:

| Type | Format | Input type | Output type | Conversion | | — | — | — | — | — | | string | date-time | string | Date | output = new Date(input) | | number | | string | number | output = +input | | boolean | | string | boolean | true if “true” or “1”, false if “false” or “0” | | Array | | string | Array | output = input.split(',') |

For arrays, the library supports coercion from strings using by the default the comma-separated format (csv). Similarly to the OpenAPI specification (Swagger), it's possible to specify a different format using the collectionFormat property: the supported formats are csv, ssv, tsv and pipes.

Example

Using the following schema:


pe: 'object',
operties: {
d: {
  type: 'string',
  format: 'date-time'
},
i: {
  type: 'integer'
},
b: {
  type: [ 'boolean', 'number' ]
},
c: {
  default: 5
}


And parsing the following data:

output = schema.validate({
 '2016-03-18T16:33:46.651Z',
 '10',
 '1',
 "5,7"

Produces the following output:


": "2016-03-18T16:33:46.651Z",
": 10,
": true,
": [
5,
7



ut.c === 5; // true
lt.d instanceof Date; // true

This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.