UNPKG

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.

52 lines (51 loc) 2.82 kB
import { Schema, symbol, type ISchemaOptions, type StrictOptions, Node, type WalkOptions } from "../schema/schema"; import type { Merge, OptionalUndefined, Simplify, Static, StaticCoerced, Writeable } from "../static"; export type TProperties = { [key: string]: Schema; }; export type TProperties2<P extends object> = { [K in keyof P]: P[K] extends Schema ? P[K] : never; }; export type ObjectStatic<T extends TProperties> = Simplify<OptionalUndefined<Writeable<{ [K in keyof T]: Static<T[K]>; }>>>; export type ObjectCoerced<T extends TProperties> = Simplify<OptionalUndefined<Writeable<{ [K in keyof T]: StaticCoerced<T[K]>; }>>>; export type ObjectDefaults<T extends TProperties> = Simplify<OptionalUndefined<Writeable<{ [K in keyof T]: T[K] extends { default: infer D; } ? D : T[K][typeof symbol]["static"]; }>>>; export interface IObjectOptions extends ISchemaOptions { $defs?: Record<string, Schema>; patternProperties?: Record<string, Schema>; additionalProperties?: Schema | false; minProperties?: number; maxProperties?: number; propertyNames?: Schema; } export declare class ObjectSchema<const P extends TProperties = TProperties, const O extends IObjectOptions = IObjectOptions> extends Schema<O, O extends { additionalProperties: false; } ? ObjectStatic<P> : Simplify<Merge<ObjectStatic<P> & { [key: string]: unknown; }>>, O extends { additionalProperties: false; } ? ObjectCoerced<P> : Simplify<Merge<ObjectCoerced<P> & { [key: string]: unknown; }>>> { readonly type = "object"; properties: P; required: string[] | undefined; constructor(properties: P, o?: O); strict(): ObjectSchema<P, Merge<O & { additionalProperties: false; }>>; partial(): ObjectSchema<{ [Key in keyof P]: P[Key] extends Schema<infer O_1, infer T, infer C> ? Schema<O_1, P[Key][typeof symbol]["static"] | undefined, P[Key][typeof symbol]["coerced"] | undefined> : never; }, O>; children(opts?: WalkOptions): Node[]; } export declare const object: <const P extends TProperties2<P>, const O extends IObjectOptions>(properties: P, options?: StrictOptions<IObjectOptions, O>) => ObjectSchema<P, O> & O; export declare const strictObject: <const P extends TProperties2<P>, const O extends IObjectOptions>(properties: P, options?: StrictOptions<IObjectOptions, O>) => ObjectSchema<P, Merge<O & { additionalProperties: false; }>>; export declare const partialObject: <const P extends TProperties2<P>, const O extends IObjectOptions>(properties: P, options?: StrictOptions<IObjectOptions, O>) => ObjectSchema<{ [Key in keyof P]: P[Key] extends Schema<infer O_1 extends ISchemaOptions, infer T, infer C> ? Schema<O_1, P[Key][typeof symbol]["static"] | undefined, P[Key][typeof symbol]["coerced"] | undefined> : never; }, O>;