@mojule/schema-mapper
v0.3.0
Published
Maps between schema and models
Downloads
7
Readme
schema-mapper
npm install @mojule/schema-mapper
const Mapper = require( '@mojule/schema-mapper' )
const { from, to } = Mapper()
let data = {
name: 'Nik',
age: 37,
classes: [ 'foo', 'bar' ]
}
const schema = to( data )
{
"type": "object",
"name": "object",
"id": "",
"properties": {
"name": {
"type": "string",
"name": "name",
"id": "/name"
},
"age": {
"type": "number",
"name": "age",
"id": "/age"
},
"classes": {
"type": "array",
"name": "classes",
"id": "/classes",
"items": {
"type": "string",
"name": "string",
"id": "/classes[]"
}
}
}
}
data = from( schema )
{
"name": "",
"age": 0,
"classes": [
""
]
}
const Mapper = require( '@mojule/schema-mapper' )
const { from, to } = Mapper( { omitDefault: false } )
let data = {
name: 'Nik',
age: 37,
classes: [ 'foo', 'bar' ]
}
const schema = to( data )
{
"type": "object",
"name": "object",
"id": "",
"properties": {
"name": {
"type": "string",
"name": "name",
"id": "/name",
"default": "Nik"
},
"age": {
"type": "number",
"name": "age",
"id": "/age",
"default": 37
},
"classes": {
"type": "array",
"name": "classes",
"id": "/classes",
"default": [
"foo",
"bar"
],
"items": {
"id": "/classes[]",
"name": "any"
}
}
}
}
data = from( schema )
{
"name": "Nik",
"age": 37,
"classes": [
"foo",
"bar"
]
}