lacspace-schema

Infer schema, gen TS & Zod, validate, OpenAPI.

terminal
npx lacspace-schema infer data.json
What it does

Everything, in one command

lacspace-schema is a pure, dependency-free codegen toolkit for the JSON ⇄ Schema ⇄ TypeScript loop. Point it at a real API response to derive a merged draft-07 schema (required vs optional properties, unioned types, enum and format detection), generate idiomatic interface/type declarations from either JSON or a schema (with $ref and cycle support, JSDoc examples, and real TS enums), synthesize a valid example instance from any schema, and catch breaking API changes by diffing two schemas — all locally, with nothing sent anywhere.

Features

What's inside

Infer draft-07 schemas

Merge many JSON/NDJSON samples into one schema with required/optional detection.

JSON → TypeScript

Named root, extracted nested interfaces, unions, optionals, readonly, JSDoc.

Schema → TypeScript

Walks $ref/enum/anyOf/const, cycle-safe, string-literal unions or TS enums.

Example generator

Minimal valid instance honouring required/enum/const/format/min-max.

Schema diff

Flags removed/type-changed/newly-required fields as breaking; exits non-zero in CI.

Format & enum detection

email/date-time/date/uri/uuid/ipv4 formats, configurable enum threshold.

Zero-dependency & local

Node built-ins only; reads file, glob, or stdin; no telemetry.

Validate against a schema

{ valid, errors[] } in the lib; non-zero CLI exit for CI gates.

Zod codegen

Turn JSON or a schema into Zod source with z.infer types.

OpenAPI round-trip

Extract components.schemas/definitions and wrap schemas back into a components block or full doc.

Broader $ref resolution

Local #/definitions, #/$defs and #/components/schemas across every generator.

More formats

Adds time and ipv6 detection (on top of uuid/date-time/date/email/uri/ipv4).

How to use

Copy, paste, done

Real commands and snippets — from a one-liner to the typed library.

Infer a schema from data
terminal
echo '{"id":1,"email":"a@x.com"}' | npx lacspace-schema infer

Draft-07 schema with format:email detected.

JSON → TS types
terminal
npx lacspace-schema types user.json --name User --jsdoc

Extracts nested interfaces and adds example JSDoc.

Schema → TS enums
terminal
npx lacspace-schema types api.schema.json --from schema --enum

Resolves $ref (cycles safe); emits real TS enums.

Example from a schema
terminal
npx lacspace-schema example user.schema.json

A minimal valid fixture instance.

Breaking-change diff in CI
terminal
npx lacspace-schema diff v1.schema.json v2.schema.json

Exits 2 when a change breaks consumers.

Validate a payload (CI gate)
terminal
npx lacspace-schema validate user.schema.json user.json

Prints per-field errors and exits 1 when invalid.

JSON → Zod
terminal
echo '{"id":1,"email":"a@x.com"}' | npx lacspace-schema zod --name User

Emits z.object({...}) + export type User = z.infer<typeof User>.

Extract OpenAPI components
terminal
npx lacspace-schema openapi api.json

Pulls components.schemas (or Swagger 2 definitions) into { name: schema }.

Good for

What people build with it

✓ Bootstrap types from a real API response

✓ Keep TS types in sync with a JSON Schema

✓ Generate fixtures/examples for docs and tests

✓ Guard an API contract in CI via schema diff

Start now

Run lacspace-schema today

Free, open-source, no API keys. It's a CLI and a typed library.

terminal
npx lacspace-schema infer data.json