jsonv-ts
Version:
JSON Schema builder and validator for TypeScript with static type inference, Hono middleware for OpenAPI generation and validation, and MCP server/client implementation. Lightweight, dependency-free, and built on Web Standards.
109 lines (108 loc) • 4.48 kB
TypeScript
import type { OptionallyOptional, Simplify, StaticConstEnum } from "../static";
import type { JSONSchemaDefinition } from "../types";
import type { StandardSchemaV1 } from "@standard-schema/spec";
import { type CoercionOptions } from "../validation/coerce";
import { Resolver } from "../validation/resolver";
import { type ValidationOptions, type ValidationResult } from "../validation/validate";
import { schemaSymbol } from "../shared";
export { schemaSymbol as symbol };
export type TSchemaTemplateOptions = {
withOptional?: boolean;
withExtendedOptional?: boolean;
};
export interface IBaseSchemaOptions {
$id?: string;
$ref?: string;
$schema?: string;
title?: string;
description?: string;
default?: any;
readOnly?: boolean;
writeOnly?: boolean;
$comment?: string;
examples?: any[];
enum?: readonly any[] | any[];
const?: any;
}
export interface ISchemaFn {
validate?: (value: unknown, opts?: ValidationOptions) => ValidationResult;
coerce?: (value: unknown, opts?: CoercionOptions) => unknown;
template?: (value?: unknown, opts?: TSchemaTemplateOptions) => unknown;
}
export interface ISchemaOptions extends IBaseSchemaOptions, Partial<ISchemaFn> {
}
export type StrictOptions<S extends ISchemaOptions, O extends S> = O & {
[K in Exclude<keyof O, keyof S>]: never;
};
export interface IAnySchema extends IBaseSchemaOptions {
[schemaSymbol]: any;
toJSON(): object;
}
export declare class Schema<Options extends ISchemaOptions = ISchemaOptions, Type = unknown, Coerced = Type> implements IBaseSchemaOptions {
["~standard"]: StandardSchemaV1.Props<Type, Type>;
[schemaSymbol]: {
raw?: Options | any;
static: StaticConstEnum<Options, Type>;
coerced: Options extends {
coerce: (...args: any[]) => infer C;
} ? OptionallyOptional<Coerced, C> : OptionallyOptional<Coerced, StaticConstEnum<Options, Coerced>>;
optional: boolean;
overrides?: ISchemaFn;
};
protected _resolver?: Resolver;
readonly type: string | undefined;
$id?: string;
$ref?: string;
$schema?: string;
title?: string;
description?: string;
readOnly?: boolean;
writeOnly?: boolean;
$comment?: string;
examples?: any[];
constructor(o?: Options, overrides?: ISchemaFn);
template(_value?: unknown, opts?: TSchemaTemplateOptions): Type;
validate(value: unknown, opts?: ValidationOptions): ValidationResult;
coerce(value: unknown, opts?: CoercionOptions): any;
optional<T extends Schema>(this: T): T extends Schema<infer O, infer S, infer C> ? Simplify<Pick<T, Exclude<keyof T, keyof Schema>>> & Schema<O, S extends unknown ? T[typeof schemaSymbol]["static"] | undefined : S | undefined, C extends unknown ? T[typeof schemaSymbol]["coerced"] | undefined : C | undefined> : never;
isOptional(): boolean;
getResolver(): Resolver;
children(opts?: WalkOptions): Node[];
/**
* Depth-first walk (pre-order).
* Yields the node together with the JSON-pointer-like path so that
* callers know where they are.
*/
walk({ instancePath, keywordPath, data: _data, maxDepth, ...opts }?: WalkOptions): Generator<Node<Schema>>;
/**
* Makes every schema iterable so you can use `for (const n of schema)`
*/
[Symbol.iterator](opts?: WalkOptions): Generator<Node>;
toJSON(): JSONSchemaDefinition;
}
export type WalkOptions = {
instancePath?: (string | number)[];
keywordPath?: (string | number)[];
includeSelf?: boolean;
data?: any;
maxDepth?: number;
};
export declare class Node<T extends Schema = Schema> {
readonly schema: T;
instancePath: (string | number)[];
keywordPath: (string | number)[];
data?: any;
depth: number;
constructor(schema: T, opts?: WalkOptions);
appendInstancePath(path: (string | number)[]): this;
appendKeywordPath(path: (string | number)[]): this;
setData(data: any): this;
}
export declare function createSchema<O extends ISchemaOptions, Type = unknown, Coerced = Type>(type: string, o?: O, overrides?: ISchemaFn): Schema<O, Type, Coerced> & O;
declare class BooleanSchema<B extends boolean> extends Schema<ISchemaOptions, B extends true ? unknown : never> {
private bool;
constructor(bool: B);
toJSON(): any;
validate(value: unknown, opts?: ValidationOptions): ValidationResult;
}
export declare function booleanSchema<B extends boolean>(bool: B): BooleanSchema<B>;