import { PathArray } from './path.js'; /** * The type of valid Spartan Schemas. * * When writing schemas directly in Typescript code, you should not use this * type; instead, use `as const` and let Typescript infer the exact type of the * schema. */ export interface Schema { readonly spartan?: 1; readonly let?: { readonly [key: string]: SchemaType; }; readonly schema: SchemaType; } export declare type SchemaType = null | 'null' | 'boolean' | 'integer' | 'float' | 'number' | 'string' | 'date' | 'binary' | readonly [ 'enum', string | number | boolean | null, ...(string | number | boolean | null)[] ] | readonly ['tuple', SchemaType, ...SchemaType[]] | readonly ['array', SchemaType, ...SchemaType[]] | readonly ['dictionary', SchemaType] | readonly ['oneof', SchemaType, ...SchemaType[]] | readonly ['ref', string] | { readonly [key: string]: SchemaType | readonly ['optional', SchemaType]; }; export interface SchemaError { readonly message: string; readonly location: PathArray; } /** * A type predicate that checks whether `schema` is a valid Spartan Schema. * * `errors` is a mutable array of `{ message, location }` pairs; if it is * present and `isSchema` returns false, it will be populated with a list of * parsing errors. */ export declare function isSchema(schema: unknown, errors?: SchemaError[]): schema is Schema; declare type MatchesObject = { readonly [K in keyof S as S[K] extends readonly ['optional', SchemaType] ? K : never]?: S[K] extends readonly ['optional', SchemaType] ? MatchesSchemaType : unknown; } & { readonly [K in keyof S as S[K] extends readonly ['optional', SchemaType] ? never : K]: S[K] extends SchemaType ? MatchesSchemaType : unknown; }; declare type MatchesArray = A extends readonly [] ? never : A extends readonly [SchemaType] ? readonly MatchesSchemaType[] : A extends readonly [SchemaType, SchemaType] ? readonly [MatchesSchemaType, ...MatchesSchemaType[]] : A extends readonly [SchemaType, SchemaType, SchemaType] ? readonly [ MatchesSchemaType, MatchesSchemaType, ...MatchesSchemaType[] ] : A extends readonly [SchemaType, SchemaType, SchemaType, SchemaType] ? readonly [ MatchesSchemaType, MatchesSchemaType, MatchesSchemaType, ...MatchesSchemaType[] ] : A extends readonly [SchemaType, SchemaType, SchemaType, SchemaType, SchemaType] ? readonly [ MatchesSchemaType, MatchesSchemaType, MatchesSchemaType, MatchesSchemaType, ...MatchesSchemaType[] ] : A extends readonly [SchemaType, SchemaType, SchemaType, SchemaType, SchemaType, SchemaType] ? readonly [ MatchesSchemaType, MatchesSchemaType, MatchesSchemaType, MatchesSchemaType, MatchesSchemaType, ...MatchesSchemaType[] ] : A extends readonly [SchemaType, SchemaType, SchemaType, SchemaType, SchemaType, SchemaType, SchemaType] ? readonly [ MatchesSchemaType, MatchesSchemaType, MatchesSchemaType, MatchesSchemaType, MatchesSchemaType, MatchesSchemaType, ...MatchesSchemaType[] ] : A extends readonly [ SchemaType, SchemaType, SchemaType, SchemaType, SchemaType, SchemaType, SchemaType, SchemaType ] ? readonly [ MatchesSchemaType, MatchesSchemaType, MatchesSchemaType, MatchesSchemaType, MatchesSchemaType, MatchesSchemaType, MatchesSchemaType, ...MatchesSchemaType[] ] : A extends readonly [ SchemaType, SchemaType, SchemaType, SchemaType, SchemaType, SchemaType, SchemaType, SchemaType, SchemaType ] ? readonly [ MatchesSchemaType, MatchesSchemaType, MatchesSchemaType, MatchesSchemaType, MatchesSchemaType, MatchesSchemaType, MatchesSchemaType, MatchesSchemaType, ...MatchesSchemaType[] ] : A extends readonly [ SchemaType, SchemaType, SchemaType, SchemaType, SchemaType, SchemaType, SchemaType, SchemaType, SchemaType, SchemaType ] ? readonly [ MatchesSchemaType, MatchesSchemaType, MatchesSchemaType, MatchesSchemaType, MatchesSchemaType, MatchesSchemaType, MatchesSchemaType, MatchesSchemaType, MatchesSchemaType, ...MatchesSchemaType[] ] : A extends readonly [ SchemaType, SchemaType, SchemaType, SchemaType, SchemaType, SchemaType, SchemaType, SchemaType, SchemaType, SchemaType, ...SchemaType[] ] ? readonly [ MatchesSchemaType, MatchesSchemaType, MatchesSchemaType, MatchesSchemaType, MatchesSchemaType, MatchesSchemaType, MatchesSchemaType, MatchesSchemaType, MatchesSchemaType, MatchesSchemaType, ...unknown[] ] : unknown[]; declare type MatchesTuple = A extends readonly [] ? never : A extends readonly [SchemaType] ? readonly [MatchesSchemaType] : A extends readonly [SchemaType, SchemaType] ? readonly [MatchesSchemaType, MatchesSchemaType] : A extends readonly [SchemaType, SchemaType, SchemaType] ? readonly [ MatchesSchemaType, MatchesSchemaType, MatchesSchemaType ] : A extends readonly [SchemaType, SchemaType, SchemaType, SchemaType] ? readonly [ MatchesSchemaType, MatchesSchemaType, MatchesSchemaType, MatchesSchemaType ] : A extends readonly [SchemaType, SchemaType, SchemaType, SchemaType, SchemaType] ? readonly [ MatchesSchemaType, MatchesSchemaType, MatchesSchemaType, MatchesSchemaType, MatchesSchemaType ] : A extends readonly [SchemaType, SchemaType, SchemaType, SchemaType, SchemaType, SchemaType] ? readonly [ MatchesSchemaType, MatchesSchemaType, MatchesSchemaType, MatchesSchemaType, MatchesSchemaType, MatchesSchemaType ] : A extends readonly [SchemaType, SchemaType, SchemaType, SchemaType, SchemaType, SchemaType, SchemaType] ? readonly [ MatchesSchemaType, MatchesSchemaType, MatchesSchemaType, MatchesSchemaType, MatchesSchemaType, MatchesSchemaType, MatchesSchemaType ] : A extends readonly [ SchemaType, SchemaType, SchemaType, SchemaType, SchemaType, SchemaType, SchemaType, SchemaType ] ? readonly [ MatchesSchemaType, MatchesSchemaType, MatchesSchemaType, MatchesSchemaType, MatchesSchemaType, MatchesSchemaType, MatchesSchemaType, MatchesSchemaType ] : A extends readonly [ SchemaType, SchemaType, SchemaType, SchemaType, SchemaType, SchemaType, SchemaType, SchemaType, SchemaType ] ? readonly [ MatchesSchemaType, MatchesSchemaType, MatchesSchemaType, MatchesSchemaType, MatchesSchemaType, MatchesSchemaType, MatchesSchemaType, MatchesSchemaType, MatchesSchemaType ] : A extends readonly [ SchemaType, SchemaType, SchemaType, SchemaType, SchemaType, SchemaType, SchemaType, SchemaType, SchemaType, SchemaType ] ? readonly [ MatchesSchemaType, MatchesSchemaType, MatchesSchemaType, MatchesSchemaType, MatchesSchemaType, MatchesSchemaType, MatchesSchemaType, MatchesSchemaType, MatchesSchemaType, MatchesSchemaType ] : A extends readonly [ SchemaType, SchemaType, SchemaType, SchemaType, SchemaType, SchemaType, SchemaType, SchemaType, SchemaType, SchemaType, ...SchemaType[] ] ? readonly [ MatchesSchemaType, MatchesSchemaType, MatchesSchemaType, MatchesSchemaType, MatchesSchemaType, MatchesSchemaType, MatchesSchemaType, MatchesSchemaType, MatchesSchemaType, MatchesSchemaType, ...unknown[] ] : unknown[]; export declare type MatchesSchemaType = SchemaType extends S ? unknown : S extends null | 'null' ? null : S extends 'boolean' ? boolean : S extends 'integer' | 'float' | 'number' ? number : S extends 'string' ? string : S extends 'date' ? Date : S extends 'binary' ? Uint8Array : S extends readonly ['enum', ...infer Values] ? Values[number] : S extends readonly ['tuple', ...infer Props] ? MatchesTuple : S extends readonly ['array', ...infer Props] ? MatchesArray : S extends readonly ['dictionary', SchemaType] ? { readonly [key: string]: MatchesSchemaType; } : S extends readonly ['oneof', ...infer Branches] ? Branches extends readonly SchemaType[] ? MatchesSchemaType : unknown : S extends readonly ['ref', keyof Refs] ? MatchesSchemaType : S extends { readonly [key: string]: SchemaType | readonly ['optional', SchemaType]; } ? MatchesObject : never; /** * Given a type `S` that describes the exact shape of a `Schema`, * `MatchesSchema` is the type of values that match that schema. * * For example, `MatchesSchema<{ schema: { foo: "string" } }>` is * `{ foo: string }`. * * `MatchesSchema` is a complex recursive type, and can easily cause the * Typescript compiler to fail with a "Type instantiation is excessively deep * and possibly infinite" error. It should only be used on schema types that are * 100% statically known. */ export declare type MatchesSchema = Schema extends S ? unknown : MatchesSchemaType>; export interface ValidationError { dataPath: PathArray; schemaPath: PathArray; message: string; children?: ValidationError[][]; } export declare function validationErrorToString({ dataPath, schemaPath, message, children }: ValidationError, maxLength?: number, maxDepth?: number): string; /** * A detailed error thrown by `assertMatchesSchema` when schema validation * fails. Includes the schema, the JSON that didn't match, and the list of * validation errors, complete with JSONPath locations in both the schema and * the JSON value. */ export declare class SchemaAssertionError extends Error { readonly json: unknown; readonly schema: Schema; readonly validationErrors: ValidationError[]; private readonly initialMessage; static maxErrorsPerMessage: number; constructor(json: unknown, schema: Schema, validationErrors: ValidationError[], initialMessage?: string); get message(): string; get validationMessage(): string; } /** * A curried function that checks whether `value` matches `schema` and returns * a boolean. * * If `schema` is statically known at typechecking type (defined with * `as const`), then the function returned by `matchesSchema(schema)` will be a * type predicate. * * `errors` is a mutable array of `{ dataPath, schemaPath, message, children? }` * objects. If it is present and `matchesSchema` returns false, it will be * populated with a list of validation errors. */ export declare function matchesSchema({ schema, let: refs, }: S): (value: unknown, errors?: ValidationError[]) => value is MatchesSchema; /** * A curried function that checks whether `value` matches `schema` and throws * a `SchemaAssertionError` if it doesn't. * * If `schema` is statically known at typechecking type (defined with * `as const`), then the function returned by `assertMatchesSchema(schema)` will * be a type assertion function. * * `message` is an optional message string to include in the thrown error. */ export declare function assertMatchesSchema(schema: S): (value: unknown, message?: string) => asserts value is MatchesSchema; /** * Returns the *zero value* of this schema's root type. * * | Type | Zero value | * | -------------- | --------------------------------------------- | * | `null` | `null` | * | boolean | `false` | * | integer, float | `0` | * | string | `""` | * | binary | `0`-length `Uint8Array` | * | date | `new Date(0)` (Jan 1, 1970) | * | object | object populated with properties' zero values | * | `oneof` | zero value of first type | * | `enum` | first enum value | * | `array` | `[]` | * | `tuple` | array populated with elements' zero values | * | `dictionary` | `{}` | * * This function typechecks the schema it receives. If it is passed a known * schema type `S` (defined `as const` in a Typescript file), then its return * type will be `MatchesSchema`. * * May throw an exception if the schema type is infinitely recursive. */ export declare function zeroValue({ schema, let: refs, }: S): MatchesSchema; export {};