Skip to content

Commit a6bfdc7

Browse files
authored
Merge pull request #339 from thib3113/master
fix(types): improve typing
2 parents 349aac6 + 802753a commit a6bfdc7

File tree

1 file changed

+113
-73
lines changed

1 file changed

+113
-73
lines changed

index.d.ts

Lines changed: 113 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@ export type ValidationRuleName =
1717
| "multi"
1818
| "number"
1919
| "object"
20+
| "objectID"
2021
| "record"
2122
| "string"
23+
| "tuple"
2224
| "url"
2325
| "uuid"
2426
| string;
@@ -39,39 +41,47 @@ export interface RuleAny extends RuleCustom {
3941
* @see https://github.com/icebob/fastest-validator#array
4042
*/
4143
export interface RuleArray<T = any> extends RuleCustom {
42-
/**
43-
* Name of built-in validator
44-
*/
45-
type: "array";
46-
/**
47-
* If true, the validator accepts an empty array [].
48-
* @default true
49-
*/
50-
empty?: boolean;
51-
/**
52-
* Minimum count of elements
53-
*/
54-
min?: number;
55-
/**
56-
* Maximum count of elements
57-
*/
58-
max?: number;
59-
/**
60-
* Fixed count of elements
61-
*/
62-
length?: number;
63-
/**
64-
* The array must contain this element too
65-
*/
66-
contains?: T | T[];
67-
/**
68-
* Every element must be an element of the enum array
69-
*/
70-
enum?: T[];
71-
/**
72-
* Validation rules that should be applied to each element of array
73-
*/
74-
items?: ValidationRule;
44+
/**
45+
* Name of built-in validator
46+
*/
47+
type: "array";
48+
/**
49+
* If true, the validator accepts an empty array [].
50+
* @default true
51+
*/
52+
empty?: boolean;
53+
/**
54+
* Minimum count of elements
55+
*/
56+
min?: number;
57+
/**
58+
* Maximum count of elements
59+
*/
60+
max?: number;
61+
/**
62+
* Fixed count of elements
63+
*/
64+
length?: number;
65+
/**
66+
* The array must contain this element too
67+
*/
68+
contains?: T | T[];
69+
/**
70+
* The array must be unique (array of objects is always unique).
71+
*/
72+
unique?: boolean;
73+
/**
74+
* Every element must be an element of the enum array
75+
*/
76+
enum?: T[];
77+
/**
78+
* Validation rules that should be applied to each element of array
79+
*/
80+
items?: ValidationRule;
81+
/**
82+
* Wrap value into array if different type provided
83+
*/
84+
convert?: boolean
7585
}
7686

7787
/**
@@ -102,7 +112,7 @@ export interface RuleClass<T = any> extends RuleCustom {
102112
/**
103113
* Checked Class
104114
*/
105-
instanceOf?: T;
115+
instanceOf: T;
106116
}
107117

108118
/**
@@ -116,7 +126,6 @@ export interface RuleCurrency extends RuleCustom {
116126
type: "currency";
117127
/**
118128
* The currency symbol expected in string (as prefix)
119-
* @default null
120129
*/
121130
currencySymbol?: string;
122131
/**
@@ -167,13 +176,19 @@ export interface RuleEmail extends RuleCustom {
167176
type: "email";
168177
/**
169178
* If true, the validator accepts an empty string ""
170-
* @default true
179+
* @default false
171180
*/
172181
empty?: boolean;
173182
/**
174183
* Checker method. Can be quick or precise
184+
* @default quick
175185
*/
176186
mode?: "quick" | "precise";
187+
/**
188+
* Normalize the e-mail address (trim & lower-case). It's a sanitizer, it will change the value in the original object.
189+
* @default false
190+
*/
191+
normalize?: boolean;
177192
/**
178193
* Minimum value length
179194
*/
@@ -182,8 +197,6 @@ export interface RuleEmail extends RuleCustom {
182197
* Maximum value length
183198
*/
184199
max?: number;
185-
186-
normalize?: boolean;
187200
}
188201

189202
/**
@@ -223,8 +236,7 @@ export interface RuleEqual<T = any> extends RuleCustom {
223236
/**
224237
* Strict value checking.
225238
*
226-
* @type {'boolean'}
227-
* @memberof RuleEqual
239+
* @default false
228240
*/
229241
strict?: boolean;
230242
}
@@ -242,8 +254,7 @@ export interface RuleForbidden extends RuleCustom {
242254
/**
243255
* Removes the forbidden value.
244256
*
245-
* @type {'boolean'}
246-
* @memberof RuleForbidden
257+
* @default false
247258
*/
248259
remove?: boolean;
249260
}
@@ -291,7 +302,7 @@ export interface RuleMulti extends RuleCustom {
291302
*/
292303
type: "multi";
293304

294-
rules: RuleCustom[] | string[];
305+
rules: (RuleCustom | string)[];
295306
}
296307

