Useful JSON Schema utility functions for forms.
npm i [email protected]:Prismatik/json-schema-form.git
To map a data object against your schema use mapData
.
- Fields that do exist in
schema
but are not supplied indata
will return asundefined
. - Fields that do not exist
schema
but are supplied indata
will not be returned.
import { mapData } from 'json-schema-form'
const schema = {
"sheep": {
"type": "object",
"name": "sheep",
"properties": {
"id": {
"type": "string"
},
"name": {
"type": "string"
},
"hair": {
"type": "string",
"pattern": "cool|daggy"
}
}
}
}
const data = { id: 123, hair: 'cool', pants: true }
mapData(schema, data)
// result
// {
// id: 123,
// name: undefined,
// hair: 'cool'
// }
To create objects that use HTML form input attributes from your schema:
data
is optional. If supplied it will map the value of the field tovalue
.
import { toFormInputs } from 'json-schema-form'
const schema = {
"sheep": {
"type": "object",
"name": "sheep",
"properties": {
"id": {
"type": "string"
},
"name": {
"type": "string"
},
"hair": {
"type": "string",
"pattern": "cool|daggy"
}
}
"required": ["name"]
}
}
const data = { id: 123, name: 'garry' }
toFormInputs(schema, data)
// result
// {
// id: {
// type: 'text',
// value: 123
// },
// name: {
// type: 'text',
// required: true,
// value: 'garry'
// },
// hair: {
// type: 'text',
// pattern: 'cool|daggy'
// }
// }