UNPKG

zod-compiler

Version:

Compile Zod schemas to fast parsers or TypeScript types

387 lines (378 loc) 12.4 kB
declare enum ZcParsedType { string = "string", nan = "nan", number = "number", integer = "integer", float = "float", boolean = "boolean", date = "date", bigint = "bigint", symbol = "symbol", function = "function", undefined = "undefined", null = "null", array = "array", object = "object", unknown = "unknown", promise = "promise", void = "void", never = "never", map = "map", set = "set" } declare function typeOf(data: any): ZcParsedType; declare function mergeValues(a: any, b: any): { valid: true; data: any; } | { valid: false; }; declare function isValidJWT(jwt: string, alg?: string): boolean; declare function floatSafeRemainder(val: number, step: number): number; declare const helpers: { readonly typeOf: typeof typeOf; readonly mergeValues: typeof mergeValues; readonly isValidJWT: typeof isValidJWT; readonly floatSafeRemainder: typeof floatSafeRemainder; }; type Primitive = string | number | symbol | bigint | boolean | null | undefined; type Scalars = Primitive | Primitive[]; type typeToFlattenedError<T, U = string> = { formErrors: U[]; fieldErrors: { [P in keyof T]?: U[]; }; }; declare enum ZcIssueCode { invalid_type = "invalid_type", invalid_literal = "invalid_literal", custom = "custom", invalid_union = "invalid_union", invalid_union_discriminator = "invalid_union_discriminator", invalid_enum_value = "invalid_enum_value", unrecognized_keys = "unrecognized_keys", invalid_arguments = "invalid_arguments", invalid_return_type = "invalid_return_type", invalid_date = "invalid_date", invalid_string = "invalid_string", too_small = "too_small", too_big = "too_big", invalid_intersection_types = "invalid_intersection_types", not_multiple_of = "not_multiple_of", not_finite = "not_finite" } interface ZcIssueBase { path: (string | number)[]; message?: string; } interface ZcInvalidTypeIssue extends ZcIssueBase { code: ZcIssueCode.invalid_type; expected: ZcParsedType; received: ZcParsedType; } interface ZcInvalidLiteralIssue extends ZcIssueBase { code: ZcIssueCode.invalid_literal; expected: unknown; received: unknown; } interface ZcUnrecognizedKeysIssue extends ZcIssueBase { code: ZcIssueCode.unrecognized_keys; keys: string[]; } interface ZcInvalidUnionIssue extends ZcIssueBase { code: ZcIssueCode.invalid_union; unionErrors: ZcError[]; } interface ZcInvalidUnionDiscriminatorIssue extends ZcIssueBase { code: ZcIssueCode.invalid_union_discriminator; options: Primitive[]; } interface ZcInvalidEnumValueIssue extends ZcIssueBase { code: ZcIssueCode.invalid_enum_value; received: string | number; options: (string | number)[]; } interface ZcInvalidArgumentsIssue extends ZcIssueBase { code: ZcIssueCode.invalid_arguments; argumentsError: ZcError; } interface ZcInvalidReturnTypeIssue extends ZcIssueBase { code: ZcIssueCode.invalid_return_type; returnTypeError: ZcError; } interface ZcInvalidDateIssue extends ZcIssueBase { code: ZcIssueCode.invalid_date; } type StringValidation = 'email' | 'url' | 'emoji' | 'uuid' | 'nanoid' | 'regex' | 'cuid' | 'cuid2' | 'ulid' | 'datetime' | 'date' | 'time' | 'duration' | 'ip' | 'cidr' | 'base64' | 'jwt' | 'base64url' | { includes: string; position?: number; } | { startsWith: string; } | { endsWith: string; }; interface ZcInvalidStringIssue extends ZcIssueBase { code: ZcIssueCode.invalid_string; validation: StringValidation; } interface ZcTooSmallIssue extends ZcIssueBase { code: ZcIssueCode.too_small; minimum: number | bigint; inclusive: boolean; exact?: boolean; type: 'array' | 'string' | 'number' | 'set' | 'date' | 'bigint'; } interface ZcTooBigIssue extends ZcIssueBase { code: ZcIssueCode.too_big; maximum: number | bigint; inclusive: boolean; exact?: boolean; type: 'array' | 'string' | 'number' | 'set' | 'date' | 'bigint'; } interface ZcInvalidIntersectionTypesIssue extends ZcIssueBase { code: ZcIssueCode.invalid_intersection_types; } interface ZcNotMultipleOfIssue extends ZcIssueBase { code: ZcIssueCode.not_multiple_of; multipleOf: number | bigint; } interface ZcNotFiniteIssue extends ZcIssueBase { code: ZcIssueCode.not_finite; } interface ZcCustomIssue extends ZcIssueBase { code: ZcIssueCode.custom; params?: { [k: string]: any; }; } type DenormalizedError = { [k: string]: DenormalizedError | string[]; }; type ZcIssueOptionalMessage = ZcInvalidTypeIssue | ZcInvalidLiteralIssue | ZcUnrecognizedKeysIssue | ZcInvalidUnionIssue | ZcInvalidUnionDiscriminatorIssue | ZcInvalidEnumValueIssue | ZcInvalidArgumentsIssue | ZcInvalidReturnTypeIssue | ZcInvalidDateIssue | ZcInvalidStringIssue | ZcTooSmallIssue | ZcTooBigIssue | ZcInvalidIntersectionTypesIssue | ZcNotMultipleOfIssue | ZcNotFiniteIssue | ZcCustomIssue; type ZcIssue = ZcIssueOptionalMessage & { fatal?: boolean; message: string; }; type recursiveZcFormattedError<T> = T extends [any, ...any[]] ? { [K in keyof T]?: ZcFormattedError<T[K]>; } : T extends any[] ? { [k: number]: ZcFormattedError<T[number]>; } : T extends object ? { [K in keyof T]?: ZcFormattedError<T[K]>; } : unknown; type ZcFormattedError<T, U = string> = { _errors: U[]; } & recursiveZcFormattedError<NonNullable<T>>; declare class ZcError<T = any> extends Error { issues: ZcIssue[]; get errors(): ZcIssue[]; constructor(issues: ZcIssue[]); format(): ZcFormattedError<T>; format<U>(mapper: (issue: ZcIssue) => U): ZcFormattedError<T, U>; static create(issues: ZcIssue[]): ZcError; toString(): string; get message(): string; get isEmpty(): boolean; addIssue(sub: ZcIssue): void; addIssues(subs?: ZcIssue[]): void; flatten(): typeToFlattenedError<T>; flatten<U>(mapper?: (issue: ZcIssue) => U): typeToFlattenedError<T, U>; get formErrors(): typeToFlattenedError<T>; } type IssueData = Omit<ZcIssueOptionalMessage, 'path'> & { path?: (string | number)[]; fatal?: boolean; }; type ErrorMapCtx = { defaultError: string; data: any; }; type ZcErrorMap = (issue: ZcIssueOptionalMessage, ctx: ErrorMapCtx) => { message: string; }; declare const CUID: RegExp; declare const CUID2: RegExp; declare const ULID: RegExp; declare const UUID: RegExp; declare const NANOID: RegExp; declare const JWT: RegExp; declare const DURATION: RegExp; declare const EMAIL: RegExp; declare const EMOJI: RegExp; declare const IPV4: RegExp; declare const IPV4_CIDR: RegExp; declare const IPV6: RegExp; declare const IPV6_CIDR: RegExp; declare const BASE64: RegExp; declare const BASE64URL: RegExp; declare const DATE: RegExp; declare const regex_BASE64: typeof BASE64; declare const regex_BASE64URL: typeof BASE64URL; declare const regex_CUID: typeof CUID; declare const regex_CUID2: typeof CUID2; declare const regex_DATE: typeof DATE; declare const regex_DURATION: typeof DURATION; declare const regex_EMAIL: typeof EMAIL; declare const regex_EMOJI: typeof EMOJI; declare const regex_IPV4: typeof IPV4; declare const regex_IPV4_CIDR: typeof IPV4_CIDR; declare const regex_IPV6: typeof IPV6; declare const regex_IPV6_CIDR: typeof IPV6_CIDR; declare const regex_JWT: typeof JWT; declare const regex_NANOID: typeof NANOID; declare const regex_ULID: typeof ULID; declare const regex_UUID: typeof UUID; declare namespace regex { export { regex_BASE64 as BASE64, regex_BASE64URL as BASE64URL, regex_CUID as CUID, regex_CUID2 as CUID2, regex_DATE as DATE, regex_DURATION as DURATION, regex_EMAIL as EMAIL, regex_EMOJI as EMOJI, regex_IPV4 as IPV4, regex_IPV4_CIDR as IPV4_CIDR, regex_IPV6 as IPV6, regex_IPV6_CIDR as IPV6_CIDR, regex_JWT as JWT, regex_NANOID as NANOID, regex_ULID as ULID, regex_UUID as UUID, }; } /** * The Standard Schema interface. */ type StandardSchemaV1<Input = unknown, Output = Input> = { /** * The Standard Schema properties. */ readonly '~standard': StandardSchemaV1Props<Input, Output>; }; /** * The Standard Schema properties interface. */ interface StandardSchemaV1Props<Input = unknown, Output = Input> { /** * The version number of the standard. */ readonly version: 1; /** * The vendor name of the schema library. */ readonly vendor: string; /** * Validates unknown input values. */ readonly validate: (value: unknown) => StandardSchemaV1Result<Output> | Promise<StandardSchemaV1Result<Output>>; /** * Inferred types associated with the schema. */ readonly types?: StandardSchemaV1Types<Input, Output> | undefined; } /** * The result interface of the validate function. */ type StandardSchemaV1Result<Output> = StandardSchemaV1SuccessResult<Output> | StandardSchemaV1FailureResult; /** * The result interface if validation succeeds. */ interface StandardSchemaV1SuccessResult<Output> { /** * The typed output value. */ readonly value: Output; /** * The non-existent issues. */ readonly issues?: undefined; } /** * The result interface if validation fails. */ interface StandardSchemaV1FailureResult { /** * The issues of failed validation. */ readonly issues: ReadonlyArray<StandardSchemaV1Issue>; } /** * The issue interface of the failure output. */ interface StandardSchemaV1Issue { /** * The error message of the issue. */ readonly message: string; /** * The path of the issue, if any. */ readonly path?: ReadonlyArray<PropertyKey | StandardSchemaV1PathSegment> | undefined; } /** * The path segment interface of the issue. */ interface StandardSchemaV1PathSegment { /** * The key representing a path segment. */ readonly key: PropertyKey; } /** * The Standard Schema types interface. */ interface StandardSchemaV1Types<Input = unknown, Output = Input> { /** * The input type of the schema. */ readonly input: Input; /** * The output type of the schema. */ readonly output: Output; } declare const errorMap: ZcErrorMap; declare function setErrorMap(map: ZcErrorMap): void; declare function getErrorMap(): ZcErrorMap; declare const enum ParseStatus { VALID = 0, /** At least one issue has been identified, but parsing can still continue to identify more. */ DIRTY = 1, /** A fatal issue has been encountered and parsing cannot continue. */ INVALID = 2 } interface ParseContext<T> { output: T | null; helpers: typeof helpers; ZcError: typeof ZcError<T>; regex: typeof regex; dependencies: readonly any[]; basePath: (string | number)[]; errorMaps: ZcErrorMap[]; issues: ZcIssue[]; reportIssue(issue: ZcIssue, input?: any): void; } type SafeParseSuccess<T> = { success: true; data: T; error?: never; }; type SafeParseError<T> = { success: false; error: ZcError<T>; data?: never; }; interface ParseParams { path?: (string | number)[]; errorMap?: ZcErrorMap; } interface CompiledParser<T> extends StandardSchemaV1<unknown, T> { parse(data: unknown, params?: ParseParams): T; safeParse(data: unknown, params?: ParseParams): SafeParseSuccess<T> | SafeParseError<T>; } declare const createContext: <T>(dependencies: readonly any[], parseParams: ParseParams | undefined) => ParseContext<T>; declare function standalone<T>(parser: Function, dependencies?: readonly any[]): CompiledParser<T>; export { ParseStatus, ZcError, ZcIssueCode, createContext, standalone as default, errorMap as defaultErrorMap, getErrorMap, setErrorMap }; export type { CompiledParser, DenormalizedError, ErrorMapCtx, IssueData, ParseContext, ParseParams, Primitive, SafeParseError, SafeParseSuccess, Scalars, StringValidation, ZcCustomIssue, ZcErrorMap, ZcFormattedError, ZcInvalidArgumentsIssue, ZcInvalidDateIssue, ZcInvalidEnumValueIssue, ZcInvalidIntersectionTypesIssue, ZcInvalidLiteralIssue, ZcInvalidReturnTypeIssue, ZcInvalidStringIssue, ZcInvalidTypeIssue, ZcInvalidUnionDiscriminatorIssue, ZcInvalidUnionIssue, ZcIssue, ZcIssueBase, ZcIssueOptionalMessage, ZcNotFiniteIssue, ZcNotMultipleOfIssue, ZcTooBigIssue, ZcTooSmallIssue, ZcUnrecognizedKeysIssue, typeToFlattenedError };