UNPKG

6.38 kBTypeScriptView Raw
1import { Struct, StructType, StructContext } from './struct';
2declare type StructRecord<T> = Record<string, Struct<T>>;
3declare type StructTuple<T> = {
4 [K in keyof T]: Struct<T[K]>;
5};
6/**
7 * Validate any value.
8 */
9export declare function any(): Struct<any>;
10/**
11 * Validate that an array of values of a specific type.
12 */
13export declare function array<T>(Element: Struct<T>): Struct<T[]>;
14/**
15 * Validate that boolean values.
16 */
17export declare function boolean(): Struct<boolean>;
18/**
19 * Augment a `Struct` to add an additional coercion step to its input.
20 */
21export declare function coercion<T>(struct: Struct<T>, coercer: Struct<T>['coercer']): Struct<T>;
22/**
23 * Validate that `Date` values.
24 *
25 * Note: this also ensures that the value is *not* an invalid `Date` object,
26 * which can occur when parsing a date fails but still returns a `Date`.
27 */
28export declare function date(): Struct<Date>;
29/**
30 * Augment a struct to coerce a default value for missing values.
31 *
32 * Note: You must use `coerce(value, Struct)` on the value before validating it
33 * to have the value defaulted!
34 */
35export declare function defaulted<T>(S: Struct<T>, fallback: any, strict?: true): Struct<T>;
36/**
37 * Validate that a value dynamically, determing which struct to use at runtime.
38 */
39export declare function dynamic<T>(fn: (value: unknown, ctx: StructContext) => Struct<T>): Struct<T>;
40/**
41 * Validate that a value against a set of potential values.
42 */
43export declare function enums<T>(values: T[]): Struct<T>;
44/**
45 * Validate that a value is an instance of a class.
46 */
47export declare function instance<T extends {
48 new (...args: any): any;
49}>(Class: T): Struct<InstanceType<T>>;
50/**
51 * Validate that a value is an integer.
52 */
53export declare function integer(): Struct<number>;
54/**
55 * Validate that a value matches all of a set of structs.
56 */
57export declare function intersection<A>(Structs: StructTuple<[A]>): Struct<A>;
58export declare function intersection<A, B>(Structs: StructTuple<[A, B]>): Struct<A & B>;
59export declare function intersection<A, B, C>(Structs: StructTuple<[A, B, C]>): Struct<A & B & C>;
60export declare function intersection<A, B, C, D>(Structs: StructTuple<[A, B, C, D]>): Struct<A & B & C & D>;
61export declare function intersection<A, B, C, D, E>(Structs: StructTuple<[A, B, C, D, E]>): Struct<A & B & C & D & E>;
62/**
63 * Validate a value lazily, by constructing the struct right before the first
64 * validation. This is useful for cases where you want to have self-referential
65 * structs for nested data structures.
66 */
67export declare function lazy<T>(fn: () => Struct<T>): Struct<T>;
68/**
69 * Augment a string or array struct to constrain its length to being between a
70 * minimum and maximum size.
71 */
72export declare function length<T extends string | any[]>(S: Struct<T>, min: number, max?: number): Struct<T>;
73/**
74 * Validate that a value is a specific constant.
75 */
76export declare function literal<T>(constant: T): Struct<T>;
77/**
78 * Validate that a value is a map with specific key and value entries.
79 */
80export declare function map<K, V>(Structs: StructTuple<[K, V]>): Struct<Map<K, V>>;
81/**
82 * Validate that a value always fails.
83 */
84export declare function never(): Struct<never>;
85/**
86 * Validate that a value is a number.
87 */
88export declare function number(): Struct<number>;
89/**
90 * Validate that an object with specific entry values.
91 */
92export declare function object<V extends StructRecord<any>>(Structs: V): Struct<{
93 [K in keyof V]: StructType<V[K]>;
94}>;
95/**
96 * Augment a struct to make it accept optionally accept `undefined` values.
97 */
98export declare function optional<T>(S: Struct<T>): Struct<T | undefined>;
99/**
100 * Validate that a partial object with specific entry values.
101 */
102export declare function partial<V extends StructRecord<any>>(Structs: V): Struct<{
103 [K in keyof V]?: StructType<V[K]>;
104}>;
105/**
106 * Refine a string struct to match a specific regexp pattern.
107 */
108export declare function pattern<T extends string>(S: Struct<T>, regexp: RegExp): Struct<T>;
109/**
110 * Validate that a value is a record with specific key and
111 * value entries.
112 */
113export declare function record<K extends string | number, V>(Structs: StructTuple<[K, V]>): Struct<Record<K, V>>;
114/**
115 * Augment a `Struct` to add an additional refinement to the validation.
116 */
117export declare function refinement<T>(struct: Struct<T>, type: string, refiner: Struct<T>['refiner']): Struct<T>;
118/**
119 * Validate that a set of values matches a specific type.
120 */
121export declare function set<T>(Element: Struct<T>): Struct<Set<T>>;
122/**
123 * Validate that a value is a string.
124 */
125export declare function string(): Struct<string>;
126/**
127 * Coerce a string value to ensure it is trimmed.
128 */
129export declare function trimmed<T extends string>(S: Struct<T>): Struct<T>;
130/**
131 * Validate that a value is a tuple with entries of specific types.
132 */
133export declare function tuple<A>(Elements: StructTuple<[A]>): Struct<[A]>;
134export declare function tuple<A, B>(Elements: StructTuple<[A, B]>): Struct<[A, B]>;
135export declare function tuple<A, B, C>(Elements: StructTuple<[A, B, C]>): Struct<[A, B, C]>;
136export declare function tuple<A, B, C, D>(Elements: StructTuple<[A, B, C, D]>): Struct<[A, B, C, D]>;
137export declare function tuple<A, B, C, D, E>(Elements: StructTuple<[A, B, C, D, E]>): Struct<[A, B, C, D, E]>;
138/**
139 * Validate that a value matches a specific strutural interface, like the
140 * structural typing that TypeScript uses.
141 */
142export declare function type<V extends StructRecord<any>>(Structs: V): Struct<{
143 [K in keyof V]: StructType<V[K]>;
144}>;
145/**
146 * Validate an unknown value, accepting anything but not narrowing the type.
147 */
148export declare function unknown(): Struct<unknown>;
149/**
150 * Validate that a value is one of a set of types.
151 */
152export declare function union<A>(Structs: StructTuple<[A]>): Struct<A>;
153export declare function union<A, B>(Structs: StructTuple<[A, B]>): Struct<A | B>;
154export declare function union<A, B, C>(Structs: StructTuple<[A, B, C]>): Struct<A | B | C>;
155export declare function union<A, B, C, D>(Structs: StructTuple<[A, B, C, D]>): Struct<A | B | C | D>;
156export declare function union<A, B, C, D, E>(Structs: StructTuple<[A, B, C, D, E]>): Struct<A | B | C | D | E>;
157export {};