Skip to content

Latest commit

Β 

History

986 Commits

Folders and files

NameName
Last commit message
Last commit date
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Utilize Schema

Convert JSON Schema to Zod, Effect Schema, or plain TypeScript. One parser resolves local, external and cyclic $ref links into standalone schemas and types for every dialect from draft-04 to 2020-12.

Check Codebase health npm version License

Table of contents

✨ Features

  • Three targets, one pipeline. @utilize/zod, @utilize/effect and @utilize/typescript share the same parser, linker and naming rules, so they agree on what a schema means.
  • $ref links resolve. Local, external and cyclic, including $id and $anchor. External documents come from disk, HTTP, or memory with externalSchemas.
  • Every definition is standalone. $defs and definitions each get their own schema or type. Zod also orders them by dependency.
  • Dialect-aware. Draft-04/06/07 are read natively, 2019-09 and 2020-12 are normalized. $schema is resolved per schema resource and $ref siblings are applied only where the dialect defines them. The per-dialect rules are in docs/dialects.md.
  • Proven on the official suite. Draft-07 scores 304/304 cases for Zod, 304/304 for Effect Schema and 192/285 for TypeScript; five dialects are measured. See supported features.
  • Types without runtime. @utilize/typescript emits interfaces and aliases only; the output imports nothing.

⚑ Quick start

pnpm add zod @utilize/json-schema
pnpm add -D @utilize/zod
utilize-zod user.json user.zod.ts

Generated schemas import structural format validators from @utilize/json-schema/formats, so @utilize/json-schema is a runtime dependency of the app, not only a generator dependency.

user.json:

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "properties": {
    "id": {
      "type": "string",
      "format": "date"
    }
  },
  "required": ["id"],
  "additionalProperties": false
}

user.zod.ts (banner omitted):

import { z } from 'zod';

export const User = z.strictObject({
  id: z.iso.date().meta({ format: 'date' }),
});
export type User = z.infer<typeof User>;

The date format keeps this example short. Formats that need structural validation, such as email, hostname and uri, are imported from @utilize/json-schema/formats as one validator per format, shared by every schema that uses it.

The same three steps work for the other targets: @utilize/effect with utilize-effect, and @utilize/typescript with utilize-typescript (no runtime dependency). See Usage for the API and CLI; each package README lists the options.


πŸ“¦ Packages

  • @utilize/json-schema β€” Resolves $ref, links nodes with metadata, provides the CLI runner and compile helpers the three generators share, exports the runtime format validators generated schemas import, and the optional Ajv adapter at ./validate/ajv.

    npm version weekly downloads coverage

  • @utilize/zod β€” Emits Zod schemas and z.infer types.

    npm version weekly downloads coverage

  • @utilize/effect β€” Emits Effect Schema codecs and types, with every $ref going through Schema.suspend.

    npm version weekly downloads coverage

  • @utilize/typescript β€” Emits interfaces and type aliases. No runtime dependency.

    npm version weekly downloads coverage

The badges are committed to .github/badges/ and checked in CI. Coverage is the line coverage of each package's src, including the JSON Schema Test Suite and the CLI entry points (coverage badges). Conformance is the share of compatible JSON Schema Test Suite cases where every expected result matches, with known gaps left in the denominator (conformance badges). The json-schema-to-typescript row measures the pinned competitor through the same type-level runner, for a like-for-like comparison (methodology). Regenerate both with pnpm badge:coverage and pnpm badge:suite.

Conformance badges for all five dialects
Project draft-04 draft-06 draft-07 2019-09 2020-12
@utilize/zod draft-04 conformance draft-06 conformance draft-07 conformance 2019-09 conformance 2020-12 conformance
@utilize/effect draft-04 conformance draft-06 conformance draft-07 conformance 2019-09 conformance 2020-12 conformance
@utilize/typescript draft-04 conformance draft-06 conformance draft-07 conformance 2019-09 conformance 2020-12 conformance
json-schema-to-typescript draft-04 conformance draft-06 conformance draft-07 conformance 2019-09 conformance 2020-12 conformance

