import { type Unwrap, type Kind, type WrappedValue, type FixDeepFunctionInfer, type IsEqual, type IsNever, type NeverCoalescing } from "../.."; import { type Primitive, type EligiblePrimitive, type PrimitiveHandler, type PrimitiveHandlers } from "../primitive"; import * as DArray from "../../array"; import * as DEither from "../../either"; import type * as DDataParser from "../../dataParser"; export declare const constrainedTypeKind: import("../..").KindHandler>>; export interface ConstrainedType extends Kind>, WrappedValue { } export interface ConstraintError { constraintName: GenericName; dataParserError: DDataParser.DataParserError; } export declare const constraintHandlerKind: import("../..").KindHandler>; export interface ConstraintHandler extends Kind { /** * The constraint name used as the internal marker (for example "email"). * */ readonly name: GenericName; /** * @deprecated */ readonly checkers: GenericCheckers; /** * @deprecated */ readonly primitiveHandler: PrimitiveHandler; readonly internal: { /** * The DataParser with the constraint checkers applied. * */ readonly dataParser: DDataParser.DataParser; /** * The primitive handler on which the constraint applies (String, Number, etc.). * */ readonly primitiveHandler: PrimitiveHandler & PrimitiveHandlers; /** * The list of DataParser checkers used to validate the primitive. * */ readonly checkers: GenericCheckers; /** * The constraint kind metadata used to tag constrained values. * */ readonly constraintKindValue: Record; }; /** * Creates a constrained value and returns an Either. * On validation failure, the left value contains the constraint name and the underlying DataParser error. * * ```ts * const result = Between1And10.create(5); * * if (E.isRight(result)) { * // result : E.Right<"createConstrainedType", C.ConstrainedType<"between-1-10", 5>> * } * ``` * */ create(data: GenericData): (DEither.Right<"createConstrainedType", ConstrainedType> | DEither.Left<"createConstrainedTypeError", ConstraintError>); create>(data: GenericPrimitive): (DEither.Right<"createConstrainedType", (GenericPrimitive & ConstrainedType>)> | DEither.Left<"createConstrainedTypeError", ConstraintError>); /** * Creates a constrained value and throws on error. * * ```ts * const value = Between1And10.createOrThrow(7); * // value: C.ConstrainedType<"between-1-10", 7> * ``` * */ createOrThrow(data: GenericData): ConstrainedType; createOrThrow>(data: GenericPrimitive): (GenericPrimitive & ConstrainedType>); /** * Creates a constrained value from the handler's wider input type and returns an Either. * * This is useful when the output type is refined, but the source data is only known as the primitive input type. * * ```ts * const largeValue = Between1And10.createWithLarge(8); * // E.Left<"createConstrainedTypeError", C.ConstraintError<"between-1-10">> * // | E.Right<"createConstrainedType", C.ConstrainedType<"between-1-10", number>> * ``` * */ createWithLarge(data: NeverCoalescing): (DEither.Right<"createConstrainedType", ConstrainedType> | DEither.Left<"createConstrainedTypeError", ConstraintError>); /** * Creates a constrained value from the handler's wider input type and throws on error. * * This is useful when the output type is refined, but the source data is only known as the primitive input type. * * ```ts * const strictLargeValue = Between1And10.createWithLargeOrThrow(8); * ``` * */ createWithLargeOrThrow(data: NeverCoalescing): ConstrainedType; /** * Creates a constrained value from an unknown input and returns an Either. * * ```ts * const unknownValue: unknown = 9; * const maybe = Between1And10.createWithUnknown(unknownValue); * ``` * */ createWithUnknown(data: GenericData): (DEither.Right<"createConstrainedType", ConstrainedType> | DEither.Left<"createConstrainedTypeError", ConstraintError>); /** * Creates a constrained value from an unknown input and throws on error. * * ```ts * const strictValue = Between1And10.createWithUnknownOrThrow(unknownValue); * ``` * */ createWithUnknownOrThrow(data: GenericData): ConstrainedType; /** * Checks if a value carries this constraint (type guard). * * ```ts * Between1And10.is(value); // type guard * ``` * */ is(input: GenericInput): input is Extract>; } declare const CreateConstrainedTypeError_base: new (params: { "@DuplojsUtilsError/create-constrained-type-error"?: unknown; }, parentParams: readonly [message?: string | undefined, options?: ErrorOptions | undefined]) => Error & Kind, unknown> & Kind, unknown>; export declare class CreateConstrainedTypeError extends CreateConstrainedTypeError_base { constraintName: string; data: unknown; dataParserError: DDataParser.DataParserError; constructor(constraintName: string, data: unknown, dataParserError: DDataParser.DataParserError); } /** * Creates a constraint handler to enforce a rule on a primitive. * * **Supported call styles:** * - Classic: `createConstraint(name, primitive, checker)` -> returns a handler * * Constraints cover rules that typing cannot express (format, range, custom checks). The handler validates input, tags the value with the constraint name, and can be reused as a contract or plugged into a NewType. * * ```ts * const Between1And10 = C.createConstraint( * "between-1-10", * C.Number, * [ * DP.checkerNumberMin(1), * DP.checkerNumberMax(10), * ], * ); * * const result = Between1And10.create(5); * * if (E.isRight(result)) { * // result : E.Right<"createConstrainedType", C.ConstrainedType<"between-1-10", 5>> * } * * const value = Between1And10.createOrThrow(7); * // value: C.ConstrainedType<"between-1-10", 7> * * const primitive = C.Number.createOrThrow(3); * Between1And10.create(primitive); * * Between1And10.is(value); // type guard * * const unknownValue: unknown = 9; * const maybe = Between1And10.createWithUnknown(unknownValue); * * const strictValue = Between1And10.createWithUnknownOrThrow(unknownValue); * * ``` * * @remarks * - You can pass multiple DataParser checkers by providing an array. * - The handler accepts both raw values and primitive values. * * @see https://utils.duplojs.dev/en/v1/api/clean/constraints * * @namespace C * */ export declare function createConstraint | readonly [ DDataParser.DataParserChecker, ...DDataParser.DataParserChecker[] ]) = never, GenericPrimitiveRefinedValue extends EligiblePrimitive = DDataParser.ApplyRefinementOfChecker[number]>>(name: GenericName, primitiveHandler: PrimitiveHandler, checker: FixDeepFunctionInfer<(DDataParser.DataParserChecker | readonly [ DDataParser.DataParserChecker, ...DDataParser.DataParserChecker[] ]), GenericChecker>): ConstraintHandler, IsNever extends true ? IsEqual extends true ? never : GenericPrimitiveValue : GenericPrimitiveInput>; export declare namespace createConstraint { var overrideHandler: import("../..").OverrideHandler[], unknown>>; } export type GetConstraint = DDataParser.Output> = Extract, any>; export {};