1 | import { StructSchema } from './utils.js';
|
2 | import { StructError, Failure } from './error.js';
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | export 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 | */
|
60 | export 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 | */
|
64 | export 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 | */
|
68 | export declare function mask<T, S>(value: unknown, struct: Struct<T, S>, message?: string): T;
|
69 | /**
|
70 | * Check if a value passes a struct.
|
71 | */
|
72 | export 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 | */
|
77 | export 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 | */
|
89 | export type Context = {
|
90 | branch: Array<any>;
|
91 | path: Array<any>;
|
92 | mask?: boolean;
|
93 | };
|
94 |
|
95 |
|
96 |
|
97 | export type Infer<T extends Struct<any, any>> = T['TYPE'];
|
98 |
|
99 |
|
100 |
|
101 | export type Describe<T> = Struct<T, StructSchema<T>>;
|
102 |
|
103 |
|
104 |
|
105 | export type Result = boolean | string | Partial<Failure> | Iterable<boolean | string | Partial<Failure>>;
|
106 |
|
107 |
|
108 |
|
109 | export type Coercer<T = unknown> = (value: T, context: Context) => unknown;
|
110 |
|
111 |
|
112 |
|
113 | export type Validator = (value: unknown, context: Context) => Result;
|
114 |
|
115 |
|
116 |
|
117 |
|
118 | export type Refiner<T> = (value: T, context: Context) => Result;
|
119 |
|
\ | No newline at end of file |