JSON Schema TypeScript Types

8.0.2 · active · verified Sun Apr 19

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

Warnings

Install

Imports

Quickstart

Demonstrates defining a JSON Schema using `JSONSchema` type and `Format` enum, including a type-specific narrowed interface.

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));

view raw JSON →