npm install @ferrow/json-schema-from-sampleInfer a draft-07 JSON Schema subset from sample JSON data. Strict TypeScript, zero runtime dependencies.
Hand-writing JSON Schema for an API response or config shape is tedious and drifts from reality. This library infers one from real samples — the more samples you provide, the more it can safely say about nullability, required fields, and enums, and it never claims more than the samples support.
import { infer } from "json-schema-from-sample";
const schema = infer([
{ id: "8f14e45f-ceea-467e-b3a3-7a3d3f2f1a10", email: "a@example.com", role: "admin" },
{ id: "9a14e45f-ceea-467e-b3a3-7a3d3f2f1a11", email: "b@example.com", role: "member" },
// ...
]);Merges observations across all samples into a single schema.
options.tupleLengthConstraints(defaultfalse) — emitminItems/maxItemswhen an array field's observed length never varies. Off by default: a few samples can't prove a length rule.options.enumMaxDistinct(default5) — max distinct string values for enum detection.options.enumMinSamples(default10) — min sample count before enum detection runs.
- Types: merged as a union across samples (e.g.
["null", "string"]); integer + float observed together widen to"number". - Nullable: a field observed as
nullin some samples and a value in others gets"null"added to itstype. - Required: a property present in every sample that reached that object is marked required.
- Enum: string fields with ≤
enumMaxDistinctdistinct values across ≥enumMinSamplessamples. - Format:
date-time,email,uuid,uridetected via regex when all observed strings at that field match. - Arrays: item schema is inferred by merging every element across every array observed at that path.
- Samples-only inference — it cannot prove constraints samples don't demonstrate (numeric ranges,
string length bounds, exact tuple shapes).
tupleLengthConstraintsis opt-in for that reason. - Format detection requires unanimous agreement across all observed strings at a field; mixed-format
fields get no
format. - No
$ref,oneOf/anyOf, or schema composition beyond simple type unions — this targets the common 80% case, not the full draft-07 spec.
Part of the ferrow-toolkit collection · Sponsored by Ferrow