-
Notifications
You must be signed in to change notification settings - Fork 11
/
index.d.ts
537 lines (490 loc) · 17.4 KB
/
index.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
declare module 'passable' {
const passable: Passable;
const enforce: (value) => PassableNS.IEnforceInstance;
const Enforce: PassableNS.IEnforceConstructor;
const validate: PassableNS.IValidate;
const WARN: PassableNS.IWARN;
const FAIL: PassableNS.IFAIL;
const VERSION: PassableNS.IVERSION;
export default passable;
export { enforce, Enforce, validate, WARN, FAIL, VERSION };
interface Passable {
(name: string, testFn: (test: (name: string, errorMessage: string, callback: PassableNS.IFunctionOrPromise) => void,
draft: PassableNS.IValidationResult) => void,
specific?: string | string[] | {only?: string | string[], not?: string | string[]}):
PassableNS.IValidationResult,
enforce(value): PassableNS.IEnforceInstance;
test(name: string, errorMessage: string, callback: PassableNS.IFunctionOrPromise): void;
draft(): PassableNS.IEnforceInstance;
Enforce: PassableNS.IEnforceConstructor;
any: PassableNS.IAny;
validate: PassableNS.IValidate;
VERSION: PassableNS.IVERSION;
WARN: PassableNS.IWARN;
FAIL: PassableNS.IFAIL;
}
namespace PassableNS {
export interface IValidationResult {
/**
* The name of the form being validated
*/
name: string;
/**
* Overall errors count in current validation suite
*/
failCount: number;
/**
* All skipped fields in suite (empty, unless the specific option is used)
*/
skipped: string[];
/**
* Overall warnings count in current validation suite
*/
testCount: number;
/**
* Detailed stats per field (structure detailed below)
*/
testsPerformed: {
[fieldName: string]: {
/**
* Overall test count in this field
*/
testCount: number;
/**
* Overall errors count for this field
*/
failCount: number;
/**
* Overall warnings count for this field
*/
warnCount: number;
}
};
/**
* Actual errors per each field
*/
errors: {
[fieldName: string]: string[];
};
/**
* Actual errors per each field
*/
warnings: {
[fieldName: string]: string[];
};
/**
* Overall warnings count for this form
*/
warnCount: number;
/**
* Getter function which allows accessing the errors array of a certain field (or the whole suite if not supplied)
*/
getErrors: (field?: string) => any[];
/**
* Getter function which allows accessing the warnings array of a certain field (or the whole suite if not supplied)
*/
getWarnings: (field?: string) => any[];
/**
* Returns whether a certain field (or the whole suite if not supplied) has errors
*/
hasErrors: (field?: string) => boolean;
/**
* Returns whether a certain field (or the whole suite if not supplied) has warnings
*/
hasWarnings: (field?: string) => boolean;
/**
* Registers a completion callback for the validation suite
*/
done: (callback: (res: PassableNS.IValidationResult) => void) => PassableNS.IValidationResult;
/**
* Registers a completion callback for a specific field
*/
after: (fieldName: string, callback: (res: PassableNS.IValidationResult) => void) => PassableNS.IValidationResult;
/**
* Cancels Async tests callbacks (after/done)
*/
cancel: () => PassableNS.IValidationResult;
}
export type IFunctionOrPromise = () => void | Promise<any>;
export type IVERSION = string;
export type IWARN = 'warn';
export type IFAIL = 'fail';
export interface IValidate {
(): boolean;
}
export interface IAny {
(): boolean;
}
type IEnforceChain<T> = {
[K in keyof T]: IEnforceInstance
};
export type IEnforceConstructor = {
new(): (value) => IEnforceInstance;
new<T extends { [key: string]: (...args) => boolean }>
(arg: T): (value) => IEnforceInstance<IEnforceChain<T>>;
};
export interface IEnforceInstance<T = {}> {
/**
* Checks if a value contains a regex match by Regex expression
*
* @example
* enforce(1984).matches(/[0-9]/) // truthy
*
* enforce('nineteen eighty four').matches(/[0-9]/) // falsy
*/
matches(regex: RegExp): IEnforceInstance & T;
/**
* Checks if a value contains a regex match by a string expression
*
* @example
* enforce(1984).matches('[0-9]') //truthy
*
* enforce('nineteen eighty four').matches('[0-9]') // falsy
*/
matches(regexAsString: string): IEnforceInstance & T;
/**
* Checks if a value doesn't contains a regex match by Regex expression
*
* @example
* enforce(1984).notMatches(/[0-9]/) // falsy
*/
notMatches(regex: RegExp): IEnforceInstance & T;
/**
* Checks if a value doesn't contains a regex match by string expression
*
* @example
* enforce('nineteen eighty four').notMatches('[0-9]') // truthy
*/
notMatches(regexAsString: string): IEnforceInstance & T;
/**
* Checks if your enforce value is contained in another array
*
* @example
* enforce('hello').inside(['hello', 'world']) // truthy
*
* enforce(3).inside([1, 2]) // falsy
*
* enforce(false).inside([true, false]) // truthy
*/
inside(array: number[] | string[] | boolean[]): IEnforceInstance & T;
/**
* Checks if your enforce value is contained in another string
*
* @example
* enforce('da').inside('tru dat.') // truthy
*
* enforce('ad').inside('tru dat.') // falsy
*/
inside(text: string): IEnforceInstance & T;
/**
* Checks if your enforce value is not contained in another array
*
* @example
* enforce('hello').notInside(['hello', 'world']) // falsy
*/
notInside(array: number[] | string[] | boolean[]): IEnforceInstance & T;
/**
* Checks if your enforce value is not contained in another string
*
* @example
* enforce('ad').notInside('tru dat.') // truthy
*/
notInside(text: string): IEnforceInstance & T;
/**
* Checks if a value is of type Array
*
* @example
* enforce(['hello']).isArray() // truthy
*
* enforce('hello').isArray() // falsy
*/
isArray(): IEnforceInstance & T;
/**
* Checks if a value is of any type other than array
*
* @example
* enforce(['hello']).isNotArray() // falsy
*
* enforce('hello').isNotArray() // truthy
*/
isNotArray(): IEnforceInstance & T;
/**
* Checks if a value is of type String
*
* @example
* enforce('hello').isString() // truthy
*
* enforce(['hello']).isString() // falsy
*/
isString(): IEnforceInstance & T;
/**
* Checks if a value is of any type other than string
*
* @example
* enforce('hello').isNotString() // falsy
*
* enforce(['hello']).isNotString() // truthy
*/
isNotString(): IEnforceInstance & T;
/**
* Checks if a value is of type number
*
* @example
* enforce(143).isNumber() // truthy
*
* enforce(NaN).isNumber() // truthy! (NaN is of type 'number!')
*/
isNumber(): IEnforceInstance & T;
/**
* Checks if a value is of any type other than number
*
* @example
* enforce(143).isNotNumber() // falsy
*
* enforce('143').isNotNumber() // truthy
*/
isNotNumber(): IEnforceInstance & T;
/**
* Checks if your enforce value is empty, false, zero, null or undefined
*
* @example
* enforce([]).isEmpty() // truthy
*
* enforce('').isEmpty() // truthy
*
* enforce({}).isEmpty() // truthy
*/
isEmpty(): IEnforceInstance & T;
/**
* Checks that your enforce value is not empty, false, or zero
*
* @example
*
* enforce([1]).isNotEmpty() // truthy
*
* enforce({1:1}).isNotEmpty() // truthy
*
* enforce([]).isNotEmpty() // falsy
*/
isNotEmpty(): IEnforceInstance & T;
/**
* Checks that your enforce value is a numeric value
*
* @example
*
* enforce('-0x42').isNumeric() // falsy
*
* enforce('0xFF').isNumeric() // truthy
*/
isNumeric(): IEnforceInstance & T;
/**
* Checks that your enforce value is not a numeric value
*
* @example
*
* enforce('7.2acdgs').isNotNumeric() // truthy
*
* enforce('-10').isNotNumeric() // falsy
*/
isNotNumeric(): IEnforceInstance & T;
/**
* Checks that your numeric enforce value is smaller than another value
*
* @example
*
* enforce(0).lessThan(1) // truthy
*
* enforce('1').lessThan(0) // falsy
*/
lessThan(size: number): IEnforceInstance & T;
/**
* Checks that your numeric enforce value is smaller than another value
*
* @example
*
* enforce(0).lt(1) // truthy
*
* enforce('1').lt(0) // falsy
*/
lt(size: number): IEnforceInstance & T;
/**
* Checks that your numeric enforce value is smaller than or equals another value
*
* @example
*
* enforce(0).lessThanOrEquals(1) // truthy
*
* enforce('1').lessThanOrEquals(1) // truthy
*
* enforce(2).lessThanOrEquals(1) // falsy
*/
lessThanOrEquals(size: number): IEnforceInstance & T;
/**
* Checks that your numeric enforce value is smaller than or equals another value
*
* @example
*
* enforce(0).lte(1) // truthy
*
* enforce('1').lte(1) // truthy
*
* enforce(2).lte('1') // falsy
*/
lte(size: number): IEnforceInstance & T;
/**
* Checks that your numeric enforce value is greater than another value
*
* @example
*
* enforce(1).greaterThan(0) // truthy
*
* enforce('0').greaterThan('1') // falsy
*/
greaterThan(size: number): IEnforceInstance & T;
/**
* Checks that your numeric enforce value is greater than another value
*
* @example
*
* enforce(1).gt(0) // truthy
*
* enforce(0).gt('1') // falsy
*/
gt(size: number): IEnforceInstance & T;
/**
* Checks that your numeric enforce value is greater than or equals another value
*
* @example
*
* enforce(1).greaterThanOrEquals(1) // truthy
*
* enforce('1').greaterThanOrEquals(0) // truthy
*
* enforce('2').greaterThanOrEquals(3) // falsy
*/
greaterThanOrEquals(size: number): IEnforceInstance & T;
/**
* Checks that your numeric enforce value is greater than or equals another value
*
* @example
*
* enforce(1).gte(1) // truthy
*
* enforce('1').gte(0) // truthy
*
* enforce(2).gte('3') // falsy
*/
gte(size: number): IEnforceInstance & T;
/**
* Checks that your enforce value equals a given number
*
* @example
*
* enforce('1').numberEquals(1) // truthy
*
* enforce(2).numberEquals(2) // truthy
*
* enforce('2').numberEquals(0) // falsy
*/
numberEquals(size: number): IEnforceInstance & T;
/**
* Checks that your enforce value does not equal a given number
*
* @example
*
* enforce(3).numberNotEquals(1) // truthy
*
* enforce('3').numberNotEquals(3) // falsy
*/
numberNotEquals(size: number): IEnforceInstance & T;
/**
* Checks that your enforce value is longer than a given number
*
* @example
*
* enforce(['one']).longerThan(0) // truthy
*
* enforce('').longerThan(0) // falsy
*/
longerThan(size: number): IEnforceInstance & T;
/**
* Checks that your enforce value is longer than or equals another value
*
* @example
*
* enforce([1]).longerThanOrEquals(0) // truthy
*
* enforce('').longerThanOrEquals(1) // falsy
*/
longerThanOrEquals(size: number): IEnforceInstance & T;
/**
* Checks that your enforce value is shorter than a given number
*
* @example
*
* enforce([]).shorterThan(1) // truthy
*
* enforce('0').shorterThan(0) // falsy
*/
shorterThan(size: number): IEnforceInstance & T;
/**
* Checks that your enforce value is shorter than or equals another value
*
* @example
*
* enforce([]).shorterThanOrEquals(1) // truthy
*
* enforce('0').shorterThanOrEquals(0) // falsy
*/
shorterThanOrEquals(size: number): IEnforceInstance & T;
/**
* Checks that your enforce value equals a given number
*
* @example
*
* enforce([1]).lengthEquals(1) // truthy
*
* enforce('0').lengthEquals(0) // falsy
*/
lengthEquals(size: number): IEnforceInstance & T;
/**
* Checks that your enforce value does not equal a given number
*
* @example
*
* enforce([]).lengthNotEquals(1) // truthy
*
* enforce('').lengthNotEquals(0) // falsy
*/
lengthNotEquals(size: number): IEnforceInstance & T;
/**
* Checks that your enforce value strictly equals (===) a given value
*
* @example
*
* enforce(1).equals(1) // truthy
*
* enforce('hello').equals('hello') // truthy
*
* enforce('1').equals(1) // falsy
*
* enforce([1]).equals([1]) // falsy
*/
equals(value: any): IEnforceInstance & T;
/**
* Checks that your enforce value doesn't strictly equal (===) a given value
*
* @example
*
* enforce('1').notEquals(1) // truthy
*
* enforce([1]).notEquals([1]) // truthy
*
* enforce(1).notEquals(1) // falsy
*
* enforce('hello').notEquals('hello') // falsy
*/
notEquals(value: any): IEnforceInstance & T;
}
}
}