import { SchemaUtils } from "./builders"; import { MaybePromise } from "./utils/MaybePromise"; export type Schema = BaseSchema & SchemaUtils; export type inferRaw = S extends Schema ? Raw : never; export type inferParsed = S extends Schema ? Parsed : never; export interface BaseSchema { parse: (raw: unknown, opts?: SchemaOptions) => MaybePromise>; json: (parsed: unknown, opts?: SchemaOptions) => MaybePromise>; getType: () => SchemaType | Promise; } export const SchemaType = { DATE: "date", ENUM: "enum", LIST: "list", STRING_LITERAL: "stringLiteral", BOOLEAN_LITERAL: "booleanLiteral", OBJECT: "object", ANY: "any", BOOLEAN: "boolean", NUMBER: "number", STRING: "string", UNKNOWN: "unknown", RECORD: "record", SET: "set", UNION: "union", UNDISCRIMINATED_UNION: "undiscriminatedUnion", OPTIONAL: "optional", } as const; export type SchemaType = typeof SchemaType[keyof typeof SchemaType]; export type MaybeValid = Valid | Invalid; export interface Valid { ok: true; value: T; } export interface Invalid { ok: false; errors: ValidationError[]; } export interface ValidationError { path: string[]; message: string; } export interface SchemaOptions { /** * how to handle unrecognized keys in objects * * @default "fail" */ unrecognizedObjectKeys?: "fail" | "passthrough" | "strip"; /** * whether to fail when an unrecognized discriminant value is * encountered in a union * * @default false */ allowUnrecognizedUnionMembers?: boolean; /** * whether to fail when an unrecognized enum value is encountered * * @default false */ allowUnrecognizedEnumValues?: boolean; /** * whether to allow data that doesn't conform to the schema. * invalid data is passed through without transformation. * * when this is enabled, .parse() and .json() will always * return `ok: true`. `.parseOrThrow()` and `.jsonOrThrow()` * will never fail. * * @default false */ skipValidation?: boolean; /** * each validation failure contains a "path" property, which is * the breadcrumbs to the offending node in the JSON. you can supply * a prefix that is prepended to all the errors' paths. this can be * helpful for zurg's internal debug logging. */ breadcrumbsPrefix?: string[]; }