truefit
API reference

truefit/json-schema

Every export of truefit/json-schema, with its signature and doc comment: standardJson, toJsonSchema, fromJsonSchema, jsonSchemaReader, plus 13 types.

import { standardJson, toJsonSchema, fromJsonSchema, … } from 'truefit/json-schema';

Constants

fromJsonSchema

A schema of this library from a JSON Schema document (2020-12 or draft-07) or an OpenAPI 3.0 Schema Object, and every place where it may give another verdict than the document. A document that is not JSON Schema is refused with a TypeError and a fix: line.

const fromJsonSchema: (document: unknown, options?: ImportOptions) => Imported;

jsonSchemaReader

A reader for many schemas against one document, such as every payload of an OpenAPI document against its components: each $ref is read once and shared by every schema that names it, where fromJsonSchema on each would read the whole graph behind it again. The options and the refusals are fromJsonSchema's.

const jsonSchemaReader: (document: unknown, options?: ImportOptions) => JsonSchemaReader;

standardJson

The schema as a Standard JSON Schema (standardschema.dev): a library that asks a schema for JSON Schema, such as a model provider's SDK or an MCP server, calls ~standard.jsonSchema.input({ target }) and gets toJsonSchema's document. Kept beside a ~standard the schema already has, so json(standard(Order)) is both a Standard Schema and a Standard JSON Schema.

const standardJson: <T, O>(schema: Schema<T, O>) => StandardJson<T, O>;

toJsonSchema

The schema as a JSON Schema document, and every place where the document says less than the schema: findings is empty exactly when a JSON value passes the document if and only if check accepts it.

const toJsonSchema: <T>(schema: Schema<T, unknown>, options?: ExportOptions) => Exported;

Interfaces

Exported

interface Exported {
    readonly schema: JsonSchema;
    readonly findings: Finding[];
}

ExportOptions

interface ExportOptions {
    /** The dialect to write. They differ in how a tuple is written and where shared schemas are kept. */
    readonly draft?: Draft;
}

Finding

One place where the document says less than the schema.

interface Finding {
    /** JSON Pointer into the document, to the schema that stands for the node; `''` is the root. */
    readonly path: string;
    /**
     * What was lost: `not-json`, `custom`, `refine`, `transform`, `format`, `pattern`, `coerce`,
     * `strip`, `multipleOf`, `default`, `enum`, `tag`, `meta`, `pipe`, `preprocess`, `contains`,
     * `catch`, `codec`.
     */
    readonly keyword: string;
    readonly message: string;
}

Imported

interface Imported {
    readonly schema: Schema<unknown>;
    readonly findings: ImportFinding[];
}

ImportFinding

One place where the schema's verdict may differ from the document's.

interface ImportFinding {
    /** JSON Pointer into the document, to the schema the keyword is in. */
    readonly path: string;
    /** The keyword that is not read, or not read as the document means it. */
    readonly keyword: string;
    readonly message: string;
}

ImportOptions

interface ImportOptions {
    /**
     * The dialect the document is written in. Read from its `$schema` when it is not given, and
     * 2020-12 when neither says. `openapi-3.0` is an OpenAPI 3.0 Schema Object: `nullable` adds `null`,
     * `exclusiveMinimum` and `exclusiveMaximum` are booleans, and a `$ref` silences its siblings.
     */
    readonly draft?: ImportDraft;
    /** Formats by the name the document gives them, beside and over the ones this library reads. */
    readonly formats?: Readonly<Record<string, Format>>;
}

JsonSchemaReader

Schemas read against one document: the $refs they name point into it, and are shared.

interface JsonSchemaReader {
    /**
     * The schema of this library for a schema whose `$ref`s point into the document: the document
     * itself, a part of it, or a schema made beside it. `path` is where findings in it point.
     */
    readonly read: (schema: unknown, path?: string) => Schema<unknown>;
    /** Every finding of every schema read so far; a reference's, once, at the reference. */
    readonly findings: ImportFinding[];
}

StandardJsonConverter

interface StandardJsonConverter {
    readonly input: (options: StandardJsonOptions) => Record<string, unknown>;
    readonly output: (options: StandardJsonOptions) => Record<string, unknown>;
}

StandardJsonOptions

interface StandardJsonOptions {
    /** `draft-2020-12` or `draft-07`; any other target is refused, as the specification asks. */
    readonly target: string;
    /** `findings`: a function handed the findings of each document written, which the interface has no place for. */
    readonly libraryOptions?: Readonly<Record<string, unknown>> | undefined;
}

Types

Draft

The JSON Schema writer (spec.md R2, R10, R13): a node as a JSON Schema document, and beside it every place where the document says less than the node does. Nothing is dropped silently (intent.md Z10): a rule or a map of the caller's, a kind JSON has no value for, a behaviour only parse has, each is a finding with a pointer to where in the document it would have stood.

type Draft = '2020-12' | 'draft-07';

ImportDraft

The JSON Schema reader (spec.md R13): a JSON Schema document as a schema of this library, and beside it every place where the schema's verdict may differ from the document's. The contract is the writer's turned round: findings is empty exactly when check accepts a JSON value if and only if the document does.

type ImportDraft = '2020-12' | 'draft-07' | 'openapi-3.0';

JsonSchema

A JSON Schema, as plain data.

type JsonSchema = Readonly<Record<string, unknown>>;

StandardJson

A schema of this library that is also a Standard JSON Schema: ~standard.jsonSchema.input(...) and output(...) write it.

type StandardJson<T, O> = Schema<T, O> & {
    readonly '~standard': {
        readonly version: 1;
        readonly vendor: string;
        readonly jsonSchema: StandardJsonConverter;
        readonly types?: {
            readonly input: T;
            readonly output: Out<T, O>;
        } | undefined;
    };
};

On this page