/** * Schema IR and the ArkType-compatible definition parser. * * `parseDef` turns the definition subset this repo uses — string DSL * (primitives, literals, unions, arrays, bounds, `number.integer`, * `string.url`, inline `= literal` defaults), object literals (optional `?` * keys, `"+"` undeclared-key policy, `"[string]"` index signatures), tuple * `[def, "[]"]` arrays, and embedded `Type` instances — into a small IR tree * consumed by the interpreter (`interp.ts`), the JIT compiler (`compile.ts`), * and the JSON Schema emitter (`json-schema.ts`). */ import { type ErrorConfig, OmpErrors } from "./errors.js"; /** Brand carried by `Type` instances so the parser can embed them in defs. */ export declare const IR_BRAND: unique symbol; declare const kMorph: unique symbol; declare const kMorphOwner: unique symbol; declare const kAlias: unique symbol; declare const kAliasOwner: unique symbol; declare const kSimple: unique symbol; declare const kSimpleOwner: unique symbol; interface IRAnalysis { [kMorph]?: boolean; [kMorphOwner]?: object; [kAlias]?: boolean; [kAliasOwner]?: object; [kSimple]?: boolean; [kSimpleOwner]?: object; /** Node-local metadata used for shallow error formatting. */ cfg?: ErrorConfig; /** True when `desc` was derived from the node itself rather than authored via `.describe()`. */ descAuto?: boolean; } /** * The parser-facing surface of an embedded `Type` instance. * `type.ts` implements this on every schema it creates. */ export interface EmbeddableSchema { [IR_BRAND]: true; /** Structural IR of the schema (base type when runtime steps exist). */ ir: IR; /** True when the schema carries `.pipe()`/`.narrow()` steps. */ hasSteps: boolean; /** Output IR of the last `.to(target)` step, when statically known. */ stepOut?: IR; /** True when the last pipe step is bare — output shape statically unknown. */ opaqueOutput?: boolean; /** `.default()` payload; a function is a factory invoked per fill. */ defaultValue?: unknown; hasDefault: boolean; /** Precomputed output for a non-factory default after validation and morphs. */ defaultOutput?: unknown; hasDefaultOutput?: boolean; /** `.describe()` annotation, emitted into JSON Schema. */ description?: string; /** Full validate+morph pipeline (identical to calling the schema). */ run(value: unknown, path?: readonly PropertyKey[]): unknown; } /** Policy for undeclared object keys. */ export type Extras = "keep" | "reject" | "delete"; /** Constructor accepted by `type.instanceOf` and tuple `instanceof` expressions. */ export type Constructor = abstract new (...args: never[]) => object; /** Context available to in-definition morph callbacks. */ export interface MorphContext { /** Return a validation error at the current path. */ error(expectation: string): OmpErrors; /** Alias of `error` matching ArkType's rejection vocabulary. */ reject(expectation: string): OmpErrors; } /** One fixed tuple position, optionally absent or defaulted. */ export interface TupleItemIR { val: IR; opt: boolean; def?: unknown; defFactory?: boolean; hasDefault?: boolean; /** True once the default has been validated and static morph output precomputed. */ defValidated?: boolean; } /** Fixed, optional, variadic, and postfix tuple sequence. */ export interface TupleIR { k: "tuple"; prefix: TupleItemIR[]; variadic?: IR; postfix: IR[]; desc?: string; } export type IR = IRAnalysis & ({ k: "unknown"; desc?: string; } | { k: "null"; desc?: string; } | { k: "undefined"; desc?: string; } | { k: "boolean"; desc?: string; } | { k: "bigint"; desc?: string; } | { k: "symbol"; desc?: string; } | { k: "never"; desc?: string; } /** Any non-null object (the bare `object` keyword). */ | { k: "anyobject"; desc?: string; } | { k: "string"; min?: number; max?: number; url?: boolean; desc?: string; } | { k: "number"; min?: number; max?: number; xmin?: boolean; xmax?: boolean; int?: boolean; divisor?: number; desc?: string; } | { k: "lit"; v: unknown; desc?: string; } | { k: "union"; members: IR[]; desc?: string; } | { k: "intersection"; members: IR[]; desc?: string; } | { k: "array"; el: IR; min?: number; max?: number; desc?: string; } | TupleIR | { k: "object"; props: PropIR[]; index?: IR; symbolIndex?: IR; patternIndexes?: { key: IR; val: IR; }[]; extras: Extras; desc?: string; } | { k: "refine"; base: IR; pred: (value: unknown) => boolean | OmpErrors; expected: string; json?: Record; desc?: string; } | { k: "morph"; input: IR; fn: (value: unknown, context: MorphContext) => unknown; out?: IR; desc?: string; } | { k: "instance"; ctor: Constructor; expected: string; desc?: string; } | { k: "alias"; name: string; resolve: () => IR; desc?: string; } /** Embedded schema with runtime steps; validated by calling `run`. */ | { k: "sub"; schema: EmbeddableSchema; desc?: string; }); export interface PropIR { key: PropertyKey; opt: boolean; val: IR; /** Default payload (value, or factory when `defFactory`); missing key is filled. */ def?: unknown; defFactory?: boolean; hasDefault?: boolean; /** True once the default has been validated and static morph output precomputed. */ defValidated?: boolean; } /** Definition input accepted by `type()` and object property values. */ export type Def = string | RegExp | Date | EmbeddableSchema | readonly unknown[] | { readonly [k: string]: unknown; }; /** Resolve named scope aliases and, when present, scoped generic invocations. */ export interface AliasResolver { (name: string): IR | undefined; hasGeneric?(name: string): boolean; generic?(name: string, arguments_: readonly IR[]): IR | undefined; } /** Declare that `resolve` only intercepts `this` (see THIS_ONLY_RESOLVERS). */ export declare function markThisOnlyResolver(resolve: AliasResolver): void; /** Install the assignability comparator used by `Extract`/`Exclude`. */ export declare function useAssignability(compare: (source: IR, target: IR) => boolean): void; /** * Distribute `base` over its union members, keeping those assignable to * `target` (`keepAssignable`) or those that are not (`Exclude`). */ export declare function distributeFilter(base: IR, target: IR, keepAssignable: boolean): IR; /** Embed a schema value: inline pure structure, keep `sub` nodes for stepped schemas. */ export declare function embed(schema: EmbeddableSchema): IR; /** Build the runtime schema for an object's or tuple's keys. */ export declare function keyOf(node: IR): IR; /** Parse a definition, optionally resolving names from an enclosing scope. */ export declare function parseDef(def: unknown, resolve?: AliasResolver): IR; /** Whether `ir` needs no construction-time normalization or morph analysis. */ export declare function isSimpleIR(ir: IR): boolean; /** True when validating `ir` can produce an output different from its input. */ export declare function hasMorph(ir: IR): boolean; /** * True when a traversal of `ir` can revisit nodes through recursive aliases, * requiring cycle guards in the interpreter. Embedded sub-schemas run their * own guarded traversal and are intentionally not inspected. */ export declare function hasAlias(ir: IR): boolean; /** Human-readable expectation for error messages, e.g. `"a string"`. */ export declare function expectedOf(ir: IR): string; export {};