auto-json-schema
v1.0.0
Published
json-schema made easier
Downloads
3
Maintainers
Readme
auto-json-schema
json-schema without pain
• Why • What • How • License •
Why
Because generating json-schema by hand is hard, complex and error prone.
What
The boiler plate stuff are inferred from the values of a model.
var autoS = require('auto-json-schema')
var tedious = {
type: 'object',
properties: {'qty': {type: 'number'}, uid:{type: 'string'}},
$schema: 'http://json-schema.org/schema#'
}
var easy = autoS({
qty:0,
uid:''
}) //same result as tedious
The model can contain nested json-schema and additional optional json-schema fragments can be provided to tweek the inferred values.
var autoS = require('auto-json-schema')
var tedious = {
required: ['uid'],
type: 'object',
properties: {
user: {
type: 'object',
properties: {
age: {
minimum:18,
type: 'number'
},
uid: {
pattern: '[a-z]{4}',
type: 'string'
}
}
}
},
$schema: 'http://json-schema.org/schema#'
}
var easier = autoS({
user: {
age: autoS(0, { minimum:18 }),
uid: autoS('', { pattern:'[a-z]{4}' })
}
}, {
required: ['uid']
}) //same result as tedious
How
autoSchema(model, schemaFragment, schemaFragment, ...)
model
: any valid json structure. The value can be sub-schema. Examples:3
: a standalone primitive{value: 3, password: anotherSchemaObject}
: a mix of values, tructures and schema
schemaFragment
: any valid parts of the json-schema standard. Last entries take precedence.{type: 'integer'}
: to overide the inferred type
License
Released under the MIT License