297308
/**
@@ -371,28 +382,14 @@ export interface RuleObject extends RuleCustom {
371382
maxProps?: number;
372383
}
373384

374-
export interface RuleObjectID extends RuleCustom {
375-
/**
376-
* Name of built-in validator
377-
*/
378-
type: "objectID";
379-
/**
380-
* To inject ObjectID dependency
381-
*/
382-
ObjectID?: any;
383-
/**
384-
* Convert HexStringObjectID to ObjectID
385-
*/
386-
convert?: boolean | "hexString";
387-
}
388-
389385
export interface RuleRecord extends RuleCustom {
390386
/**
391387
* Name of built-in validator
392388
*/
393389
type: "record";
394390
/**
395391
* Key validation rule
392+
* @default "string"
396393
*/
397394
key?: RuleString;
398395
/**
@@ -439,14 +436,14 @@ export interface RuleString extends RuleCustom {
439436
* The value must be an element of the enum array
440437
*/
441438
enum?: string[];
442-
/**
443-
* The value must be a numeric string
444-
*/
445-
numeric?: boolean;
446439
/**
447440
* The value must be an alphabetic string
448441
*/
449442
alpha?: boolean;
443+
/**
444+
* The value must be a numeric string
445+
*/
446+
numeric?: boolean;
450447
/**
451448
* The value must be an alphanumeric string
452449
*/
@@ -457,37 +454,61 @@ export interface RuleString extends RuleCustom {
457454
alphadash?: boolean;
458455
/**
459456
* The value must be a hex string
460-
* @default false
461457
*/
462458
hex?: boolean;
463459
/**
464460
* The value must be a singleLine string
465-
* @default false
466461
*/
467462
singleLine?: boolean;
468463
/**
469464
* The value must be a base64 string
470-
* @default false
471465
*/
472466
base64?: boolean;
473467
/**
474-
* if true and the type is not a String, converts with String()
475-
* @default false
468+
* If true, the value will be trimmed. It's a sanitizer, it will change the value in the original object.
476469
*/
477-
convert?: boolean;
478-
479470
trim?: boolean;
471+
/**
472+
* If true, the value will be left trimmed. It's a sanitizer, it will change the value in the original object.
473+
*/
480474
trimLeft?: boolean;
475+
/**
476+
* If true, the value will be right trimmed. It's a sanitizer, it will change the value in the original object.
477+
*/
481478
trimRight?: boolean;
482-
479+
/**
480+
* If it's a number, the value will be left padded. It's a sanitizer, it will change the value in the original object.
481+
*/
483482
padStart?: number;
483+
/**
484+
* If it's a number, the value will be right padded. It's a sanitizer, it will change the value in the original object.
485+
*/
484486
padEnd?: number;
487+
/**
488+
* The padding character for the padStart and padEnd.
489+
* @default " "
490+
*/
485491
padChar?: string;
486-
492+
/**
493+
* If true, the value will be lower-cased. It's a sanitizer, it will change the value in the original object.
494+
*/
487495
lowercase?: boolean;
496+
/**
497+
* If true, the value will be upper-cased. It's a sanitizer, it will change the value in the original object.
498+
*/
488499
uppercase?: boolean;
500+
/**
501+
* If true, the value will be locale lower-cased. It's a sanitizer, it will change the value in the original object.
502+
*/
489503
localeLowercase?: boolean;
504+
/**
505+
* If true, the value will be locale upper-cased. It's a sanitizer, it will change the value in the original object.
506+
*/
490507
localeUppercase?: boolean;
508+
/**
509+
* if true and the type is not a String, converts with String()
510+
*/
511+
convert?: boolean;
491512
}
492513

493514
/**
@@ -499,6 +520,10 @@ export interface RuleTuple<T = any> extends RuleCustom {
499520
* Name of built-in validator
500521
*/
501522
type: "tuple";
523+
/**
524+
* If true, the validator accepts an empty array [].
525+
*/
526+
empty?: boolean
502527
/**
503528
* Validation rules that should be applied to the corresponding element of array
504529
*/
@@ -516,7 +541,7 @@ export interface RuleURL extends RuleCustom {
516541
type: "url";
517542
/**
518543
* If true, the validator accepts an empty string ""
519-
* @default true
544+
* @default false
520545
*/
521546
empty?: boolean;
522547
}
@@ -536,6 +561,21 @@ export interface RuleUUID extends RuleCustom {
536561
version?: 0 | 1 | 2 | 3 | 4 | 5 | 6;
537562
}
538563

564+
export interface RuleObjectID extends RuleCustom {
565+
/**
566+
* Name of built-in validator
567+
*/
568+
type: "objectID";
569+
/**
570+
* To inject ObjectID dependency
571+
*/
572+
ObjectID?: any;
573+
/**
574+
* Convert HexStringObjectID to ObjectID
575+
*/
576+
convert?: boolean | "hexString";
577+
}
578+
539579
/**
540580
* Validation schema definition for custom inline validator
541581
* @see https://github.com/icebob/fastest-validator#custom-validator
@@ -1107,7 +1147,7 @@ export default class Validator {
11071147
* @return {ValidationRule}
11081148
*/
11091149
getRuleFromSchema(
1110-
name: ValidationRuleName | ValidationRuleName[] | { [key: string]: unknown }
1150+
name: ValidationRuleName | ValidationRuleName[] | ValidationSchema | ValidationSchema[] | { [key: string]: unknown }
11111151
): {
11121152
messages: MessagesType;
11131153
schema: ValidationSchema;

0 commit comments

Comments
 (0)