tlvince-json-clay v1.3.0
JSON Clay
JSON schema validation, fake data generation and migrations.
Usage
var Clay = require('json-clay')
var person = new Clay({
schema: {
id: 'https://schema.ehealthafrica.org/1.0/person#',
$schema: 'http://json-schema.org/draft-04/schema#',
allOf: [
{
$ref: 'https://schema.ehealthafrica.org/1.0/base#'
}
],
type: 'object',
properties: {
name: {
type: 'string',
minLength: 1
},
age: {
type: 'number'
}
},
required: ['name']
},
defaults: {
type: 'person',
version: '1.2.3'
}
})
person.validate({
type: 'person',
version: '1.2.3'
})
// =>
// {
// validation: {
// name: {
// required: true
// }
// }
// }
person.generate({
version: '1.0.0',
})
// =>
// {
// "type": "person",
// "version": "1.0.0"
// }
// The future:
// person.migrate({
// version: '1.0.0'
// firstName: 'Audrey',
// lastName: 'Horne'
// })
// // =>
// // [
// // {
// // version: '1.2.3'
// // name: 'Audrey Horne'
// // }
// // ]Constructor: new Clay([options])
Create a new JSON schema clay.
options.schema- JSON schema of the clay. Default is the base schema.options.refs- Array of referenced schemas.options.defaults- Default properties used for generating fake data.
Validation: clay.validate(json)
Validate the attribjson against the schema.
Returns undefined if the data is valid, otherwise an array of errors.
Validation Errors
A typical error object looks like this:
{
validation: {
name: {
required: true
}
}
}Generate Fake Data: clay.generate([attributes[, opts]])
Use this method if you want to get fake data. Utilizes json-schema-faker.
If an attributes object is provided, its properties will be used instead of
faked values.
Passing opts modifies json-schema-faker behaviour:
opts.all: generate fake data for all of the schema's properties, not just required properties.
Clay.schema
Holds the schema.
Clay.refs
Holds the refs.
Clay.defaults
Holds the defaults.
Clay.properties
Holds the concatenation of the schema's properties and all of its references properties.
CLI
Use it to create a simple command line utility which generates fake data:
var Clay = require('json-clay')
var cli = require('json-clay/cli')
var person = new Clay()
cli(person, process.argv.slice(2))Shell usage:
./cli.js
{
"type": "person",
"version": "1.2.3"
}Arguments will be added as attributes:
./cli.js --name Matt
{
"type": "person",
"version": "1.2.3",
"name": "Matt"
}Arguments after -- will be passed as options to generate:
./cli.js --name Matt -- --all
{
"type": "person",
"version": "1.2.3",
"name": "Matt",
"age": 27
}Browserify Build
npm run buildCreates a browserified release in dist/json-clay.js.
This build does not include the fake data generator.
Tests
npm testAuthor
© 2015 eHealth Systems Africa