import { type Kind, type WrappedValue, type UnionToIntersection, type IsEqual, type IsNever, type NeverCoalescing } from "../.."; import { type GetConstraint, type ConstraintHandler, type ConstraintError } from "../constraint"; import { type Primitive, type EligiblePrimitive, type PrimitiveHandler, type PrimitiveHandlers } from "../primitive"; import * as DEither from "../../either"; import * as DDataParser from "../../dataParser"; export declare const constraintsSetHandlerKind: import("../..").KindHandler>; export interface ConstraintsSetHandler extends Kind { /** * @deprecated */ readonly primitiveHandler: PrimitiveHandler; /** * @deprecated */ readonly constraints: GenericConstraintsHandler; readonly internal: { /** * The DataParser with all constraint checkers from the set applied. * */ readonly dataParser: DDataParser.DataParser; /** * The primitive handler used to validate and wrap values (e.g. `C.String`, `C.Number`). * */ readonly primitiveHandler: PrimitiveHandler & PrimitiveHandlers; /** * The list of constraint handlers applied by this set. * */ readonly constraints: GenericConstraintsHandler; /** * The constraint kind metadata applied by the set. * */ readonly constraintKindValue: Record; }; /** * Creates a constrained value and returns an Either. * * On error, returns `Left<"createConstraintsSetError", ConstraintError<...>>`. The `constraintName` is resolved from the failing checker; if primitive parsing fails before a constraint checker is responsible, it falls back to the first constraint name in the set. * * ```ts * const result = UsernameConstraints.create("Ada"); * * if (E.isRight(result)) { * // result: E.Right< * // "createConstraintsSet", * ``` * */ create(data: GenericInput): (DEither.Right<"createConstraintsSet", (Primitive & UnionToIntersection : never : never>)> | DEither.Left<"createConstraintsSetError", GenericConstraintsHandler[number] extends infer InferredConstraint ? InferredConstraint extends ConstraintHandler ? ConstraintError : never : never>); create>(data: GenericPrimitive): (DEither.Right<"createConstraintsSet", (GenericPrimitive & UnionToIntersection : never : never>)> | DEither.Left<"createConstraintsSetError", GenericConstraintsHandler[number] extends infer InferredConstraint ? InferredConstraint extends ConstraintHandler ? ConstraintError : never : never>); /** * Creates a constrained value and throws on error. Works with raw values or primitives. * * Throws `CreateConstraintsSetError` with the received data, the `dataParserError`, and the resolved `constraintName`. If primitive parsing fails before a constraint checker is responsible, `constraintName` falls back to the first constraint name in the set. * * ```ts * // > * } * * const value = UsernameConstraints.createOrThrow("Nova"); * // value: C.Primitive<"Nova"> & C.GetConstraints * ``` * */ createOrThrow(data: GenericInput): (Primitive & UnionToIntersection : never : never>); createOrThrow>(data: GenericPrimitive): (GenericPrimitive & UnionToIntersection : never : never>); /** * Creates a constrained value from the set handler's wider input type and returns an Either. * * This is useful when one of the set constraints refines the output type, but the source data is only known as the primitive input type. * * ```ts * const largeValue = UsernameConstraints.createWithLarge("from-database"); * // E.Left< * // "createConstraintsSetError", * // C.ConstraintError<"string-min-3" | "string-max-16"> * // > * // | E.Right< * // "createConstraintsSet", * // C.Primitive & C.GetConstraints * // > * ``` * */ createWithLarge(data: NeverCoalescing): (DEither.Right<"createConstraintsSet", (Primitive & UnionToIntersection : never : never>)> | DEither.Left<"createConstraintsSetError", GenericConstraintsHandler[number] extends infer InferredConstraint ? InferredConstraint extends ConstraintHandler ? ConstraintError : never : never>); /** * Creates a constrained value from the set handler's wider input type and throws on error. * * This is useful when one of the set constraints refines the output type, but the source data is only known as the primitive input type. * * ```ts * const strictLargeValue = UsernameConstraints.createWithLargeOrThrow("from-database"); * ``` * */ createWithLargeOrThrow(data: NeverCoalescing): (Primitive & UnionToIntersection : never : never>); /** * Creates a constrained value from an unknown input and returns an Either. * * On error, returns `Left<"createConstraintsSetError", ConstraintError<...>>`. The `constraintName` is resolved from the failing checker; if primitive parsing fails before a constraint checker is responsible, it falls back to the first constraint name in the set. * * ```ts * const primitive = C.String.createOrThrow("Kit"); * UsernameConstraints.create(primitive); * ``` * */ createWithUnknown(data: GenericInput): (DEither.Right<"createConstraintsSet", (Primitive & UnionToIntersection : never : never>)> | DEither.Left<"createConstraintsSetError", GenericConstraintsHandler[number] extends infer InferredConstraint ? InferredConstraint extends ConstraintHandler ? ConstraintError : never : never>); /** * Creates a constrained value from an unknown input and throws on error. * * Throws `CreateConstraintsSetError` with the received data, the `dataParserError`, and the resolved `constraintName`. If primitive parsing fails before a constraint checker is responsible, `constraintName` falls back to the first constraint name in the set. * * ```ts * * ``` * */ createWithUnknownOrThrow(data: GenericInput): (Primitive & UnionToIntersection : never : never>); /** * Checks if a value satisfies all constraints of the set (type guard). * * ```ts * const maybe = UsernameConstraints.createWithUnknown(unknownValue); * ``` * */ is(input: GenericInput): input is Extract & UnionToIntersection : never : never>)>; /** * Returns a constraint handler by name from the constraints set. * * ```ts * * UsernameConstraints.is(value); // type guard * ``` * */ getConstraint(name: GenericConstraintName): Extract>; } declare const CreateConstraintsSetError_base: new (params: { "@DuplojsUtilsError/create-constraint-set-error"?: unknown; }, parentParams: readonly [message?: string | undefined, options?: ErrorOptions | undefined]) => Error & Kind, unknown> & Kind, unknown>; export declare class CreateConstraintsSetError extends CreateConstraintsSetError_base { constraintName: string; data: unknown; dataParserError: DDataParser.DataParserError; constructor(constraintName: string, data: unknown, dataParserError: DDataParser.DataParserError); } export type ConstraintSetInputConstraint = (ConstraintHandler[]> | ConstraintsSetHandler[]>[]>); export type ConstraintsHandlerArguments = (ConstraintSetInputConstraint | readonly [ ConstraintSetInputConstraint, ...ConstraintSetInputConstraint[] ]); export type ExtractConstraintSetConstraintHandlers | readonly ConstraintSetInputConstraint[] | readonly [])> = IsEqual extends true ? readonly [] : GenericConstraint extends ConstraintHandler ? readonly [GenericConstraint] : GenericConstraint extends ConstraintsSetHandler ? ExtractConstraintSetConstraintHandlers : GenericConstraint extends readonly [] ? GenericConstraint : GenericConstraint extends readonly [ infer InferredFirst extends ConstraintSetInputConstraint, ...infer InferredRest extends readonly ConstraintSetInputConstraint[] ] ? ExtractConstraintSetConstraintHandlers extends infer InferredResultRest extends readonly any[] ? readonly [ ...ExtractConstraintSetConstraintHandlers, ...InferredResultRest ] : never : never; /** * Creates a constraints set handler to apply multiple constraints to a primitive. * * **Supported call styles:** * - Classic: `createConstraintsSet(primitiveHandler, constraints)` -> returns a handler * * A constraints set validates input with the primitive DataParser, applies all constraint checkers, and tags the value with each constraint name. Use it to compose multiple constraints once and reuse the handler. * * On validation failure, `create` and `createWithUnknown` return `Left<"createConstraintsSetError", ConstraintError<...>>`. The error contains the failing `dataParserError` and a `constraintName` resolved from the checker source. * * ```ts * const UsernameConstraints = C.createConstraintsSet( * C.String, * [C.StringMin(3), C.StringMax(16)], * ); * * const result = UsernameConstraints.create("Ada"); * * if (E.isRight(result)) { * // result: E.Right< * // "createConstraintsSet", * // C.Primitive<"Ada"> & C.GetConstraints * // > * } * * const value = UsernameConstraints.createOrThrow("Nova"); * // value: C.Primitive<"Nova"> & C.GetConstraints * * const primitive = C.String.createOrThrow("Kit"); * UsernameConstraints.create(primitive); * * const unknownValue: unknown = "Sam"; * const maybe = UsernameConstraints.createWithUnknown(unknownValue); * const strictValue = UsernameConstraints.createWithUnknownOrThrow(unknownValue); * * UsernameConstraints.is(value); // type guard * ``` * * @remarks * - You can pass a single constraint handler, a constraints set handler, or a tuple mixing constraints and constraints sets. * - Constraints sets are expanded internally, so their contained constraints are applied in declaration order. * - The handler accepts both raw values and primitives. * - When a constraint checker fails, `constraintName` points to the constraint that owns the checker, including constraints expanded from nested sets. * - When primitive parsing fails before any constraint checker is responsible, `constraintName` falls back to the first constraint name in the set. * * @see https://utils.duplojs.dev/en/v1/api/clean/constraints * * @namespace C * */ export declare function createConstraintsSet = never, GenericConstraints extends readonly ConstraintHandler[] = ExtractConstraintSetConstraintHandlers, GenericPrimitiveRefinedValue extends EligiblePrimitive = DDataParser.ApplyRefinementOfChecker>(primitiveHandler: PrimitiveHandler, constraint: GenericConstrainHandler): ConstraintsSetHandler extends true ? IsEqual extends true ? never : GenericPrimitiveValue : GenericPrimitiveInput>; export declare namespace createConstraintsSet { var overrideHandler: import("../..").OverrideHandler>; } export type GetConstraints, GenericValue extends DDataParser.Output = never> = Extract : never : never> & (IsNever extends true ? unknown : Primitive) : never, any>; export {};