# json-schema-default

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

Latest version **1.0.2** (published 2024-05-21) · MIT license · 0 weekly downloads

## Install

```sh
npm install json-schema-default
pnpm add json-schema-default
yarn add json-schema-default
bun add json-schema-default
```

## Health

**Score 40/100 (D)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 1.0.2 |
| Published | 2024-05-21 |
| First published | 2021-12-14 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=18 |
| Dependencies | 2 |
| Unpacked size | 11.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 4 |
| Author | Dr. Maximillian Dornseif |
| Maintainers | mdornseif |
| Keywords | JSON, schema, JSON-Schema, Data Generation |

## Links

- npm: https://www.npmjs.com/package/json-schema-default
- Repository: https://github.com/mdornseif/json-schema-default
- Issues: https://github.com/mdornseif/json-schema-default/issues
- npm.io page: https://npm.io/package/json-schema-default

## Dependencies (2)

- [json-schema](https://npm.io/package/json-schema.md) ^0.4.0
- [json-schema-traverse](https://npm.io/package/json-schema-traverse.md) ^1.0.0

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 1.0.2 (latest) — 2024-05-21
- 1.0.1 — 2023-02-27
- 1.0.0 — 2021-12-14

## README

![NPM License](https://img.shields.io/npm/l/json-schema-default)
[![NPM Version](https://img.shields.io/npm/v/json-schema-default)](https://www.npmjs.com/package/json-schema-default)
[![Last Commit](https://img.shields.io/github/last-commit/mdornseif/json-schema-default)](https://github.com/mdornseif/json-schema-default)

# json-schema-default

Generate an Object based on default values inside a [JSON-Schema](https://json-schema.org).

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

[json-schema-empty](https://www.npmjs.com/package/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:

```js
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](https://www.npmjs.com/package/json-schema-empty-strings/):

```js
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

- [AJV](https://ajv.js.org/guide/modifying-data.html#assigning-defaults)
- [json-schema-empty-strings](https://github.com/romeovs/json-schema-empty-strings)
- [json-schema-empty-arrays](https://github.com/mdornseif/json-schema-empty-arrays)
- [empty-schema](https://github.com/slurmulon/empty-schema)
- [json-schema-empty](https://github.com/romeovs/json-schema-empty)
- [json-schema-default](https://www.npmjs.com/package/json-schema-default)
- [json-schema-fill-defaults](https://www.npmjs.com/package/json-schema-fill-defaults)
- [@nodecg/json-schema-defaults](https://www.npmjs.com/package/@nodecg/json-schema-defaults)
- [minimal-schema](https://github.com/tyv/minimal-schema)
- [json-from-default-schema](https://www.npmjs.com/package/json-from-default-schema) (CJS)
- [json-schema-default-instance](https://www.npmjs.com/package/json-schema-default-instance) (CJS)

---
_Source: https://npm.io/package/json-schema-default · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
