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

1.0.5 • Public • Published

NPM License NPM Version NPM Types Last Commit

json-schema-empty-strings

Generate empty strings for all JSON-Schema string fields.

There are four different ways in Javsscript / Typescript to represent "no text":

  • ""
  • null
  • undefined
  • absence of the corresponding property

In the context of JSON-Schema absence of the corresponding property is widely used to represent absence of strings. Sometimes you might not want that.

For a given schema jsonEmptyStrings() generates an object containing empty strings for all properties of type: string.

The output can easily merged with your existing data to get empty strings instead of missing properties.

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

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

Iy you have null or undefined values in your data deep-clean them before merging:

import { jsonEmptyStrings } from 'json-schema-empty-strings'
import merge from 'lodash.merge'
import cleanDeep from 'clean-deep'

const finalData = merge({}, jsonEmptyStrings(schema), cleanDeep(inputData, {
    emptyArrays: false,
    emptyObjects: false,
    emptyStrings: false,
  })

See also:

Readme

Keywords

none

Package Sidebar

Install

npm i json-schema-empty-strings

Weekly Downloads

23

Version

1.0.5

License

MIT

Unpacked Size

22.4 kB

Total Files

15

Last publish

Collaborators

  • mdornseif