import { and } from "https://deno.land/x/abstruct@1.0.0-beta.12/mod.ts";
Factory for validator composer like Logical AND.
and
composes multiple validators and creates a new validator. The composed
validator executes the validator from left to right, just like the Logical AND
operator. If the validation fails en route, the evaluation stops there.
Examples
Example 1
Example 1
import {
and,
between,
gte,
int,
lte,
} from "https://deno.land/x/abstruct@$VERSION/bindings.ts";
const _int8 = and(int, lte(128), gte(-127));
const __int8 = and(int, between(-127, 128));
Type-narrowing
Composition of and
is type-safe.
Each validator is corrected to satisfy the narrowed type from the previous validators.
Example 2
Example 2
import {
and,
type Validator,
} from "https://deno.land/x/abstruct@$VERSION/mod.ts";
import { assertType, type Has } from "https://deno.land/std/testing/types.ts";
declare const v1: Validator<unknown, string>;
declare const v2: Validator<string, "a" | "b">;
declare const v3: Validator<"a" | "b" | "c", "a">;
const validator = and(v1, v2, v3);
assertType<Has<typeof validator, Validator<unknown, "a">>>(true);
Limit the number of arguments
The number of arguments is limited by overloading to achieve this.
Currently it accepts up to 5 arguments.
This limit is based on the strict Function.bind
type signature.
If more than that is needed, you must nest and
.
Returns
AndValidator<In, RIn & RIn2> & Expectation<{ input: In; }>