Skip to main content
Module

x/abstruct/factories.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.7/factories.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/factories.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
A extends In
In2
A2 extends In2

Type Parameters

In
A extends In
In2
A2 extends In2
In3
A3 extends In3

Type Parameters

In
A extends In
In2
A2 extends In2
In3
A3 extends In3
In4
A4 extends In4

Parameters

v1: Validator<In, A>
v2: Validator<In2 | A, A2>
v3: Validator<In3 | A & A2, A3>
v4: Validator<In4 | A & A2 & A3, A4>

Type Parameters

In
A extends In
In2
A2 extends In2
In3
A3 extends In3
In4
A4 extends In4
In5
A5 extends In5

Parameters

v1: Validator<In, A>
v2: Validator<In2 | A, A2>
v3: Validator<In3 | A & A2, A3>
v4: Validator<In4 | A & A2 & A3, A4>
v5: Validator<In5 |
& A
& A2
& A3
& A4
, A5>