UNPKG

zon-format

Version:

ZON: The most token-efficient serialization format for LLMs - beats CSV, TOON, JSON, and all competitors

137 lines (136 loc) 4.68 kB
export type ZonIssue = { path: (string | number)[]; message: string; code: 'invalid_type' | 'missing_field' | 'invalid_enum' | 'custom'; }; export type ZonResult<T> = { success: true; data: T; } | { success: false; error: string; issues: ZonIssue[]; }; /** * Abstract base class for ZON schemas. * Defines the contract for parsing validation and prompt generation. */ export declare abstract class ZonSchema<T = any> { protected description?: string; protected exampleValue?: T; /** * Parses and validates the input data against the schema. * * @param data - The data to validate * @param path - Current path in the data structure (for error reporting) * @returns Validation result (success or failure with issues) */ abstract parse(data: any, path?: (string | number)[]): ZonResult<T>; /** * Generates a human-readable description of the schema for LLM prompts. * * @param indent - Indentation level for nested structures * @returns Schema description string */ abstract toPrompt(indent?: number): string; describe(description: string): this; example(value: T): this; optional(): ZonSchema<T | undefined>; nullable(): ZonSchema<T | null>; default(value: T): ZonSchema<T>; refine(validator: (val: T) => boolean, message: string): ZonSchema<T>; } declare class ZonString extends ZonSchema<string> { private minLength?; private maxLength?; private emailValidation; private urlValidation; private regexPattern?; private regexMessage?; private uuidValidation; private datetimeValidation; private dateValidation; private timeValidation; min(length: number): this; max(length: number): this; email(): this; url(): this; regex(pattern: RegExp, message?: string): this; uuid(version?: 'v4' | 'any'): this; datetime(): this; date(): this; time(): this; parse(data: any, path?: (string | number)[]): ZonResult<string>; toPrompt(indent?: number): string; } declare class ZonNumber extends ZonSchema<number> { private minValue?; private maxValue?; private isPositive; private isNegative; private isInteger; min(value: number): this; max(value: number): this; positive(): this; negative(): this; int(): this; parse(data: any, path?: (string | number)[]): ZonResult<number>; toPrompt(indent?: number): string; } declare class ZonBoolean extends ZonSchema<boolean> { parse(data: any, path?: (string | number)[]): ZonResult<boolean>; toPrompt(indent?: number): string; } declare class ZonEnum<T extends string> extends ZonSchema<T> { private values; constructor(values: T[]); parse(data: any, path?: (string | number)[]): ZonResult<T>; toPrompt(indent?: number): string; } declare class ZonArray<T> extends ZonSchema<T[]> { private elementSchema; private minLength?; private maxLength?; constructor(elementSchema: ZonSchema<T>); min(length: number): this; max(length: number): this; parse(data: any, path?: (string | number)[]): ZonResult<T[]>; toPrompt(indent?: number): string; } declare class ZonObject<T extends Record<string, any>> extends ZonSchema<T> { private shape; constructor(shape: { [K in keyof T]: ZonSchema<T[K]>; }); parse(data: any, path?: (string | number)[]): ZonResult<T>; toPrompt(indent?: number): string; } declare class ZonLiteral<T extends string | number | boolean> extends ZonSchema<T> { private value; constructor(value: T); parse(data: any, path?: (string | number)[]): ZonResult<T>; toPrompt(indent?: number): string; } declare class ZonUnion<T extends any[]> extends ZonSchema<T[number]> { private schemas; constructor(schemas: ZonSchema<any>[]); parse(data: any, path?: (string | number)[]): ZonResult<any>; toPrompt(indent?: number): string; } export declare const zon: { string: () => ZonString; number: () => ZonNumber; boolean: () => ZonBoolean; enum: <T extends string>(values: T[]) => ZonEnum<T>; array: <T>(schema: ZonSchema<T>) => ZonArray<T>; object: <T extends Record<string, any>>(shape: { [K in keyof T]: ZonSchema<T[K]>; }) => ZonObject<T>; literal: <T extends string | number | boolean>(value: T) => ZonLiteral<T>; union: <T extends ZonSchema<any>[]>(...schemas: T) => ZonUnion<any[]>; }; /** * Validates a ZON string or decoded object against a schema. * @param input ZON string or decoded object * @param schema ZON Schema */ export declare function validate<T>(input: string | any, schema: ZonSchema<T>): ZonResult<T>; export {};