export class ValidationError { /** * Reverse errors * @type {Array<{ path: string?, expected: string, has: string, message: string? }>} */ _rerrs: Array<{ path: string | null; expected: string; has: string; message: string | null; }>; /** * @param {string?} path * @param {string} expected * @param {string} has * @param {string?} message */ extend(path: string | null, expected: string, has: string, message?: string | null): void; toString(): string; } /** * @template T * @implements {equalityTraits.EqualityTrait} */ export class Schema implements equalityTraits.EqualityTrait { /** * If true, the more things are added to the shape the more objects this schema will accept (e.g. * union). By default, the more objects are added, the the fewer objects this schema will accept. * @protected */ protected static _dilutes: boolean; /** * @param {Schema} other */ extends(other: Schema): boolean; /** * Overwrite this when necessary. By default, we only check the `shape` property which every shape * should have. * @param {Schema} other */ equals(other: Schema): boolean; /** * Use `schema.validate(obj)` with a typed parameter that is already of typed to be an instance of * Schema. Validate will check the structure of the parameter and return true iff the instance * really is an instance of Schema. * * @param {T} o * @return {boolean} */ validate(o: T): boolean; /** * Similar to validate, but this method accepts untyped parameters. * * @param {any} _o * @param {ValidationError} [_err] * @return {_o is T} */ check(_o: any, _err?: ValidationError): _o is T; /** * @type {Schema} */ get nullable(): Schema; /** * @type {$Optional>} */ get optional(): $Optional>; /** * Cast a variable to a specific type. Returns the casted value, or throws an exception otherwise. * Use this if you know that the type is of a specific type and you just want to convince the type * system. * * **Do not rely on these error messages!** * Performs an assertion check only if not in a production environment. * * @template OO * @param {OO} o * @return {Extract extends never ? T : (OO extends Array ? T : Extract)} */ cast(o: OO): Extract extends never ? T : (OO extends Array ? T : Extract); /** * EXPECTO PATRONUM!! 🪄 * This function protects against type errors. Though it may not work in the real world. * * "After all this time?" * "Always." - Snape, talking about type safety * * Ensures that a variable is a a specific type. Returns the value, or throws an exception if the assertion check failed. * Use this if you know that the type is of a specific type and you just want to convince the type * system. * * Can be useful when defining lambdas: `s.lambda(s.$number, s.$void).expect((n) => n + 1)` * * **Do not rely on these error messages!** * Performs an assertion check if not in a production environment. * * @param {T} o * @return {o extends T ? T : never} */ expect(o: T): T extends T ? T : never; [schemaSymbol](): boolean; /** * @param {object} other */ [equalityTraits.EqualityTraitSymbol](other: object): boolean; } /** * @template {(new (...args:any[]) => any) | ((...args:any[]) => any)} Constr * @typedef {Constr extends ((...args:any[]) => infer T) ? T : (Constr extends (new (...args:any[]) => any) ? InstanceType : never)} Instance */ /** * @template {(new (...args:any[]) => any) | ((...args:any[]) => any)} C * @extends {Schema>} */ export class $ConstructedBy any) | ((...args: any[]) => any)> extends Schema> { /** * @param {C} c * @param {((o:Instance)=>boolean)|null} check */ constructor(c: C, check: ((o: Instance) => boolean) | null); shape: C; _c: ((o: Instance) => boolean) | null; /** * @param {any} o * @param {ValidationError} [err] * @return {o is C extends ((...args:any[]) => infer T) ? T : (C extends (new (...args:any[]) => any) ? InstanceType : never)} o */ check(o: any, err?: ValidationError): o is C extends ((...args: any[]) => infer T) ? T : (C extends (new (...args: any[]) => any) ? InstanceType : never); } export function $constructedBy any) | ((...args: any[]) => any)>(c: C, check?: ((o: Instance) => boolean) | null): CastToSchema<$ConstructedBy>; export const $$constructedBy: Schema<$ConstructedBy<(new (...args: any[]) => any) | ((...args: any[]) => any)>>; /** * Check custom properties on any object. You may want to overwrite the generated Schema. * * @extends {Schema} */ export class $Custom extends Schema { /** * @param {(o:any) => boolean} check */ constructor(check: (o: any) => boolean); /** * @type {(o:any) => boolean} */ shape: (o: any) => boolean; /** * @param {any} o * @param {ValidationError} err * @return {o is any} */ check(o: any, err: ValidationError): o is any; } export function $custom(check: (o: any) => boolean): Schema; export const $$custom: Schema<$Custom>; /** * @template {Primitive} T * @extends {Schema} */ export class $Literal extends Schema { /** * @param {Array} literals */ constructor(literals: Array); shape: T[]; } export function $literal(...literals: T): CastToSchema<$Literal>; export const $$literal: Schema<$Literal>; /** * @template {Array>} T * @extends {Schema>} */ export class $StringTemplate>> extends Schema> { /** * @param {T} shape */ constructor(shape: T); shape: T; _r: RegExp; } export function $stringTemplate>>(...literals: T): CastToSchema<$StringTemplate>; export const $$stringTemplate: Schema<$StringTemplate<(string | Schema)[]>>; export const $$optional: Schema<$Optional>>; /** * @type {Schema} */ export const $never: Schema; export const $$never: Schema<$Never>; /** * @template {{ [key: string|symbol|number]: Schema }} S * @typedef {{ [Key in keyof S as S[Key] extends $Optional> ? Key : never]?: S[Key] extends $Optional> ? Type : never } & { [Key in keyof S as S[Key] extends $Optional> ? never : Key]: S[Key] extends Schema ? Type : never }} $ObjectToType */ /** * @template {{[key:string|symbol|number]: Schema}} S * @extends {Schema<$ObjectToType>} */ export class $Object; }> extends Schema<$ObjectToType> { /** * @param {S} shape * @param {boolean} partial */ constructor(shape: S, partial?: boolean); /** * @type {S} */ shape: S; _isPartial: boolean; /** * @type {Schema>>} */ get partial(): Schema>>; /** * @param {any} o * @param {ValidationError} err * @return {o is $ObjectToType} */ check(o: any, err: ValidationError): o is $ObjectToType; } export function $object; }>(def: S): _ObjectDefToSchema extends Schema ? Schema<{ [K in keyof S_1]: S_1[K]; }> : never; export const $$object: Schema<$Object<{ [key: string]: Schema; [key: number]: Schema; [key: symbol]: Schema; }>>; /** * @type {Schema<{[key:string]: any}>} */ export const $objectAny: Schema<{ [key: string]: any; }>; /** * @template {Schema} Keys * @template {Schema} Values * @extends {Schema<{ [key in Unwrap]: Unwrap }>} */ export class $Record, Values extends Schema> extends Schema<{ [key in Unwrap]: Unwrap; }> { /** * @param {Keys} keys * @param {Values} values */ constructor(keys: Keys, values: Values); shape: { keys: Keys; values: Values; }; /** * @param {any} o * @param {ValidationError} err * @return {o is { [key in Unwrap]: Unwrap }} */ check(o: any, err: ValidationError): o is { [key_1 in Unwrap]: Unwrap; }; } export function $record, Values extends Schema>(keys: Keys, values: Values): CastToSchema<$Record>; export const $$record: Schema<$Record, Schema>>; /** * @template {Schema[]} S * @extends {Schema<{ [Key in keyof S]: S[Key] extends Schema ? Type : never }>} */ export class $Tuple[]> extends Schema<{ [Key in keyof S]: S[Key] extends Schema ? Type : never; }> { /** * @param {S} shape */ constructor(shape: S); shape: S; /** * @param {any} o * @param {ValidationError} err * @return {o is { [K in keyof S]: S[K] extends Schema ? Type : never }} */ check(o: any, err: ValidationError): o is { [K in keyof S]: S[K] extends Schema ? Type : never; }; } export function $tuple>>(...def: T): CastToSchema<$Tuple>; export const $$tuple: Schema<$Tuple[]>>; /** * @template {Schema} S * @extends {Schema ? T : never>>} */ export class $Array> extends Schema<(S extends Schema ? T : never)[]> { /** * @param {Array} v */ constructor(v: Array); /** * @type {Schema ? T : never>} */ shape: Schema ? T_1 : never>; /** * @param {any} o * @param {ValidationError} [err] * @return {o is Array ? T : never>} o */ check(o: any, err?: ValidationError): o is Array ? T_1 : never>; } export function $array>>(...def: T): Schema> ? S : never>>; export const $$array: Schema<$Array>>; /** * @type {Schema>} */ export const $arrayAny: Schema>; /** * @template T * @extends {Schema} */ export class $InstanceOf extends Schema { /** * @param {new (...args:any) => T} constructor * @param {((o:T) => boolean)|null} check */ constructor(constructor: new (...args: any) => T, check: ((o: T) => boolean) | null); shape: new (...args: any) => T; _c: ((o: T) => boolean) | null; /** * @param {any} o * @param {ValidationError} err * @return {o is T} */ check(o: any, err: ValidationError): o is T; } export function $instanceOf(c: new (...args: any) => T, check?: ((o: T) => boolean) | null): Schema; export const $$instanceOf: Schema<$InstanceOf>; export const $$schema: Schema>; /** * @template {Schema[]} Args * @typedef {(...args:UnwrapArray>)=>Unwrap>} _LArgsToLambdaDef */ /** * @template {Array>} Args * @extends {Schema<_LArgsToLambdaDef>} */ export class $Lambda>> extends Schema<_LArgsToLambdaDef> { /** * @param {Args} args */ constructor(args: Args); len: number; args: Schema; res: Schema; /** * @param {any} f * @param {ValidationError} err * @return {f is _LArgsToLambdaDef} */ check(f: any, err: ValidationError): f is _LArgsToLambdaDef; } export function $lambda[]>(...args: Args): Schema<(...args: UnwrapArray>) => Unwrap>>; export const $$lambda: Schema<$Lambda[]>>; /** * @type {Schema} */ export const $function: Schema; /** * @template {Array>} T * @extends {Schema>>} */ export class $Intersection>> extends Schema>> { /** * @param {T} v */ constructor(v: T); /** * @type {T} */ shape: T; } export function $intersect[]>(...def: T): CastToSchema<$Intersection>; export const $$intersect: Schema<$Intersection[]>>; /** * @template S * @extends {Schema} */ export class $Union extends Schema { /** * @param {Array>} v */ constructor(v: Array>); shape: Schema[]; } export function $union>(...schemas: T): CastToSchema<$Union>>>; export const $$union: Schema<$Union>; /** * @type {Schema} */ export const $any: Schema; export const $$any: Schema>; /** * @type {Schema} */ export const $bigint: Schema; export const $$bigint: Schema>; /** * @type {Schema} */ export const $symbol: Schema; export const $$symbol: Schema>; /** * @type {Schema} */ export const $number: Schema; export const $$number: Schema>; /** * @type {Schema} */ export const $string: Schema; export const $$string: Schema>; /** * @type {Schema} */ export const $boolean: Schema; export const $$boolean: Schema>; /** * @type {Schema} */ export const $undefined: Schema; export const $$undefined: Schema>; /** * @type {Schema} */ export const $void: Schema; export const $$void: Schema>; export const $null: Schema; export const $$null: Schema>; export const $uint8Array: Schema>; export const $$uint8Array: Schema>; /** * @type {Schema} */ export const $primitive: Schema; /** * @typedef {JSON[]} JSONArray */ /** * @typedef {Primitive|JSONArray|{ [key:string]:JSON }} JSON */ /** * @type {Schema} */ export const $json: Schema; export function $(o: IN): ReadSchema; /** * Assert that a variable is of this specific type. * The assertion check is only performed in non-production environments. * * @type {(o:any,schema:Schema) => asserts o is T} */ export const assert: (o: any, schema: Schema) => asserts o is T; /** * @template In * @template Out * @typedef {{ if: Schema, h: (o:In,state?:any)=>Out }} Pattern */ /** * @template {Pattern} P * @template In * @typedef {ReturnType>['h']>} PatternMatchResult */ /** * @todo move this to separate library * @template {any} [State=undefined] * @template {Pattern} [Patterns=never] */ export class PatternMatcher = never> { /** * @param {Schema} [$state] */ constructor($state?: Schema); /** * @type {Array} */ patterns: Array; $state: Schema | undefined; /** * @template P * @template R * @param {P} pattern * @param {(o:NoInfer>>,s:State)=>R} handler * @return {PatternMatcher>,R>>} */ if(pattern: P, handler: (o: NoInfer>>, s: State) => R): PatternMatcher>, R>>; /** * @template R * @param {(o:any,s:State)=>R} h */ else(h: (o: any, s: State) => R): PatternMatcher>; /** * @return {State extends undefined * ? >(o:In,state?:undefined)=>PatternMatchResult * : >(o:In,state:State)=>PatternMatchResult} */ done(): State extends undefined ? >(o: In, state?: undefined) => PatternMatchResult : >(o: In, state: State) => PatternMatchResult; } export function match(state?: State): PatternMatcher>>; export function random(gen: prng.PRNG, schema: S): Unwrap>; export type Primitive = string | number | bigint | boolean | null | undefined | symbol; export type AnyObject = { [k: string | number | symbol]: any; }; export type Unwrap = T extends Schema ? X : T; export type TypeOf = T extends Schema ? X : T; export type UnwrapArray = T extends readonly [Schema, ...infer Rest] ? [First, ...UnwrapArray] : []; export type CastToSchema = T extends Schema ? Schema : never; export type TupleLast = Arr extends [...unknown[], infer L] ? L : never; export type TuplePop = Arr extends [...infer Fs, unknown] ? Fs : never; export type Intersect = T extends [] ? {} : T extends [infer First] ? First : T extends [infer First, ...infer Rest] ? First & Intersect : never; export type Instance any) | ((...args: any[]) => any)> = Constr extends ((...args: any[]) => infer T) ? T : (Constr extends (new (...args: any[]) => any) ? InstanceType : never); export type CastStringTemplateArgsToTemplate>> = Ts extends [] ? `` : (Ts extends [infer T] ? (Unwrap extends (string | number) ? Unwrap : never) : (Ts extends [infer T1, ...infer Rest] ? `${Unwrap extends (string | number) ? Unwrap : never}${Rest extends Array> ? CastStringTemplateArgsToTemplate : never}` : never)); export type $ObjectToType; }> = { [Key in keyof S as S[Key] extends $Optional> ? Key : never]?: S[Key] extends $Optional> ? Type : never; } & { [Key in keyof S as S[Key] extends $Optional> ? never : Key]: S[Key] extends Schema ? Type : never; }; export type _ObjectDefToSchema = Schema<{ [Key in keyof S as S[Key] extends $Optional> ? Key : never]?: S[Key] extends $Optional> ? Type : never; } & { [Key in keyof S as S[Key] extends $Optional> ? never : Key]: S[Key] extends Schema ? Type : never; }>; export type _LArgsToLambdaDef[]> = (...args: UnwrapArray>) => Unwrap>; export type JSONArray = JSON[]; export type JSON = Primitive | JSONArray | { [key: string]: JSON; }; export type ReadSchemaOld = IN extends Schema ? IN : (IN extends string | number | boolean | null ? Schema : (IN extends new (...args: any[]) => any ? Schema> : (IN extends any[] ? Schema<{ [K in keyof IN]: Unwrap>; }[number]> : (IN extends object ? (_ObjectDefToSchema<{ [K in keyof IN]: ReadSchema; }> extends Schema ? Schema<{ [K in keyof S]: S[K]; }> : never) : never)))); export type ReadSchema = [Extract>, Extract, Extract any>, Extract, Extract | string | number | boolean | null | (new (...args: any[]) => any) | any[]>, object>] extends [infer Schemas, infer Primitives, infer Constructors, infer Arrs, infer Obj] ? Schema<(Schemas extends Schema ? S : never) | Primitives | (Constructors extends new (...args: any[]) => any ? InstanceType : never) | (Arrs extends any[] ? { [K in keyof Arrs]: Unwrap>; }[number] : never) | (Obj extends object ? Unwrap<(_ObjectDefToSchema<{ [K in keyof Obj]: ReadSchema; }> extends Schema ? Schema<{ [K in keyof S]: S[K]; }> : never)> : never)> : never; export type Q = ReadSchema<{ x: 42; } | { y: 99; } | Schema | [1, 2, {}]>; export type Pattern = { if: Schema; h: (o: In, state?: any) => Out; }; export type PatternMatchResult

