UNPKG

4.55 kBTypeScriptView Raw
1import { StructSchema } from './utils.js';
2import { StructError, Failure } from './error.js';
3/**
4 * `Struct` objects encapsulate the validation logic for a specific type of
5 * values. Once constructed, you use the `assert`, `is` or `validate` helpers to
6 * validate unknown input data against the struct.
7 */
8export declare class Struct<T = unknown, S = unknown> {
9 readonly TYPE: T;
10 type: string;
11 schema: S;
12 coercer: (value: unknown, context: Context) => unknown;
13 validator: (value: unknown, context: Context) => Iterable<Failure>;
14 refiner: (value: T, context: Context) => Iterable<Failure>;
15 entries: (value: unknown, context: Context) => Iterable<[string | number, unknown, Struct<any> | Struct<never>]>;
16 constructor(props: {
17 type: string;
18 schema: S;
19 coercer?: Coercer;
20 validator?: Validator;
21 refiner?: Refiner<T>;
22 entries?: Struct<T, S>['entries'];
23 });
24 /**
25 * Assert that a value passes the struct's validation, throwing if it doesn't.
26 */
27 assert(value: unknown, message?: string): asserts value is T;
28 /**
29 * Create a value with the struct's coercion logic, then validate it.
30 */
31 create(value: unknown, message?: string): T;
32 /**
33 * Check if a value passes the struct's validation.
34 */
35 is(value: unknown): value is T;
36 /**
37 * Mask a value, coercing and validating it, but returning only the subset of
38 * properties defined by the struct's schema. Masking applies recursively to
39 * props of `object` structs only.
40 */
41 mask(value: unknown, message?: string): T;
42 /**
43 * Validate a value with the struct's validation logic, returning a tuple
44 * representing the result.
45 *
46 * You may optionally pass `true` for the `coerce` argument to coerce
47 * the value before attempting to validate it. If you do, the result will
48 * contain the coerced result when successful. Also, `mask` will turn on
49 * masking of the unknown `object` props recursively if passed.
50 */
51 validate(value: unknown, options?: {
52 coerce?: boolean;
53 mask?: boolean;
54 message?: string;
55 }): [StructError, undefined] | [undefined, T];
56}
57/**
58 * Assert that a value passes a struct, throwing if it doesn't.
59 */
60export declare function assert<T, S>(value: unknown, struct: Struct<T, S>, message?: string): asserts value is T;
61/**
62 * Create a value with the coercion logic of struct and validate it.
63 */
64export declare function create<T, S>(value: unknown, struct: Struct<T, S>, message?: string): T;
65/**
66 * Mask a value, returning only the subset of properties defined by a struct.
67 */
68export declare function mask<T, S>(value: unknown, struct: Struct<T, S>, message?: string): T;
69/**
70 * Check if a value passes a struct.
71 */
72export declare function is<T, S>(value: unknown, struct: Struct<T, S>): value is T;
73/**
74 * Validate a value against a struct, returning an error if invalid, or the
75 * value (with potential coercion) if valid.
76 */
77export declare function validate<T, S>(value: unknown, struct: Struct<T, S>, options?: {
78 coerce?: boolean;
79 mask?: boolean;
80 message?: string;
81}): [StructError, undefined] | [undefined, T];
82/**
83 * A `Context` contains information about the current location of the
84 * validation inside the initial input value. It also carries `mask`
85 * since it's a run-time flag determining how the validation was invoked
86 * (via `mask()` or via `validate()`), plus it applies recursively
87 * to all of the nested structs.
88 */
89export type Context = {
90 branch: Array<any>;
91 path: Array<any>;
92 mask?: boolean;
93};
94/**
95 * A type utility to extract the type from a `Struct` class.
96 */
97export type Infer<T extends Struct<any, any>> = T['TYPE'];
98/**
99 * A type utility to describe that a struct represents a TypeScript type.
100 */
101export type Describe<T> = Struct<T, StructSchema<T>>;
102/**
103 * A `Result` is returned from validation functions.
104 */
105export type Result = boolean | string | Partial<Failure> | Iterable<boolean | string | Partial<Failure>>;
106/**
107 * A `Coercer` takes an unknown value and optionally coerces it.
108 */
109export type Coercer<T = unknown> = (value: T, context: Context) => unknown;
110/**
111 * A `Validator` takes an unknown value and validates it.
112 */
113export type Validator = (value: unknown, context: Context) => Result;
114/**
115 * A `Refiner` takes a value of a known type and validates it against a further
116 * constraint.
117 */
118export type Refiner<T> = (value: T, context: Context) => Result;
119//# sourceMappingURL=struct.d.ts.map
\No newline at end of file