UNPKG

5.16 kBTypeScriptView Raw
1import { Struct, StructType, StructContext } from './struct';
2import { StructRecord, StructTuple } from './utils';
3/**
4 * Validate any value.
5 */
6export declare function any(): Struct<any>;
7/**
8 * Validate that an array of values of a specific type.
9 */
10export declare function array<T>(Element: Struct<T>): Struct<T[], Struct<T>>;
11/**
12 * Validate that boolean values.
13 */
14export declare function boolean(): Struct<boolean>;
15/**
16 * Validate that `Date` values.
17 *
18 * Note: this also ensures that the value is *not* an invalid `Date` object,
19 * which can occur when parsing a date fails but still returns a `Date`.
20 */
21export declare function date(): Struct<Date>;
22/**
23 * Validate that a value dynamically, determing which struct to use at runtime.
24 */
25export declare function dynamic<T>(fn: (value: unknown, ctx: StructContext) => Struct<T>): Struct<T>;
26/**
27 * Validate that a value against a set of potential values.
28 */
29export declare function enums<T>(values: T[]): Struct<T>;
30/**
31 * Validate that a value is a function.
32 */
33export declare function func(): Struct<Function>;
34/**
35 * Validate that a value is an instance of a class.
36 */
37export declare function instance<T extends {
38 new (...args: any): any;
39}>(Class: T): Struct<InstanceType<T>>;
40/**
41 * Validate that a value matches all of a set of structs.
42 */
43export declare function intersection<A>(Structs: StructTuple<[A]>): Struct<A>;
44export declare function intersection<A, B>(Structs: StructTuple<[A, B]>): Struct<A & B>;
45export declare function intersection<A, B, C>(Structs: StructTuple<[A, B, C]>): Struct<A & B & C>;
46export declare function intersection<A, B, C, D>(Structs: StructTuple<[A, B, C, D]>): Struct<A & B & C & D>;
47export declare function intersection<A, B, C, D, E>(Structs: StructTuple<[A, B, C, D, E]>): Struct<A & B & C & D & E>;
48/**
49 * Validate a value lazily, by constructing the struct right before the first
50 * validation. This is useful for cases where you want to have self-referential
51 * structs for nested data structures.
52 */
53export declare function lazy<T>(fn: () => Struct<T>): Struct<T>;
54/**
55 * Validate that a value is a specific constant.
56 */
57export declare function literal<T>(constant: T): Struct<T>;
58/**
59 * Validate that a value is a map with specific key and value entries.
60 */
61export declare function map<K, V>(Key: Struct<K>, Value: Struct<V>): Struct<Map<K, V>>;
62/**
63 * Validate that a value always fails.
64 */
65export declare function never(): Struct<never>;
66/**
67 * Validate that a value is a number.
68 */
69export declare function number(): Struct<number>;
70/**
71 * Validate that an object with specific entry values.
72 */
73export declare function object<V extends StructRecord<any>>(Structs: V): Struct<{
74 [K in keyof V]: StructType<V[K]>;
75}, V>;
76/**
77 * Augment a struct to make it accept optionally accept `undefined` values.
78 */
79export declare function optional<T>(S: Struct<T>): Struct<T | undefined>;
80/**
81 * Validate that a partial object with specific entry values.
82 */
83export declare function partial<T, V extends StructRecord<any>>(Structs: V | Struct<T, V>): Struct<{
84 [K in keyof V]?: StructType<V[K]>;
85}>;
86/**
87 * Validate that a value is a record with specific key and
88 * value entries.
89 */
90export declare function record<K extends string | number, V>(Key: Struct<K>, Value: Struct<V>): Struct<Record<K, V>>;
91/**
92 * Validate that a set of values matches a specific type.
93 */
94export declare function set<T>(Element: Struct<T>): Struct<Set<T>>;
95/**
96 * Validate that a value is a string.
97 */
98export declare function string(): Struct<string>;
99/**
100 * Define a `Struct` instance with a type and validation function.
101 */
102export declare function struct<T>(name: string, validator: Struct<T>['validator']): Struct<T, null>;
103/**
104 * Validate that a value is a tuple with entries of specific types.
105 */
106export declare function tuple<A>(Elements: StructTuple<[A]>): Struct<[A]>;
107export declare function tuple<A, B>(Elements: StructTuple<[A, B]>): Struct<[A, B]>;
108export declare function tuple<A, B, C>(Elements: StructTuple<[A, B, C]>): Struct<[A, B, C]>;
109export declare function tuple<A, B, C, D>(Elements: StructTuple<[A, B, C, D]>): Struct<[A, B, C, D]>;
110export declare function tuple<A, B, C, D, E>(Elements: StructTuple<[A, B, C, D, E]>): Struct<[A, B, C, D, E]>;
111/**
112 * Validate that a value matches a specific strutural interface, like the
113 * structural typing that TypeScript uses.
114 */
115export declare function type<V extends StructRecord<any>>(Structs: V): Struct<{
116 [K in keyof V]: StructType<V[K]>;
117}>;
118/**
119 * Validate that a value is one of a set of types.
120 */
121export declare function union<A>(Structs: StructTuple<[A]>): Struct<A>;
122export declare function union<A, B>(Structs: StructTuple<[A, B]>): Struct<A | B>;
123export declare function union<A, B, C>(Structs: StructTuple<[A, B, C]>): Struct<A | B | C>;
124export declare function union<A, B, C, D>(Structs: StructTuple<[A, B, C, D]>): Struct<A | B | C | D>;
125export declare function union<A, B, C, D, E>(Structs: StructTuple<[A, B, C, D, E]>): Struct<A | B | C | D | E>;