, In> = ReturnType>["h"]>; import * as equalityTraits from './trait/equality.js'; /** * @template {Schema} S * @extends Schema|undefined> */ declare class $Optional> extends Schema | undefined> { /** * @param {S} shape */ constructor(shape: S); shape: S; get [isOptionalSymbol](): boolean; } /** * @typedef {string|number|bigint|boolean|null|undefined|symbol} Primitive */ /** * @typedef {{ [k:string|number|symbol]: any }} AnyObject */ /** * @template T * @typedef {T extends Schema ? X : T} Unwrap */ /** * @template T * @typedef {T extends Schema ? X : T} TypeOf */ /** * @template {readonly unknown[]} T * @typedef {T extends readonly [Schema, ...infer Rest] ? [First, ...UnwrapArray] : [] } UnwrapArray */ /** * @template T * @typedef {T extends Schema ? Schema : never} CastToSchema */ /** * @template {unknown[]} Arr * @typedef {Arr extends [...unknown[], infer L] ? L : never} TupleLast */ /** * @template {unknown[]} Arr * @typedef {Arr extends [...infer Fs, unknown] ? Fs : never} TuplePop */ /** * @template {readonly unknown[]} T * @typedef {T extends [] * ? {} * : T extends [infer First] * ? First * : T extends [infer First, ...infer Rest] * ? First & Intersect * : never * } Intersect */ declare const schemaSymbol: unique symbol; /** * @extends Schema */ declare class $Never extends Schema { constructor(); } import * as prng from './prng.js'; declare const isOptionalSymbol: unique symbol; export {}; //# sourceMappingURL=schema.d.ts.map