From 54b94aa8ae256747999171f433ef9957c67a0768 Mon Sep 17 00:00:00 2001 From: Thibaut SEVERAC Date: Tue, 3 Oct 2023 14:37:20 +0200 Subject: [PATCH 1/5] fix(types): unique is missing in array definition --- index.d.ts | 70 +++++++++++++++++++++++++++++------------------------- 1 file changed, 37 insertions(+), 33 deletions(-) diff --git a/index.d.ts b/index.d.ts index fe3f28e..786ffa2 100644 --- a/index.d.ts +++ b/index.d.ts @@ -39,39 +39,43 @@ export interface RuleAny extends RuleCustom { * @see https://github.com/icebob/fastest-validator#array */ export interface RuleArray extends RuleCustom { - /** - * Name of built-in validator - */ - type: "array"; - /** - * If true, the validator accepts an empty array []. - * @default true - */ - empty?: boolean; - /** - * Minimum count of elements - */ - min?: number; - /** - * Maximum count of elements - */ - max?: number; - /** - * Fixed count of elements - */ - length?: number; - /** - * The array must contain this element too - */ - contains?: T | T[]; - /** - * Every element must be an element of the enum array - */ - enum?: T[]; - /** - * Validation rules that should be applied to each element of array - */ - items?: ValidationRule; + /** + * Name of built-in validator + */ + type: "array"; + /** + * If true, the validator accepts an empty array []. + * @default true + */ + empty?: boolean; + /** + * Minimum count of elements + */ + min?: number; + /** + * Maximum count of elements + */ + max?: number; + /** + * Fixed count of elements + */ + length?: number; + /** + * The array must contain this element too + */ + contains?: T | T[]; + /** + * Every element must be an element of the enum array + */ + enum?: T[]; + /** + * Validation rules that should be applied to each element of array + */ + items?: ValidationRule; + /** + * The array must be unique (array of objects is always unique). + */ + unique?: boolean; } /** From 9657fdce3946dfc5ecc70ab1298a4069c272c4eb Mon Sep 17 00:00:00 2001 From: thibaut severac Date: Tue, 3 Oct 2023 17:53:58 +0200 Subject: [PATCH 2/5] fix(types): add missing objectId and tuple in ValidationRuleName --- index.d.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/index.d.ts b/index.d.ts index 786ffa2..6d6eac8 100644 --- a/index.d.ts +++ b/index.d.ts @@ -17,8 +17,10 @@ export type ValidationRuleName = | "multi" | "number" | "object" + | "objectID" | "record" | "string" + | "tuple" | "url" | "uuid" | string; From 11204afd568aafb45a703c7cf043b8a472ef4371 Mon Sep 17 00:00:00 2001 From: thibaut severac Date: Tue, 3 Oct 2023 18:17:04 +0200 Subject: [PATCH 3/5] fix(types): add missing types, harmonize defaults, correct some default error, ordonne like the documentation --- index.d.ts | 114 ++++++++++++++++++++++++++++++++++------------------- 1 file changed, 74 insertions(+), 40 deletions(-) diff --git a/index.d.ts b/index.d.ts index 6d6eac8..2d215fc 100644 --- a/index.d.ts +++ b/index.d.ts @@ -66,6 +66,10 @@ export interface RuleArray extends RuleCustom { * The array must contain this element too */ contains?: T | T[]; + /** + * The array must be unique (array of objects is always unique). + */ + unique?: boolean; /** * Every element must be an element of the enum array */ @@ -74,10 +78,10 @@ export interface RuleArray extends RuleCustom { * Validation rules that should be applied to each element of array */ items?: ValidationRule; - /** - * The array must be unique (array of objects is always unique). + /** + * Wrap value into array if different type provided */ - unique?: boolean; + convert?: boolean } /** @@ -122,7 +126,6 @@ export interface RuleCurrency extends RuleCustom { type: "currency"; /** * The currency symbol expected in string (as prefix) - * @default null */ currencySymbol?: string; /** @@ -173,13 +176,19 @@ export interface RuleEmail extends RuleCustom { type: "email"; /** * If true, the validator accepts an empty string "" - * @default true + * @default false */ empty?: boolean; /** * Checker method. Can be quick or precise + * @default quick */ mode?: "quick" | "precise"; + /** + * Normalize the e-mail address (trim & lower-case). It's a sanitizer, it will change the value in the original object. + * @default false + */ + normalize?: boolean; /** * Minimum value length */ @@ -188,8 +197,6 @@ export interface RuleEmail extends RuleCustom { * Maximum value length */ max?: number; - - normalize?: boolean; } /** @@ -229,8 +236,7 @@ export interface RuleEqual extends RuleCustom { /** * Strict value checking. * - * @type {'boolean'} - * @memberof RuleEqual + * @default false */ strict?: boolean; } @@ -248,8 +254,7 @@ export interface RuleForbidden extends RuleCustom { /** * Removes the forbidden value. * - * @type {'boolean'} - * @memberof RuleForbidden + * @default false */ remove?: boolean; } @@ -377,21 +382,6 @@ export interface RuleObject extends RuleCustom { maxProps?: number; } -export interface RuleObjectID extends RuleCustom { - /** - * Name of built-in validator - */ - type: "objectID"; - /** - * To inject ObjectID dependency - */ - ObjectID?: any; - /** - * Convert HexStringObjectID to ObjectID - */ - convert?: boolean | "hexString"; -} - export interface RuleRecord extends RuleCustom { /** * Name of built-in validator @@ -399,6 +389,7 @@ export interface RuleRecord extends RuleCustom { type: "record"; /** * Key validation rule + * @default "string" */ key?: RuleString; /** @@ -445,14 +436,14 @@ export interface RuleString extends RuleCustom { * The value must be an element of the enum array */ enum?: string[]; - /** - * The value must be a numeric string - */ - numeric?: boolean; /** * The value must be an alphabetic string */ alpha?: boolean; + /** + * The value must be a numeric string + */ + numeric?: boolean; /** * The value must be an alphanumeric string */ @@ -463,37 +454,61 @@ export interface RuleString extends RuleCustom { alphadash?: boolean; /** * The value must be a hex string - * @default false */ hex?: boolean; /** * The value must be a singleLine string - * @default false */ singleLine?: boolean; /** * The value must be a base64 string - * @default false */ base64?: boolean; /** - * if true and the type is not a String, converts with String() - * @default false + * If true, the value will be trimmed. It's a sanitizer, it will change the value in the original object. */ - convert?: boolean; - trim?: boolean; + /** + * If true, the value will be left trimmed. It's a sanitizer, it will change the value in the original object. + */ trimLeft?: boolean; + /** + * If true, the value will be right trimmed. It's a sanitizer, it will change the value in the original object. + */ trimRight?: boolean; - + /** + * If it's a number, the value will be left padded. It's a sanitizer, it will change the value in the original object. + */ padStart?: number; + /** + * If it's a number, the value will be right padded. It's a sanitizer, it will change the value in the original object. + */ padEnd?: number; + /** + * The padding character for the padStart and padEnd. + * @default " " + */ padChar?: string; - + /** + * If true, the value will be lower-cased. It's a sanitizer, it will change the value in the original object. + */ lowercase?: boolean; + /** + * If true, the value will be upper-cased. It's a sanitizer, it will change the value in the original object. + */ uppercase?: boolean; + /** + * If true, the value will be locale lower-cased. It's a sanitizer, it will change the value in the original object. + */ localeLowercase?: boolean; + /** + * If true, the value will be locale upper-cased. It's a sanitizer, it will change the value in the original object. + */ localeUppercase?: boolean; + /** + * if true and the type is not a String, converts with String() + */ + convert?: boolean; } /** @@ -505,6 +520,10 @@ export interface RuleTuple extends RuleCustom { * Name of built-in validator */ type: "tuple"; + /** + * If true, the validator accepts an empty array []. + */ + empty?: boolean /** * Validation rules that should be applied to the corresponding element of array */ @@ -522,7 +541,7 @@ export interface RuleURL extends RuleCustom { type: "url"; /** * If true, the validator accepts an empty string "" - * @default true + * @default false */ empty?: boolean; } @@ -542,6 +561,21 @@ export interface RuleUUID extends RuleCustom { version?: 0 | 1 | 2 | 3 | 4 | 5 | 6; } +export interface RuleObjectID extends RuleCustom { + /** + * Name of built-in validator + */ + type: "objectID"; + /** + * To inject ObjectID dependency + */ + ObjectID?: any; + /** + * Convert HexStringObjectID to ObjectID + */ + convert?: boolean | "hexString"; +} + /** * Validation schema definition for custom inline validator * @see https://github.com/icebob/fastest-validator#custom-validator From c42de38b76d7cdc667917cdfd0acc70ccc887800 Mon Sep 17 00:00:00 2001 From: thibaut severac Date: Sat, 7 Oct 2023 15:23:50 +0200 Subject: [PATCH 4/5] fix(types): instanceOf seems mandatory in the code --- index.d.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.d.ts b/index.d.ts index 2d215fc..ca2e9a4 100644 --- a/index.d.ts +++ b/index.d.ts @@ -112,7 +112,7 @@ export interface RuleClass extends RuleCustom { /** * Checked Class */ - instanceOf?: T; + instanceOf: T; } /** @@ -1147,7 +1147,7 @@ export default class Validator { * @return {ValidationRule} */ getRuleFromSchema( - name: ValidationRuleName | ValidationRuleName[] | { [key: string]: unknown } + name: ValidationRuleName | ValidationRuleName[] | ValidationSchema | ValidationSchema[] | { [key: string]: unknown } ): { messages: MessagesType; schema: ValidationSchema; From 802753a7d151c2e13c4efea24e8247cf446671ae Mon Sep 17 00:00:00 2001 From: thib3113 Date: Sun, 8 Oct 2023 12:12:12 +0200 Subject: [PATCH 5/5] fix(types): multi allow to mix shortHand and rules --- index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.d.ts b/index.d.ts index ca2e9a4..a05de47 100644 --- a/index.d.ts +++ b/index.d.ts @@ -302,7 +302,7 @@ export interface RuleMulti extends RuleCustom { */ type: "multi"; - rules: RuleCustom[] | string[]; + rules: (RuleCustom | string)[]; } /**