Skip to content

Prismatik/json-schema-form

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

json-schema-form

Useful JSON Schema utility functions for forms.

Getting started

npm i [email protected]:Prismatik/json-schema-form.git

Usage

.mapData(schema: Object, data: Object)

To map a data object against your schema use mapData.

  • Fields that do exist in schema but are not supplied in data will return as undefined.
  • Fields that do not exist schema but are supplied in data 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'
// }

.toFormInputs(schema: Object, [data: Object])

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 to value.
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'
//   }
// }

About

Useful JSON Schema utility functions for forms.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published