1.0.1 • Published 1 year ago

json-schema-default v1.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

NPM License NPM Version NPM Types 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