Provides ready-to-use functionality. They are bound with default failure messages.
They are also exported in the form of closures or, objects.
Binding includes the following interfaces.
API to perform Validation. See Validator for details.
Objects with the Expectation
interface can bind validation failure messages.
Static message:
import { string } from "https://deno.land/x/abstruct@$VERSION/bindings.ts";
const StringValidator = string.expect("should be string");
Dynamic message:
The context is different for each object.
import { string } from "https://deno.land/x/abstruct@$VERSION/bindings.ts";
const StringValidator = string.expect((context) =>
`should be string, but ${typeof context.input}`
);
Objects implementing this interface also come with default messages.
Name | Closure / Object | Validator | Expectation |
---|---|---|---|
type | Closure | TypeValidator | ✅ |
string | Object | TypeValidator | ✅ |
number | Object | TypeValidator | ✅ |
bigint | Object | TypeValidator | ✅ |
boolean | Object | TypeValidator | ✅ |
symbol | Object | TypeValidator | ✅ |
instance | Closure | InstanceValidator | ✅ |
props | Closure | PropertiesValidator | |
optional | Closure | OptionalValidator | |
enumerator | Closure | EnumValidator | ✅ |
nullish | Object | NullishValidator | ✅ |
propKey | Closure | PropertyKeyValidator | |
propValue | Closure | PropertyValueValidator | |
and | Closure | AndValidator | ✅ |
or | Closure | OrValidator | ✅ |
eq | Closure | EqualityValidator | ✅ |
ne | Closure | InequalityValidator | ✅ |
gt | Closure | GreaterThanValidator | ✅ |
gte | Closure | GreaterThanOrEqualValidator | ✅ |
lt | Closure | LessThanValidator | ✅ |
lte | Closure | LessThanOrEqualValidator | ✅ |
between | Closure | RangeValidator | ✅ |
not | Closure | NotValidator | ✅ |
has | Closure | InValidator | ✅ |
pattern | Closure | PatternValidator | ✅ |
item | Closure | ItemValidator | |
count | Closure | CountValidator | ✅ |
maxCount | Closure | MaxCountValidator | ✅ |
minCount | Closure | MinCountValidator | ✅ |
single | Object | SingleValidator | ✅ |
empty | Object | EmptyValidator | ✅ |
nonEmpty | Object | NonEmptyValidator | ✅ |
unique | Object | UniqueValidator | ✅ |
fixedArray | Closure | FixedArrayValidator | |
float | Object | FloatValidator | ✅ |
int | Object | IntegerValidator | ✅ |
positive | Object | PositiveNumberValidator | ✅ |
negative | Object | NegativeNumberValidator | ✅ |
nonPositive | Object | NonPositiveNumberValidator | ✅ |
nonNegative | Object | NonNegativeNumberValidator | ✅ |
int8 | Object | - | ✅ |
int16 | Object | - | ✅ |
int32 | Object | - | ✅ |
uint8 | Object | - | ✅ |
uint16 | Object | - | ✅ |
uint32 | Object | - | ✅ |
validDate | Object | ValidDateValidator | ✅ |
Validator factory for JavaScript data type. The difference with typeof
operator is that "object"
does not match null
.
import { type } from "https://deno.land/x/abstruct@$VERSION/bindings.ts";
const validator = type("object");
String validator.
Number validator.
Bigint validator.
Boolean validator.
Symbol validator.
Validator factory equivalent to the instanceof
operator.
import { instance } from "https://deno.land/x/abstruct@$VERSION/bindings.ts";
const validator = instance(Array);
Factory for properties validator.
import {
number,
props,
string,
} from "https://deno.land/x/abstruct@$VERSION/mod.ts";
const User = props({ name: string, age: number });
Factory for optional properties validator.
import {
number,
optional,
string,
} from "https://deno.land/x/abstruct@$VERSION/mod.ts";
const Profile = optional({ greeting: string, hobby: string });
Factory for enumerator validator.
import { enumerator } from "https://deno.land/x/abstruct@$VERSION/bindings.ts";
const validator = enumerator(0, 1, 2, 3);
const validator2 = enumerator("Red", "Yellow", "Green");
Nullish(null
or undefined
) validator.
Factory for property key validator.
import {
propKey,
type Validator,
} from "https://deno.land/x/abstruct@$VERSION/mod.ts";
declare const validator: Validator<string>;
const keyValidator = propKey(validator);
Factory for property value validator.
import {
propValue,
type Validator,
} from "https://deno.land/x/abstruct@$VERSION/mod.ts";
declare const validator: Validator;
const valueValidator = propValue(validator);
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.
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));
note: int8 provides.
Composition of and
is type-safe.
Each validator is corrected to satisfy the narrowed type from the previous validators.
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);
The following example shows type error because it is insufficient narrowing.
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<number, -1 | 0 | 1>;
declare const v2: Validator<0 | 1, 0>;
// @ts-expect-error v2 should type-compatible with `Validator<-1 | 0 | 1>`.
const validator = and(v1, v2);
The following example is correct because it is faithful to Logical AND narrowing.
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<unknown, number>;
const validator = and(v1, v2);
assertType<Has<typeof validator, Validator<unknown, never>>>(true);
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
.
Factory for validator composer like Logical OR.
import {
or,
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;
declare const v2: Validator;
declare const v3: Validator;
const validator = or(v1, v2, v3);
If the validators are not type-compatible with each other, generics must be specified.
import {
or,
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<unknown, number>;
const validator = or<unknown, string | number>(v1, v2);
assertType<Has<typeof validator, Validator<unknown, string | number>>>(true);
For more information, see Specifying Type Arguments.
Validator factory equivalent to strict equality(===
) operator.
import { eq } from "https://deno.land/x/abstruct@$VERSION/bindings.ts";
const validator = eq(0);
Factory for validator equivalent to strict inequality(!==
) operator.
import { ne } from "https://deno.land/x/abstruct@$VERSION/bindings.ts";
const validator = ne(0);
Factory for validator equivalent to greater than(<
) operator.
import { gt } from "https://deno.land/x/abstruct@$VERSION/bindings.ts";
const validator = gt(8);
Factory for validator equivalent to greater than or equal(<=
) operator.
import { gte } from "https://deno.land/x/abstruct@$VERSION/bindings.ts";
const validator = gte(8);
Factory for validator equivalent to less than(>
) operator.
import { lt } from "https://deno.land/x/abstruct@$VERSION/bindings.ts";
const validator = lt(256);
Factory for validator equivalent to less than or equal to (>=
) operator.
import { lte } from "https://deno.land/x/abstruct@$VERSION/bindings.ts";
const validator = lte(255);
Factory for range validator.
import { between } from "https://deno.land/x/abstruct@$VERSION/bindings.ts";
const numberRangeValidator = between(0, 255);
const dateRangeValidator = between(new Date("1970/1/1"), new Date("2038/1/19"));
Factory for validator inversion.
import {
not,
type Validator,
} from "https://deno.land/x/abstruct@$VERSION/mod.ts";
declare const validator: Validator;
const inversionValidator = not(validator);
Factory for existence of property validator.
import { has } from "https://deno.land/x/abstruct@$VERSION/bindings.ts";
const validator = has("prop");
Factory for regex pattern validator.
import { pattern } from "https://deno.land/x/abstruct@$VERSION/bindings.ts";
const validator = pattern(/^\d*$/);
Factory for item validator. It checks each item of items.
import {
item,
type Validator,
} from "https://deno.land/x/abstruct@$VERSION/mod.ts";
declare const validator: Validator;
const itemValidator = item(validator);
Factory for count validator. It checks count(size, length) of items.
import { count } from "https://deno.land/x/abstruct@$VERSION/bindings.ts";
const validator = count(5);
Factory for max count validator. It checks items count is less than or equal to
limit
.
import { maxCount } from "https://deno.land/x/abstruct@$VERSION/bindings.ts";
declare const limit: number;
const validator = maxCount(limit);
Factory for min count validator. It checks items count is greater than or equal
to limit
.
import { minCount } from "https://deno.land/x/abstruct@$VERSION/bindings.ts";
declare const limit: number;
const validator = minCount(limit);
Single validator. It checks items is single.
Empty validator. It checks the items is empty.
Non-Empty validator. It checks items is non-empty.
Unique validator. It checks the each item is unique.
Factory for fixed array validator. It checks each item passes each Validator
.
import {
fixedArray,
type Validator,
} from "https://deno.land/x/abstruct@$VERSION/mod.ts";
declare const v1: Validator;
declare const v2: Validator;
const validator = fixedArray(v1, v2);
Float validator.
Integer validator.
Positive number validator.
Negative number validator.
Non-positive number validator.
Non-negative number validator.
Integer in the range -127 ~ 128 validator.
Integer in the range -32768 ~ 32767 validator.
Integer in the range -2147483648 ~ 2147483647 validator.
Integer in the range 0 ~ 255 validator.
Integer in the range 0 ~ 65535 validator.
Integer in the range 0 ~ 4294967295 validator.
Valid Date
validator.