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

1.0.1 • Public • Published

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

Package Sidebar

Install

npm i json-schema-default

Weekly Downloads

97

Version

1.0.1

License

MIT

Unpacked Size

21.9 kB

Total Files

16

Last publish

Collaborators

  • mdornseif