JSON Schema TypeScript Types
json-schema-typed provides comprehensive TypeScript definitions for JSON Schema, including inline documentation, to facilitate type-safe schema declaration. It currently supports JSON Schema Draft 07, Draft 2019-09, and Draft 2020-12, with the main export defaulting to Draft 2020-12. The library is purely for defining schemas and does not include validation functionality; users must integrate a separate validation library. Version 8.x is the current stable release, which transitioned to a pure ESM package and introduced several breaking changes. Release cadence follows semantic versioning, with major bumps for breaking changes like updating the default draft export.
Common errors
-
Error [ERR_REQUIRE_ESM]: require() of ES Module ...json-schema-typed/dist/node/index.js from ... not supported.
cause Attempting to `require()` the `json-schema-typed` package in a CommonJS environment, but the package is pure ESM since v8.0.0.fixConvert your consuming code to use ES module `import` syntax. Ensure your `package.json` includes `"type": "module"` or that your file has an `.mjs` extension. -
Property 'X' does not exist on type 'JSONSchema<...>'
cause Attempting to use a keyword or property that is either not part of the specified JSON Schema draft, or is defined differently in the selected draft version, or is not allowed on the base `JSONSchema` type.fixReview the JSON Schema specification for the draft you are using (e.g., draft-2020-12, draft-07). Ensure the keyword is valid and applied correctly. Also, consider using the type-specific narrowed interfaces like `JSONSchema.String` if applicable, as they provide stricter typing for certain schema types. -
Cannot find name 'JSONSchema' (or 'Format', etc.)
cause This typically occurs when trying to use an old export name, or when importing from the main package while expecting a specific draft's exports that are now only available via subpath.fixFor v8.0.0+, ensure you are using the correct, renamed exports (e.g., `JSONSchema` for the main type) and importing from the correct path. If you need Draft 07, import from `json-schema-typed/draft-07`.
Warnings
- breaking Version 8.0.0 and above are pure ESM packages. CommonJS `require()` is no longer supported and will lead to errors.
- breaking Many exports were renamed in v8.0.0. Code using older export names will break.
- breaking The `JSONSchema` type changed from an `interface` to a `type` (a mixed union allowing boolean values) in v8.0.0 to better align with the JSON Schema spec. If you were extending the `JSONSchema` interface, your code will break.
- breaking The main package export now points to Draft 2020-12 by default in v8.0.0. Previously it pointed to Draft 07. If your schema relies on Draft 07 behavior, the default import will now provide the wrong draft's types.
- gotcha This library only provides TypeScript definitions for JSON Schema. It does NOT include any data validation capabilities. Using it alone will not validate your data against a schema.
Install
-
npm install json-schema-typed -
yarn add json-schema-typed -
pnpm add json-schema-typed
Imports
- JSONSchema
const { JSONSchema } = require('json-schema-typed')import { type JSONSchema } from 'json-schema-typed' - Format
import { Format } from 'json-schema-typed/draft-07'import { Format } from 'json-schema-typed' - JSONSchema.String
import { type JSONSchema } from 'json-schema-typed'; const stringSchema: JSONSchema.String = { /* ... */ }; - JSONSchema (Draft 07)
import { type JSONSchema } from 'json-schema-typed/draft-07'
Quickstart
import { Format, type JSONSchema } from "json-schema-typed";
// Define a schema for an object with an email property
const userSchema: JSONSchema = {
type: "object",
properties: {
email: {
format: Format.Email, // Uses the predefined Format enum for 'email' format
type: "string",
description: "User's email address in a valid format."
},
age: {
type: "integer",
minimum: 0,
description: "User's age, must be a non-negative integer."
}
},
required: ["email", "age"],
additionalProperties: false
};
// Example of a type-specific narrowed interface for a string schema
const usernameSchema: JSONSchema.String = {
type: "string",
maxLength: 50,
minLength: 3,
description: "Username must be between 3 and 50 characters long."
};
console.log('User Schema:', JSON.stringify(userSchema, null, 2));
console.log('Username Schema:', JSON.stringify(usernameSchema, null, 2));