json-schema-default
TypeScript icon, indicating that this package has built-in type declarations

1.0.2 • Public • Published

NPM License NPM Version Last Commit

json-schema-default

Generate an Object based on default values inside a JSON-Schema.

This is ideal to get a template, e.g. with form placehholders based on a schema.

json-schema-empty is different in that it closely observes the required properties and only adds them, inventing new values along the way should there be no default values.

json-schema-default on the other hand copies default values not caring if they are required or not. The resulting object can be used stand alone or merged with existing data to make sure, all defaults are filled:

import { jsonDefault } from 'json-schema-default'
import merge from 'lodash.merge'

const finalData = merge({}, jsonDefault(schema), inputData)

If you want to ensure that string properties without a default get represented eith "" instead of null use json-schema-empty-strings:

import { jsonEmptyStrings } from 'json-schema-empty-strings'
import { jsonDefault } from 'json-schema-default'
import merge from 'lodash.merge'

const finalData = merge({}, jsonEmptyStrings(schema), jsonDefault(schema), inputData)

See also

/json-schema-default/

    Package Sidebar

    Install

    npm i json-schema-default

    Weekly Downloads

    692

    Version

    1.0.2

    License

    MIT

    Unpacked Size

    11.6 kB

    Total Files

    6

    Last publish

    Collaborators

    • mdornseif