Skip to main content
Module

x/abstruct/mod.ts>and

Abstract structure for JavaScript data validation
Go to Latest
function and
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

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

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.

Type Parameters

In
RIn extends In
In2
RIn2 extends In2

Parameters

v1: Readonly<Validator<In, RIn>>
v2: Readonly<Validator<In2 | RIn, RIn2>>

Type Parameters

In
RIn extends In
In2
RIn2 extends In2
In3
RIn3 extends In3

Parameters

v1: Readonly<Validator<In, RIn>>
v2: Readonly<Validator<In2 | RIn, RIn2>>
v3: Readonly<Validator<In3 | RIn & RIn2, RIn3>>

Type Parameters

In
RIn extends In
In2
RIn2 extends In2
In3
RIn3 extends In3
In4
RIn4 extends In4

Parameters

v1: Readonly<Validator<In, RIn>>
v2: Readonly<Validator<In2 | RIn, RIn2>>
v3: Readonly<Validator<In3 | RIn & RIn2, RIn3>>
v4: Readonly<Validator<In4 | RIn & RIn2 & RIn3, RIn4>>

Type Parameters

In
RIn extends In
In2
RIn2 extends In2
In3
RIn3 extends In3
In4
RIn4 extends In4
In5
RIn5 extends In5

Parameters

v1: Readonly<Validator<In, RIn>>
v2: Readonly<Validator<In2 | RIn, RIn2>>
v3: Readonly<Validator<In3 | RIn & RIn2, RIn3>>
v4: Readonly<Validator<In4 | RIn & RIn2 & RIn3, RIn4>>
v5: Readonly<Validator<In5 |
& RIn
& RIn2
& RIn3
& RIn4
, RIn5>>