-
Notifications
You must be signed in to change notification settings - Fork 2
/
easy-schema.d.ts
164 lines (139 loc) · 4.47 KB
/
easy-schema.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
export declare const has: unique symbol;
// Base Schema class for common behavior
declare class BaseSchema<T> {
constructor(type: T);
default(value: any): this;
where(value: (item: T) => boolean): this;
enums(value: any, message?: string): this;
}
// Specialized schemas for different types
declare class StringSchema extends BaseSchema<string> {
min(value: number | string, message?: string): this;
max(value: number | string, message?: string): this;
regex(value: RegExp, message?: string): this;
}
declare class NumberSchema extends BaseSchema<number> {
min(value: number, message?: string): this;
max(value: number, message?: string): this;
}
declare class ArraySchema extends BaseSchema<any[]> {
min(value: number, message?: string): this;
max(value: number, message?: string): this;
unique(value?: boolean): this;
only(value?: any): this;
}
declare class ObjectSchema extends BaseSchema<object> {
min(value: number, message?: string): this;
max(value: number, message?: string): this;
extra(value?: boolean): this;
only(value?: any): this;
}
// Augment native constructors with "has" symbol-based getter
declare global {
interface StringConstructor {
[has]: StringSchema;
}
interface NumberConstructor {
[has]: NumberSchema;
}
interface ArrayConstructor {
[has]: ArraySchema;
}
interface ObjectConstructor {
[has]: ObjectSchema;
}
interface BooleanConstructor {
[has]: BaseSchema<boolean>;
}
interface DateConstructor {
[has]: BaseSchema<Date>;
}
}
// Custom types for Integer, ID, and ObjectID
interface IntegerConstructor {
[has]: NumberSchema;
}
interface IDConstructor {
[has]: BaseSchema<string>;
}
interface ObjectIDConstructor {
[has]: BaseSchema<Mongo.ObjectID>;
}
interface DecimalConstructor {
[has]: BaseSchema<any>;
}
export declare const ID: IDConstructor;
export declare const ObjectID: ObjectIDConstructor;
/**
* Check that data matches a schema.
* @param data The data to check
* @param schema The schema to match `data` against
*/
export declare function check<T extends Match.Pattern>(
data: any,
schema: T
): asserts data is Match.PatternMatch<T>;
/** Matches any value. */
export declare const Any: Match.Matcher<any>;
/** Matches a signed 32-bit integer. Doesn’t match `Infinity`, `-Infinity`, or `NaN`. */
export declare const Integer: Match.Matcher<number>;
/**
* Matches either `undefined`, `null`, or pattern. If used in an object, matches only if the key is not set as opposed to the value being set to `undefined` or `null`. This set of conditions
* was chosen because `undefined` arguments to Meteor Methods are converted to `null` when sent over the wire.
*/
export declare function Optional<T extends Pattern>(
pattern: T
): Matcher<PatternMatch<T> | undefined | null>;
/**
* Shapes an object based on a POJO.
*
* @param {Object} obj - The object to be shaped.
* @param {Object} [options] - Options object.
* @param {boolean} [options.optionalize=false] - If true, marks all properties as optional.
* @returns {Object} The shaped object that's ready to use with jam:easy-schema `check`.
*/
export declare const shape: (obj: Record<string, any>, options?: { optionalize?: boolean }) => Record<string, any>;
/**
* Creates a new object composed of the specified keys and their corresponding values from the given object.
*
* @param {Object} obj - The source object from which to pick keys.
* @param {string[]} keys - An array of keys to pick from the source object.
* @returns {Object} - A new object containing only the specified keys and their values.
*/
export declare const pick: (obj: Record<string, any>, keys: string[]) => Record<string, any>;
export declare const EasySchema: {
/**
* Readonly configuration options for EasySchema.
*/
readonly config: {
base?: object;
autoCheck?: boolean;
autoAttachJSONSchema?: boolean;
validationAction?: string;
validationLevel?: string;
additionalBsonTypes?: object;
},
/**
* Configures the settings for EasySchema.
*/
configure: (options: {
base?: object;
autoCheck?: boolean;
autoAttachJSONSchema?: boolean;
validationAction?: string;
validationLevel?: string;
additionalBsonTypes?: object;
}) => object,
/**
* Make a field required that was optional. See docs for more info.
*/
readonly REQUIRED: string,
};
declare module 'meteor/mongo' {
module Mongo {
interface Collection<T> {
attachSchema(schema: object): void;
schema?: Match.Pattern;
}
}
}