@@ -2,6 +2,7 @@ declare module 'vest' {
2
2
type TestCB = ( ) => void | Promise < string | void > | false ;
3
3
type DraftResult = Omit < IVestResult , 'done' > ;
4
4
type DoneCB = ( res : DraftResult ) => void ;
5
+ type ExclusionArg = string | string [ ] | void ;
5
6
6
7
interface VestTest {
7
8
failed : boolean ;
@@ -14,7 +15,57 @@ declare module 'vest' {
14
15
asyncTest ?: Promise < string | void > ;
15
16
}
16
17
17
- interface Test {
18
+ interface ISkip {
19
+ /**
20
+ * Skips provided field from current run.
21
+ * When undefined, the expression is ignored.
22
+ * @param [fieldName] Field name or a list of fields to exclude
23
+ *
24
+ * @example
25
+ *
26
+ * vest.skip('username');
27
+ * vest.skip(['username', 'password']);
28
+ */
29
+ ( fieldName ?: ExclusionArg ) : void ;
30
+ /**
31
+ * Skips provided group from current run.
32
+ * When undefined, the expression is ignored.
33
+ * @param [groupName] group name or a list of groups to exclude.
34
+ *
35
+ * @example
36
+ *
37
+ * vest.skip.group('username');
38
+ * vest.skip.group(['username', 'password']);
39
+ */
40
+ group ( groupName ?: ExclusionArg ) : void ;
41
+ }
42
+
43
+ interface IOnly {
44
+ /**
45
+ * Singles out a field name to be tested.
46
+ * When fieldName is undefined, the expression is ignored.
47
+ * @param [fieldName] Field name or a list of fields to include
48
+ *
49
+ * @example
50
+ *
51
+ * vest.only('username');
52
+ * vest.only(['username', 'password']);
53
+ */
54
+ ( fieldName ?: ExclusionArg ) : void ;
55
+ /**
56
+ * Singles out a group name to be tested.
57
+ * When groupName is undefined, the expression is ignored.
58
+ * @param [groupName] Field name or a list of groups to include.
59
+ *
60
+ * @example
61
+ *
62
+ * vest.only.group('username');
63
+ * vest.only.group(['username', 'password']);
64
+ */
65
+ group ( groupName ?: ExclusionArg ) : void ;
66
+ }
67
+
68
+ interface ITest {
18
69
/**
19
70
* Runs a single test
20
71
* @param fieldName The name of the field being validated
@@ -71,26 +122,80 @@ declare module 'vest' {
71
122
memo ( fieldName : string , testFn : TestCB , dependencies : any [ ] ) : VestTest ;
72
123
}
73
124
125
+ interface IEnforce {
126
+ /**
127
+ * Assertion function. Throws an error on failure.
128
+ * @param value Value being enforced
129
+ */
130
+ ( value : any ) : IEnforceRules ;
131
+
132
+ /**
133
+ * Creates a new enforce instance with custom rules
134
+ * @param rules Rules object to add onto enforce
135
+ *
136
+ * @example
137
+ *
138
+ * const customEnforce = enforce.extend({
139
+ * isValidEmail: (email) => email.includes('@')
140
+ * });
141
+ *
142
+ * customEnforce('notAnEmail') // throws an error
143
+ */
144
+ extend <
145
+ T extends { [ key : string ] : ( value : any , ...args : any [ ] ) => boolean }
146
+ > (
147
+ obj : T
148
+ ) : ( value : any ) => IEnforceRules < T > & EnforceExtendMap < T > ;
149
+ }
150
+
74
151
interface Vest {
75
- test : Test ;
152
+ test : ITest ;
153
+ enforce : IEnforce ;
154
+ only : IOnly ;
155
+ skip : ISkip ;
76
156
77
157
/**
78
158
* Runs a stateless validation suite.
79
159
* @param suiteName Unique suite name.
80
160
* @param tests Suite body.
161
+ *
162
+ * const res = validate('form_name', () => {
163
+ * // your tests go here
164
+ * });
81
165
*/
82
166
validate ( suiteName : string , tests : ( ) => void ) : IVestResult ;
83
167
84
168
/**
85
169
* Runs a stateful validation suite.
86
170
* @param suiteName Unique suite name.
87
171
* @param tests Suite body.
172
+ *
173
+ * @example
174
+ *
175
+ * const validate = vest.create('form_name', (data = {}) => {
176
+ * // your tests go here
177
+ * });
178
+ *
179
+ * const res = validate({username: 'example'});
88
180
*/
89
181
create (
90
182
suiteName : string ,
91
183
tests : ( ...args : any [ ] ) => void
92
184
) : ( ...args : any [ ] ) => IVestResult ;
93
185
186
+ /**
187
+ * Allows grouping tests so you can handle them together
188
+ * @param groupName Group name.
189
+ * @param testFn Group body.
190
+ *
191
+ * @example
192
+ *
193
+ * group('sign_up', () => {
194
+ * // your tests go here
195
+ * });
196
+ */
197
+ group ( groupName : string , testFn : ( ) => void ) : void ;
198
+
94
199
/**
95
200
* Marks a test as warning.
96
201
* Needs to run within a test body.
@@ -110,11 +215,6 @@ declare module 'vest' {
110
215
*/
111
216
draft ( ) : DraftResult ;
112
217
VERSION : string ;
113
- /**
114
- * Assertion function. Throws an error on failure.
115
- * @param value Value being enforced
116
- */
117
- enforce ( value : any ) : IEnforceRules ;
118
218
}
119
219
120
220
interface IVestResult {
@@ -282,14 +382,33 @@ declare module 'vest' {
282
382
lengthNotEquals : (
283
383
expected : number
284
384
) => IEnforceRules < T > & EnforceExtendMap < T > ;
285
- extend : < T extends { [ key : string ] : ( ...args : any [ ] ) => boolean } > (
286
- obj : T
287
- ) => IEnforceRules < T > & EnforceExtendMap < T > ;
288
385
}
289
386
290
- const { test, validate, create, warn, draft, VERSION , enforce } : Vest ;
387
+ const {
388
+ test,
389
+ validate,
390
+ create,
391
+ warn,
392
+ draft,
393
+ VERSION ,
394
+ enforce,
395
+ skip,
396
+ only,
397
+ group,
398
+ } : Vest ;
291
399
292
- export { test , validate , create , warn , draft , VERSION , enforce } ;
400
+ export {
401
+ test ,
402
+ validate ,
403
+ create ,
404
+ warn ,
405
+ draft ,
406
+ VERSION ,
407
+ enforce ,
408
+ skip ,
409
+ only ,
410
+ group ,
411
+ } ;
293
412
294
413
const vest : Vest ;
295
414
0 commit comments