Registry / testing / json-schema-default

json-schema-default

JSON →
library1.0.2jsnpmunverified

Generate an object based on default values defined in a JSON Schema. This package extracts only the default values from a schema, ignoring 'required' constraints, making it suitable for pre-filling forms or merging with existing data to ensure defaults are present. v1.0.2 targets Node 18+ and ships TypeScript types. It differs from json-schema-empty (which respects 'required' and creates placeholder values) by focusing strictly on explicit defaults. Release cadence: low (single stable release as of 2024). Key alternatives: AJV's assignDefaults, json-schema-empty, json-schema-fill-defaults.

npm install json-schema-default
INSTALL
IMPORT
SIG · JSON-SCHEMA-DEFAUL
J
json-schema-default
testingjavascriptv1.0.2
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

jsonDefault
import { jsonDefault } from 'json-schema-default'
const jsonDefault = require('json-schema-default')
ESM-only; no CommonJS wrapper provided.
default export
import jsonDefault from 'json-schema-default'
import { default } from 'json-schema-default'
The package also exports a default export identical to named export jsonDefault.
types
import type { JsonSchemaDefault } from 'json-schema-default'
TypeScript types are bundled; use type import for type-only usage.

Generates a plain object from JSON Schema defaults; properties without default are omitted.

import { jsonDefault } from 'json-schema-default' const schema = { type: 'object', properties: { name: { type: 'string', default: 'John' }, age: { type: 'number', default: 30 }, email: { type: 'string' } } } const result = jsonDefault(schema) console.log(result) // { name: 'John', age: 30 }
Debug
Known issues
gotchaProperties without a 'default' value are omitted from result. Do not assume all properties appear.
fix
Use json-schema-empty or json-schema-empty-strings if you need placeholders.
affects: >=1.0.0
gotchaOnly top-level 'default' values are used. Nested schemas with defaults in allOf/anyOf/oneOf may not be resolved.
fix
Simplify schema or pre-resolve with a tool like AJV before calling jsonDefault.
affects: >=1.0.0
breakingESM-only; Node <18 will throw ERR_REQUIRE_ESM if using require().
fix
Use dynamic import() or upgrade Node to 18+.
affects: >=1.0.0
gotchaThe function does not validate input; passing an invalid schema may throw unexpected errors.
fix
Validate schema with AJV before passing to jsonDefault.
affects: >=1.0.0
Errors
Common errors & fixes
SyntaxError: Cannot use import statement outside a module
Package is ESM-only but project uses CommonJS.
fix
Add "type": "module" in package.json or use dynamic import().
TypeError: jsonDefault is not a function
Incorrect import: using default import as named import.
fix
Use import { jsonDefault } from 'json-schema-default'.
ReferenceError: require is not defined in ES module scope
Using require() in an ESM context.
fix
Use import statement instead of require().
Upgrade
Version history
1.0.2latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
8 hits · last 30 days
node
8
Resources