UNPKG

4.04 kBTypeScriptView Raw
1import { StructSchema } from './utils';
2import { StructError, Failure } from './error';
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): asserts value is T;
28 /**
29 * Create a value with the struct's coercion logic, then validate it.
30 */
31 create(value: unknown): 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.
39 */
40 mask(value: unknown): T;
41 /**
42 * Validate a value with the struct's validation logic, returning a tuple
43 * representing the result.
44 *
45 * You may optionally pass `true` for the `withCoercion` argument to coerce
46 * the value before attempting to validate it. If you do, the result will
47 * contain the coerced result when successful.
48 */
49 validate(value: unknown, options?: {
50 coerce?: boolean;
51 }): [StructError, undefined] | [undefined, T];
52}
53/**
54 * Assert that a value passes a struct, throwing if it doesn't.
55 */
56export declare function assert<T, S>(value: unknown, struct: Struct<T, S>): asserts value is T;
57/**
58 * Create a value with the coercion logic of struct and validate it.
59 */
60export declare function create<T, S>(value: unknown, struct: Struct<T, S>): T;
61/**
62 * Mask a value, returning only the subset of properties defined by a struct.
63 */
64export declare function mask<T, S>(value: unknown, struct: Struct<T, S>): T;
65/**
66 * Check if a value passes a struct.
67 */
68export declare function is<T, S>(value: unknown, struct: Struct<T, S>): value is T;
69/**
70 * Validate a value against a struct, returning an error if invalid, or the
71 * value (with potential coercion) if valid.
72 */
73export declare function validate<T, S>(value: unknown, struct: Struct<T, S>, options?: {
74 coerce?: boolean;
75 mask?: boolean;
76}): [StructError, undefined] | [undefined, T];
77/**
78 * A `Context` contains information about the current location of the
79 * validation inside the initial input value.
80 */
81export declare type Context = {
82 branch: Array<any>;
83 path: Array<any>;
84};
85/**
86 * A type utility to extract the type from a `Struct` class.
87 */
88export declare type Infer<T extends Struct<any, any>> = T['TYPE'];
89/**
90 * A type utility to describe that a struct represents a TypeScript type.
91 */
92export declare type Describe<T> = Struct<T, StructSchema<T>>;
93/**
94 * A `Result` is returned from validation functions.
95 */
96export declare type Result = boolean | string | Partial<Failure> | Iterable<boolean | string | Partial<Failure>>;
97/**
98 * A `Coercer` takes an unknown value and optionally coerces it.
99 */
100export declare type Coercer<T = unknown> = (value: T, context: Context) => unknown;
101/**
102 * A `Validator` takes an unknown value and validates it.
103 */
104export declare type Validator = (value: unknown, context: Context) => Result;
105/**
106 * A `Refiner` takes a value of a known type and validates it against a further
107 * constraint.
108 */
109export declare type Refiner<T> = (value: T, context: Context) => Result;
110//# sourceMappingURL=struct.d.ts.map
\No newline at end of file