Registry / storage / to-json-schema

to-json-schema

JSON →
library0.2.5jsnpmunverified

Converts JavaScript objects (and other types) to corresponding JSON Schema (latest v0.2.5). Released irregularly with a focus on simplicity, it supports multiple array modes (all, first, uniform, tuple), custom post-processing, and inferred string formats. Differentiator: lightweight, zero-dependency alternative to larger schema generators.

npm install to-json-schema
INSTALL
IMPORT
SIG · TO-JSON-SCHEMA
T
to-json-schema
storagejavascriptv0.2.5
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.

toJsonSchema
import toJsonSchema from 'to-json-schema'
const toJsonSchema = require('to-json-schema');
Package has no default export in ESM; use default import. CommonJS require works but is not compatible with ESM projects.
toJsonSchema
const toJsonSchema = require('to-json-schema')
import { toJsonSchema } from 'to-json-schema'
Named import does not exist; use default import or require.
toJsonSchema
import { default as toJsonSchema } from 'to-json-schema'
import * as toJsonSchema from 'to-json-schema'
Namespace import also works, but default import is preferred.

Demonstrates converting a JavaScript object to JSON Schema with inferred string format and array items.

const toJsonSchema = require('to-json-schema'); const objToBeConverted = { name: 'David', rank: 7, born: '1990-04-05T15:09:56.704Z', luckyNumbers: [7, 77, 5] }; const schema = toJsonSchema(objToBeConverted); console.log(JSON.stringify(schema, null, 2)); // { // "type": "object", // "properties": { // "name": { "type": "string" }, // "rank": { "type": "integer" }, // "born": { "type": "string", "format": "date-time" }, // "luckyNumbers": { "type": "array", "items": { "type": "integer" } } // } // }
Debug
Known issues
gotchaArray mode 'all' may omit items type entirely when mixed types are encountered, resulting in schema with no items constraint.
fix
Use arrays.mode: 'first' or 'uniform' for stricter schemas.
affects: >=0.1.0
gotchaOptions object is mutated internally; avoid reusing the same options object across multiple calls.
fix
Create a fresh options object for each toJsonSchema call.
affects: >=0.1.0
gotchaDate objects are always converted to strings with format 'date-time', even if you prefer ISO date only or custom format.
fix
Override via postProcessFnc to change format or type.
affects: >=0.1.0
gotchaThe package does not support generating JSON Schema Draft 04/06/07 specific fields like $schema or definitions.
fix
Manually add $schema or definitions after conversion using postProcessFnc.
affects: >=0.1.0
gotchaCalling toJsonSchema with a primitive (e.g., number, string) will generate a schema without 'required' by default.
fix
Set options.required = true to add 'required' to primitive schemas.
affects: >=0.1.0
gotchaThe 'uniform' array mode does not merge properties from multiple objects; it only checks consistency of types across items.
fix
Use 'all' mode for most detailed schema with object merging, or 'tuple' for fixed-length arrays.
affects: >=0.1.0
Errors
Common errors & fixes
TypeError: toJsonSchema is not a function
Incorrect import style (e.g., named import or namespace import in ESM)
fix
Use default import: import toJsonSchema from 'to-json-schema';
Cannot read property 'mode' of undefined
Options object not properly structured when using arrays.mode
fix
Pass options as { arrays: { mode: 'all' } } not { mode: 'all' }.
Schema does not include 'required' property
options.required not set to true
fix
Pass { required: true } to add required on all properties.
Uncaught TypeError: Cannot convert undefined or null to object
Passing null or undefined as value
fix
Wrap call: if (value != null) { schema = toJsonSchema(value); }
Schema has no 'items' for arrays
Array contains mixed types and arrays.mode is 'all' (default)
fix
Use arrays.mode: 'first' or 'uniform' to avoid omitted items.
Upgrade
Version history
0.2.5latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
19 hits · last 30 days
node
16
Resources
to-json-schema — npm install to-json-schema · libregistry