πŸ“– Usage

Programmatic API

import { compile as compileZod } from '@utilize/zod';

await compileZod({
  input: './schema.json',
  output: './schema.zod.generated.ts',
});

@utilize/effect and @utilize/typescript export the same compile. All three accept externalSchemas to serve a document from memory instead of disk or the network; the runtime generators also take formatMode, and Zod maps individual formats with formatMap. Each package README documents the full options.

CLI

utilize-zod schema.json schema.generated.ts
utilize-effect --input 'schemas/**/*.json' --output generated/
utilize-typescript --input 'schemas/**/*.yaml' --output generated/
cat schema.yaml | utilize-zod > schema.generated.ts

The CLIs read JSON and YAML, take a file, directory or glob, and write a file, directory or stdout. Generated files start with a "do not edit" banner (--no-banner omits it). The runtime CLIs also accept --format-mode, --dialect, --fallback-dialect and --keyword-policy; see docs/dialects.md for the dialect flags.

--validate checks every resolved document against the meta-schema of the dialect the parser resolves, and stops before writing anything when a schema is invalid. It needs the optional ajv package; pass --validator <module> to use another validator instead. Draft-04 schemas also need ajv-draft-04. See docs/dialects.md.


πŸ§ͺ Playground

Playground with person.json split across referenced files, the generated Zod schema on the right, and below them a test-data editor next to the generated validator's decoded value

The playground is a browser app for editing multiple JSON Schema files with references. Switch between the three generators, change the keyword policy, the format mode or the TypeScript pragmatic inference, and watch the generated code update live. Start from one of the built-in examples: each comes with labelled valid and invalid test documents, so you can compare the generated code with what the validator does on the same screen. A test-data editor below the schemas runs the selected generator's validator and shows the decoded value, defaults included, or every issue with its JSON path; the TypeScript target emits types only, so the pane says there is no runtime validator.

Watch the demo (20 s)

Dropping person.json and address.json onto the playground, choosing the Replace target, dropping a JSON document into the test data, then switching the Format mode to annotate and the generator to Effect Schema while the code and the validator result update


βœ… Supported features

Both runtime generators implement the validation keywords, including $ref/$defs, allOf/anyOf/oneOf, conditionals, patternProperties and unevaluated* (best effort). The exceptions are annotations a target cannot enforce, such as readOnly in Effect Schema or discriminator; the full table is in docs/keywords.md.

The TypeScript generator has no runtime, so validation keywords such as minLength, pattern or uniqueItems have no type-level equivalent and are ignored. Use Zod or Effect Schema when you need runtime validation.

Official-suite numbers, with every measured dialect pinned in the package baselines:

  • Zod: 1,514 of 1,609 cases; draft-04, draft-06 and draft-07 are complete.
  • Effect Schema: 1,510 of 1,609 cases; draft-04, draft-06 and draft-07 are complete.
  • TypeScript: 192 of 285 draft-07 cases at the type level (795 of 972 instances).

The remaining runtime gaps are dynamic/recursive references, full unevaluated* annotation propagation, and a few vocabulary/annotation cases.


πŸ“š Learn more


πŸ›  Contributing

  • Clone the repo, run pnpm install
  • Run tests: pnpm test
  • Run the full check: pnpm verify
  • Rebuild the hosted reports locally: pnpm build:dev && pnpm --filter playground build && pnpm reports

Tooling: pnpm workspaces, tsdown for builds, Vitest for tests, oxlint and oxfmt for lint and format, Changesets for releases, and fallow for unused code, duplication, complexity, dependency and architecture checks (pnpm fallow, pnpm fallow:audit).

The full guide, including changesets and release steps, is in CONTRIBUTING.md and RELEASING.md. Participation is covered by the Code of Conduct.


πŸ“„ License

MIT. See LICENSE.


πŸ™ Credits

Inspired by json-schema-to-typescript and the Zod community. @utilize/typescript is a drop-in alternative to it, built on the same parser as the runtime generators.

Releases

Used by

Contributors

Languages