import type * as t from '.'; import type { Result, Failure } from './result'; import { ParsedValueConfig } from './types/ParsedValue'; interface Helpers { readonly Union: typeof t.Union; readonly Intersect: typeof t.Intersect; readonly Constraint: typeof t.Constraint; readonly Brand: typeof t.Brand; readonly ParsedValue: typeof t.ParsedValue; } export declare function provideHelpers(h: Helpers): void; export declare type InnerValidateHelper = (runtype: RuntypeBase, value: unknown) => Result; declare const internalSymbol: unique symbol; declare const internal: typeof internalSymbol; export declare function assertRuntype(...values: RuntypeBase[]): void; export declare function isRuntype(value: unknown): value is RuntypeBase; export declare type ResultWithCycle = (Result & { cycle?: false; }) | Cycle; export declare type SealedState = { readonly keysFromIntersect?: ReadonlySet; readonly deep: boolean; } | false; export interface InternalValidation { /** * parse */ p(x: any, innerValidate: (runtype: RuntypeBase, value: unknown, sealed?: SealedState) => Result, innerValidateToPlaceholder: (runtype: RuntypeBase, value: unknown, sealed?: SealedState) => ResultWithCycle, mode: 'p' | 's' | 't', sealed: SealedState): ResultWithCycle; /** * test */ t?: (x: any, innerValidate: (runtype: RuntypeBase, value: unknown, sealed?: SealedState) => Failure | undefined, sealed: SealedState, isOptionalTest: boolean) => Failure | undefined; /** * serialize */ s?: (x: any, innerSerialize: (runtype: RuntypeBase, value: unknown, sealed?: SealedState) => Result, innerSerializeToPlaceholder: (runtype: RuntypeBase, value: unknown, sealed?: SealedState) => ResultWithCycle, mode: 's', sealed: SealedState) => ResultWithCycle; /** * get underlying type */ u?: (mode: 'p' | 's' | 't') => RuntypeBase | undefined; /** * get fields, not called if "u" is implemented, can return * undefined to indicate that arbitrarily many fields are * possible. */ f?: (mode: 'p' | 't' | 's') => ReadonlySet | undefined; } /** * A runtype determines at runtime whether a value conforms to a type specification. */ export interface RuntypeBase { readonly tag: string; /** * Verifies that a value conforms to this runtype. When given a value that does * not conform to the runtype, throws an exception. * * @throws ValidationError */ assert(x: any): asserts x is TParsed; /** * A type guard for this runtype. */ test(x: any): x is TParsed; /** * Validates the value conforms to this type, and performs * the `parse` action for any `ParsedValue` types. * * If the value is valid, it returns the parsed value, * otherwise it throws a ValidationError. * * @throws ValidationError */ parse(x: any): TParsed; /** * Validates the value conforms to this type, and performs * the `parse` action for any `ParsedValue` types. * * Returns a `Result`, constaining the parsed value or * error message. Does not throw! */ safeParse(x: any): Result; show?: (needsParens: boolean) => string; [internal]: InternalValidation; } /** * A runtype determines at runtime whether a value conforms to a type specification. */ export interface Runtype extends RuntypeBase { /** * Union this Runtype with another. */ Or(B: B): t.Union<[this, B]>; /** * Intersect this Runtype with another. */ And(B: B): t.Intersect<[this, B]>; /** * Use an arbitrary constraint function to validate a runtype, and optionally * to change its name and/or its static type. * * @template T - Optionally override the static type of the resulting runtype * @param {(x: Static) => boolean | string} constraint - Custom function * that returns `true` if the constraint is satisfied, `false` or a custom * error message if not. * @param [options] * @param {string} [options.name] - allows setting the name of this * constrained runtype, which is helpful in reflection or diagnostic * use-cases. */ withConstraint, K = unknown>(constraint: t.ConstraintCheck, options?: { name?: string; args?: K; }): t.Constraint; /** * Helper function to convert an underlying Runtype into another static type * via a type guard function. The static type of the runtype is inferred from * the type of the test function. * * @template T - Typically inferred from the return type of the type guard * function, so usually not needed to specify manually. * @param {(x: Static) => x is T} test - Type test function (see * https://www.typescriptlang.org/docs/handbook/advanced-types.html#user-defined-type-guards) * * @param [options] * @param {string} [options.name] - allows setting the name of this * constrained runtype, which is helpful in reflection or diagnostic * use-cases. */ withGuard, K = unknown>(test: (x: Static) => x is T, options?: { name?: string; args?: K; }): t.Constraint; /** * Adds a brand to the type. */ withBrand(brand: B): t.Brand; /** * Apply conversion functions when parsing/serializing this value */ withParser(value: ParsedValueConfig): t.ParsedValue; } export interface Codec extends Runtype { /** * Validates the value conforms to this type, and performs * the `serialize` action for any `ParsedValue` types. * * If the value is valid, and the type supports serialize, * it returns the serialized value, otherwise it throws a * ValidationError. * * @throws ValidationError */ serialize: (x: TParsed) => unknown; /** * Validates the value conforms to this type, and performs * the `serialize` action for any `ParsedValue` types. * * Returns a `Result`, constaining the serialized value or * error message. Does not throw! */ safeSerialize: (x: TParsed) => Result; } /** * Obtains the static type associated with a Runtype. */ export declare type Static> = A extends RuntypeBase ? T : unknown; export declare function create>(tag: TConfig['tag'], internalImplementation: InternalValidation> | InternalValidation>['p'], config: Omit): TConfig; export declare type Cycle = { success: true; cycle: true; placeholder: Partial; unwrap: () => Result; }; /** * Get the underlying type of a runtype, if it is a wrapper around another type */ export declare function unwrapRuntype(t: RuntypeBase, mode: 'p' | 's' | 't'): RuntypeBase; export declare function createValidationPlaceholder(placeholder: T, fn: (placeholder: T) => Failure | undefined): Cycle; export declare function mapValidationPlaceholder(source: ResultWithCycle, fn: (placeholder: T) => Result, extraGuard?: RuntypeBase): ResultWithCycle; declare const OpaqueVisitedState: unique symbol; export declare type OpaqueVisitedState = typeof OpaqueVisitedState; export declare function createVisitedState(): OpaqueVisitedState; declare const OpaqueGuardVisitedState: unique symbol; export declare type OpaqueGuardVisitedState = typeof OpaqueGuardVisitedState; export declare function createGuardVisitedState(): OpaqueGuardVisitedState; export declare function innerValidate(targetType: RuntypeBase, value: any, $visited: OpaqueVisitedState, sealed: SealedState): Result; export declare function innerSerialize(targetType: RuntypeBase, value: any, $visited: OpaqueVisitedState, sealed: SealedState): Result; export declare function innerGuard(targetType: RuntypeBase, value: any, $visited: OpaqueGuardVisitedState, sealed: SealedState, isOptionalTest: boolean): Failure | undefined; /** * Get the possible fields for a runtype * Returns "undefined" if there can be arbitrary fields (e.g. Record) */ export declare function getFields(t: RuntypeBase, mode: 'p' | 's' | 't'): ReadonlySet | undefined; export {};