From d8768a4c05a960c05ccd59e6839127e2f1d820bc Mon Sep 17 00:00:00 2001 From: andreas Date: Thu, 6 Apr 2023 13:29:26 +0200 Subject: [PATCH] added module declaration The d.ts file is missing a module declaration for typescript to work --- index.d.ts | 2003 ++++++++++++++++++++++++++-------------------------- 1 file changed, 1002 insertions(+), 1001 deletions(-) diff --git a/index.d.ts b/index.d.ts index 05bf241..6b59ffe 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,1124 +1,1125 @@ +declare module "fastest-validator" { + export type ValidationRuleName = + | "any" + | "array" + | "boolean" + | "class" + | "currency" + | "custom" + | "date" + | "email" + | "enum" + | "equal" + | "forbidden" + | "function" + | "luhn" + | "mac" + | "multi" + | "number" + | "object" + | "record" + | "string" + | "url" + | "uuid" + | string; + + /** + * Validation schema definition for "any" built-in validator + * @see https://github.com/icebob/fastest-validator#any + */ + export interface RuleAny extends RuleCustom { + /** + * Name of built-in validator + */ + type: "any"; + } -export type ValidationRuleName = - | "any" - | "array" - | "boolean" - | "class" - | "currency" - | "custom" - | "date" - | "email" - | "enum" - | "equal" - | "forbidden" - | "function" - | "luhn" - | "mac" - | "multi" - | "number" - | "object" - | "record" - | "string" - | "url" - | "uuid" - | string; - -/** - * Validation schema definition for "any" built-in validator - * @see https://github.com/icebob/fastest-validator#any - */ -export interface RuleAny extends RuleCustom { - /** - * Name of built-in validator - */ - type: "any"; -} - -/** - * Validation schema definition for "array" built-in validator - * @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; -} - -/** - * Validation schema definition for "boolean" built-in validator - * @see https://github.com/icebob/fastest-validator#boolean - */ -export interface RuleBoolean extends RuleCustom { - /** - * Name of built-in validator - */ - type: "boolean"; - /** - * if true and the type is not Boolean, try to convert. 1, "true", "1", "on" will be true. 0, "false", "0", "off" will be false. - * @default false - */ - convert?: boolean; -} - -/** - * Validation schema definition for "class" built-in validator - * @see https://github.com/icebob/fastest-validator#class - */ -export interface RuleClass extends RuleCustom { - /** - * Name of built-in validator - */ - type: "class"; - /** - * Checked Class - */ - instanceOf?: T; -} - -/** - * Validation schema definition for "currency" built-in validator - * @see https://github.com/icebob/fastest-validator#currency - */ -export interface RuleCurrency extends RuleCustom { - /** - * Name of built-in validator - */ - type: "currency"; - /** - * The currency symbol expected in string (as prefix) - * @default null - */ - currencySymbol?: string; - /** - * Toggle to make the currency symbol optional in string - * @default false - */ - symbolOptional?: boolean; - /** - * Thousand place separator character - * @default ',' - */ - thousandSeparator?: string; - /** - * Decimal place character - * @default '.' - */ - decimalSeparator?: string; - /** - * Custom regular expression to validate currency strings - */ - customRegex?: RegExp | string; -} - -/** - * Validation schema definition for "date" built-in validator - * @see https://github.com/icebob/fastest-validator#date - */ -export interface RuleDate extends RuleCustom { - /** - * Name of built-in validator - */ - type: "date"; - /** - * if true and the type is not Date, try to convert with new Date() - * @default false - */ - convert?: boolean; -} - -/** - * Validation schema definition for "email" built-in validator - * @see https://github.com/icebob/fastest-validator#email - */ -export interface RuleEmail extends RuleCustom { - /** - * Name of built-in validator - */ - type: "email"; - /** - * If true, the validator accepts an empty string "" - * @default true - */ - empty?: boolean; - /** - * Checker method. Can be quick or precise - */ - mode?: "quick" | "precise"; - /** - * Minimum value length - */ - min?: number; - /** - * Maximum value length - */ - max?: number; - - normalize?: boolean; -} - -/** - * Validation schema definition for "enum" built-in validator - * @see https://github.com/icebob/fastest-validator#enum - */ -export interface RuleEnum extends RuleCustom { - /** - * Name of built-in validator - */ - type: "enum"; - /** - * The valid values - */ - values: T[]; -} - -/** - * Validation schema definition for "equal" built-in validator - * @see https://github.com/icebob/fastest-validator#equal - */ -export interface RuleEqual extends RuleCustom { - /** - * Name of built-in validator - */ - type: "equal"; - /** - * The valid value - */ - value?: T; - - /** - * Another field name - */ - field?: string; - - /** - * Strict value checking. - * - * @type {'boolean'} - * @memberof RuleEqual - */ - strict?: boolean; -} - -/** - * Validation schema definition for "forbidden" built-in validator - * @see https://github.com/icebob/fastest-validator#forbidden - */ -export interface RuleForbidden extends RuleCustom { - /** - * Name of built-in validator - */ - type: "forbidden"; - - /** - * Removes the forbidden value. - * - * @type {'boolean'} - * @memberof RuleForbidden - */ - remove?: boolean; -} - -/** - * Validation schema definition for "function" built-in validator - * @see https://github.com/icebob/fastest-validator#function - */ -export interface RuleFunction extends RuleCustom { - /** - * Name of built-in validator - */ - type: "function"; -} - -/** - * Validation schema definition for "luhn" built-in validator - * @see https://github.com/icebob/fastest-validator#luhn - */ -export interface RuleLuhn extends RuleCustom { - /** - * Name of built-in validator - */ - type: "luhn"; -} - -/** - * Validation schema definition for "mac" built-in validator - * @see https://github.com/icebob/fastest-validator#mac - */ -export interface RuleMac extends RuleCustom { - /** - * Name of built-in validator - */ - type: "mac"; -} - -/** - * Validation schema definition for "multi" built-in validator - * @see https://github.com/icebob/fastest-validator#multi - */ -export interface RuleMulti extends RuleCustom { /** - * Name of built-in validator + * Validation schema definition for "array" built-in validator + * @see https://github.com/icebob/fastest-validator#array */ - type: "multi"; - - rules: RuleCustom[] | string[]; -} + 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; + } -/** - * Validation schema definition for "number" built-in validator - * @see https://github.com/icebob/fastest-validator#number - */ -export interface RuleNumber extends RuleCustom { - /** - * Name of built-in validator - */ - type: "number"; - /** - * Minimum value - */ - min?: number; - /** - * Maximum value - */ - max?: number; - /** - * Fixed value - */ - equal?: number; - /** - * Can't be equal to this value - */ - notEqual?: number; - /** - * The value must be a non-decimal value - * @default false - */ - integer?: boolean; /** - * The value must be greater than zero - * @default false + * Validation schema definition for "boolean" built-in validator + * @see https://github.com/icebob/fastest-validator#boolean */ - positive?: boolean; - /** - * The value must be less than zero - * @default false - */ - negative?: boolean; - /** - * if true and the type is not Number, converts with Number() - * @default false - */ - convert?: boolean; -} + export interface RuleBoolean extends RuleCustom { + /** + * Name of built-in validator + */ + type: "boolean"; + /** + * if true and the type is not Boolean, try to convert. 1, "true", "1", "on" will be true. 0, "false", "0", "off" will be false. + * @default false + */ + convert?: boolean; + } -/** - * Validation schema definition for "object" built-in validator - * @see https://github.com/icebob/fastest-validator#object - */ -export interface RuleObject extends RuleCustom { - /** - * Name of built-in validator - */ - type: "object"; - /** - * If `true` any properties which are not defined on the schema will throw an error.
- * If `remove` all additional properties will be removed from the original object. It's a sanitizer, it will change the original object. - * @default false - */ - strict?: boolean | "remove"; /** - * List of properties that should be validated by this rule + * Validation schema definition for "class" built-in validator + * @see https://github.com/icebob/fastest-validator#class */ - properties?: ValidationSchema; - props?: ValidationSchema; - /** - * If set to a number, will throw if the number of props is less than that number. - */ - minProps?: number; - /** - * If set to a number, will throw if the number of props is greater than that number. - */ - maxProps?: number; -} + export interface RuleClass extends RuleCustom { + /** + * Name of built-in validator + */ + type: "class"; + /** + * Checked Class + */ + instanceOf?: T; + } -export interface RuleObjectID extends RuleCustom { - /** - * Name of built-in validator - */ - type: "objectID"; - /** - * To inject ObjectID dependency - */ - ObjectID?: any; /** - * Convert HexStringObjectID to ObjectID + * Validation schema definition for "currency" built-in validator + * @see https://github.com/icebob/fastest-validator#currency */ - convert?: boolean | "hexString"; -} + export interface RuleCurrency extends RuleCustom { + /** + * Name of built-in validator + */ + type: "currency"; + /** + * The currency symbol expected in string (as prefix) + * @default null + */ + currencySymbol?: string; + /** + * Toggle to make the currency symbol optional in string + * @default false + */ + symbolOptional?: boolean; + /** + * Thousand place separator character + * @default ',' + */ + thousandSeparator?: string; + /** + * Decimal place character + * @default '.' + */ + decimalSeparator?: string; + /** + * Custom regular expression to validate currency strings + */ + customRegex?: RegExp | string; + } -export interface RuleRecord extends RuleCustom { /** - * Name of built-in validator + * Validation schema definition for "date" built-in validator + * @see https://github.com/icebob/fastest-validator#date */ - type: "record"; - /** - * Key validation rule - */ - key?: RuleString; - /** - * Value validation rule - */ - value?: ValidationRuleObject; -} + export interface RuleDate extends RuleCustom { + /** + * Name of built-in validator + */ + type: "date"; + /** + * if true and the type is not Date, try to convert with new Date() + * @default false + */ + convert?: boolean; + } -/** - * Validation schema definition for "string" built-in validator - * @see https://github.com/icebob/fastest-validator#string - */ -export interface RuleString extends RuleCustom { - /** - * Name of built-in validator - */ - type: "string"; - /** - * If true, the validator accepts an empty string "" - * @default true - */ - empty?: boolean; - /** - * Minimum value length - */ - min?: number; - /** - * Maximum value length - */ - max?: number; - /** - * Fixed value length - */ - length?: number; - /** - * Regex pattern - */ - pattern?: string | RegExp; - /** - * The value must contain this text - */ - contains?: string; - /** - * 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 an alphanumeric string - */ - alphanum?: boolean; /** - * The value must be an alphabetic string that contains dashes + * Validation schema definition for "email" built-in validator + * @see https://github.com/icebob/fastest-validator#email */ - 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 - */ - convert?: boolean; - - trim?: boolean; - trimLeft?: boolean; - trimRight?: boolean; - - padStart?: number; - padEnd?: number; - padChar?: string; - - lowercase?: boolean; - uppercase?: boolean; - localeLowercase?: boolean; - localeUppercase?: boolean; -} + export interface RuleEmail extends RuleCustom { + /** + * Name of built-in validator + */ + type: "email"; + /** + * If true, the validator accepts an empty string "" + * @default true + */ + empty?: boolean; + /** + * Checker method. Can be quick or precise + */ + mode?: "quick" | "precise"; + /** + * Minimum value length + */ + min?: number; + /** + * Maximum value length + */ + max?: number; -/** - * Validation schema definition for "tuple" built-in validator - * @see https://github.com/icebob/fastest-validator#array - */ -export interface RuleTuple extends RuleCustom { - /** - * Name of built-in validator - */ - type: "tuple"; - /** - * Validation rules that should be applied to the corresponding element of array - */ - items?: [ValidationRule, ValidationRule]; -} + normalize?: boolean; + } -/** - * Validation schema definition for "url" built-in validator - * @see https://github.com/icebob/fastest-validator#url - */ -export interface RuleURL extends RuleCustom { - /** - * Name of built-in validator - */ - type: "url"; /** - * If true, the validator accepts an empty string "" - * @default true + * Validation schema definition for "enum" built-in validator + * @see https://github.com/icebob/fastest-validator#enum */ - empty?: boolean; -} + export interface RuleEnum extends RuleCustom { + /** + * Name of built-in validator + */ + type: "enum"; + /** + * The valid values + */ + values: T[]; + } -/** - * Validation schema definition for "uuid" built-in validator - * @see https://github.com/icebob/fastest-validator#uuid - */ -export interface RuleUUID extends RuleCustom { - /** - * Name of built-in validator - */ - type: "uuid"; /** - * UUID version in range 0-6 + * Validation schema definition for "equal" built-in validator + * @see https://github.com/icebob/fastest-validator#equal */ - version?: 0 | 1 | 2 | 3 | 4 | 5 | 6; -} + export interface RuleEqual extends RuleCustom { + /** + * Name of built-in validator + */ + type: "equal"; + /** + * The valid value + */ + value?: T; -/** - * Validation schema definition for custom inline validator - * @see https://github.com/icebob/fastest-validator#custom-validator - */ -export interface RuleCustomInline extends RuleCustom { - /** - * Name of built-in validator - */ - type: "custom"; - /** - * Custom checker function - */ - check: CheckerFunction; -} + /** + * Another field name + */ + field?: string; -/** - * Validation schema definition for custom validator - * @see https://github.com/icebob/fastest-validator#custom-validator - */ -export interface RuleCustom { - /** - * Name of custom validator that will be used in validation rules - */ - type: string; - /** - * Every field in the schema will be required by default. If you'd like to define optional fields, set optional: true. - * @default false - */ - optional?: boolean; + /** + * Strict value checking. + * + * @type {'boolean'} + * @memberof RuleEqual + */ + strict?: boolean; + } /** - * If you want disallow `undefined` value but allow `null` value, use `nullable` instead of `optional`. - * @default false + * Validation schema definition for "forbidden" built-in validator + * @see https://github.com/icebob/fastest-validator#forbidden */ - nullable?: boolean; + export interface RuleForbidden extends RuleCustom { + /** + * Name of built-in validator + */ + type: "forbidden"; - /** - * You can set your custom messages in the validator constructor - * Sometimes the standard messages are too generic. You can customise messages per validation type per field - */ - messages?: MessagesType; + /** + * Removes the forbidden value. + * + * @type {'boolean'} + * @memberof RuleForbidden + */ + remove?: boolean; + } /** - * Default value + * Validation schema definition for "function" built-in validator + * @see https://github.com/icebob/fastest-validator#function */ - default?: any; + export interface RuleFunction extends RuleCustom { + /** + * Name of built-in validator + */ + type: "function"; + } /** - * Custom checker function + * Validation schema definition for "luhn" built-in validator + * @see https://github.com/icebob/fastest-validator#luhn */ - custom?: CheckerFunction; + export interface RuleLuhn extends RuleCustom { + /** + * Name of built-in validator + */ + type: "luhn"; + } /** - * You can define any additional options for custom validators + * Validation schema definition for "mac" built-in validator + * @see https://github.com/icebob/fastest-validator#mac */ - [key: string]: any; -} + export interface RuleMac extends RuleCustom { + /** + * Name of built-in validator + */ + type: "mac"; + } -/** - * List of all possible keys that can be used for error message override - */ -export interface BuiltInMessages { - /** - * The '{field}' field is required. - */ - required?: string; - /** - * The '{field}' field must be a string. - */ - string?: string; - /** - * The '{field}' field must not be empty. - */ - stringEmpty?: string; - /** - * The '{field}' field length must be greater than or equal to {expected} characters long. - */ - stringMin?: string; - /** - * The '{field}' field length must be less than or equal to {expected} characters long. - */ - stringMax?: string; /** - * The '{field}' field length must be {expected} characters long. + * Validation schema definition for "multi" built-in validator + * @see https://github.com/icebob/fastest-validator#multi */ - stringLength?: string; - /** - * The '{field}' field fails to match the required pattern. - */ - stringPattern?: string; - /** - * The '{field}' field must contain the '{expected}' text. - */ - stringContains?: string; - /** - * The '{field}' field does not match any of the allowed values. - */ - stringEnum?: string; - /** - * The '{field}' field must be a numeric string. - */ - stringNumeric?: string; - /** - * The '{field}' field must be an alphabetic string. - */ - stringAlpha?: string; - /** - * The '{field}' field must be an alphanumeric string. - */ - stringAlphanum?: string; - /** - * The '{field}' field must be an alphadash string. - */ - stringAlphadash?: string; + export interface RuleMulti extends RuleCustom { + /** + * Name of built-in validator + */ + type: "multi"; - /** - * The '{field}' field must be a number. - */ - number?: string; - /** - * The '{field}' field must be greater than or equal to {expected}. - */ - numberMin?: string; - /** - * The '{field}' field must be less than or equal to {expected}. - */ - numberMax?: string; - /** - * The '{field}' field must be equal with {expected}. - */ - numberEqual?: string; - /** - * The '{field}' field can't be equal with {expected}. - */ - numberNotEqual?: string; - /** - * The '{field}' field must be an integer. - */ - numberInteger?: string; - /** - * The '{field}' field must be a positive number. - */ - numberPositive?: string; - /** - * The '{field}' field must be a negative number. - */ - numberNegative?: string; + rules: RuleCustom[] | string[]; + } /** - * The '{field}' field must be an array. - */ - array?: string; - /** - * The '{field}' field must not be an empty array. - */ - arrayEmpty?: string; - /** - * The '{field}' field must contain at least {expected} items. - */ - arrayMin?: string; - /** - * The '{field}' field must contain less than or equal to {expected} items. + * Validation schema definition for "number" built-in validator + * @see https://github.com/icebob/fastest-validator#number */ - arrayMax?: string; - /** - * The '{field}' field must contain {expected} items. - */ - arrayLength?: string; - /** - * The '{field}' field must contain the '{expected}' item. - */ - arrayContains?: string; - /** - * The '{field} field value '{expected}' does not match any of the allowed values. - */ - arrayEnum?: string; + export interface RuleNumber extends RuleCustom { + /** + * Name of built-in validator + */ + type: "number"; + /** + * Minimum value + */ + min?: number; + /** + * Maximum value + */ + max?: number; + /** + * Fixed value + */ + equal?: number; + /** + * Can't be equal to this value + */ + notEqual?: number; + /** + * The value must be a non-decimal value + * @default false + */ + integer?: boolean; + /** + * The value must be greater than zero + * @default false + */ + positive?: boolean; + /** + * The value must be less than zero + * @default false + */ + negative?: boolean; + /** + * if true and the type is not Number, converts with Number() + * @default false + */ + convert?: boolean; + } /** - * The '{field}' field must be a boolean. + * Validation schema definition for "object" built-in validator + * @see https://github.com/icebob/fastest-validator#object */ - boolean?: string; + export interface RuleObject extends RuleCustom { + /** + * Name of built-in validator + */ + type: "object"; + /** + * If `true` any properties which are not defined on the schema will throw an error.
+ * If `remove` all additional properties will be removed from the original object. It's a sanitizer, it will change the original object. + * @default false + */ + strict?: boolean | "remove"; + /** + * List of properties that should be validated by this rule + */ + properties?: ValidationSchema; + props?: ValidationSchema; + /** + * If set to a number, will throw if the number of props is less than that number. + */ + minProps?: number; + /** + * If set to a number, will throw if the number of props is greater than that number. + */ + maxProps?: number; + } - /** - * The '{field}' field must be a Date. - */ - date?: string; - /** - * The '{field}' field must be greater than or equal to {expected}. - */ - dateMin?: string; - /** - * The '{field}' field must be less than or equal to {expected}. - */ - dateMax?: string; + 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"; + } - /** - * The '{field}' field value '{expected}' does not match any of the allowed values. - */ - enumValue?: string; + export interface RuleRecord extends RuleCustom { + /** + * Name of built-in validator + */ + type: "record"; + /** + * Key validation rule + */ + key?: RuleString; + /** + * Value validation rule + */ + value?: ValidationRuleObject; + } /** - * The '{field}' field value must be equal to '{expected}'. - */ - equalValue?: string; - /** - * The '{field}' field value must be equal to '{expected}' field value. + * Validation schema definition for "string" built-in validator + * @see https://github.com/icebob/fastest-validator#string */ - equalField?: string; + export interface RuleString extends RuleCustom { + /** + * Name of built-in validator + */ + type: "string"; + /** + * If true, the validator accepts an empty string "" + * @default true + */ + empty?: boolean; + /** + * Minimum value length + */ + min?: number; + /** + * Maximum value length + */ + max?: number; + /** + * Fixed value length + */ + length?: number; + /** + * Regex pattern + */ + pattern?: string | RegExp; + /** + * The value must contain this text + */ + contains?: string; + /** + * 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 an alphanumeric string + */ + alphanum?: boolean; + /** + * The value must be an alphabetic string that contains dashes + */ + 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 + */ + convert?: boolean; - /** - * The '{field}' field is forbidden. - */ - forbidden?: string; + trim?: boolean; + trimLeft?: boolean; + trimRight?: boolean; - /** - * The '{field}' field must be a function. - */ - function?: string; + padStart?: number; + padEnd?: number; + padChar?: string; - /** - * The '{field}' field must be a valid e-mail. - */ - email?: string; + lowercase?: boolean; + uppercase?: boolean; + localeLowercase?: boolean; + localeUppercase?: boolean; + } /** - * The '{field}' field must be a valid checksum luhn. + * Validation schema definition for "tuple" built-in validator + * @see https://github.com/icebob/fastest-validator#array */ - luhn?: string; + export interface RuleTuple extends RuleCustom { + /** + * Name of built-in validator + */ + type: "tuple"; + /** + * Validation rules that should be applied to the corresponding element of array + */ + items?: [ValidationRule, ValidationRule]; + } /** - * The '{field}' field must be a valid MAC address. + * Validation schema definition for "url" built-in validator + * @see https://github.com/icebob/fastest-validator#url */ - mac?: string; + export interface RuleURL extends RuleCustom { + /** + * Name of built-in validator + */ + type: "url"; + /** + * If true, the validator accepts an empty string "" + * @default true + */ + empty?: boolean; + } /** - * The '{field}' must be an Object. + * Validation schema definition for "uuid" built-in validator + * @see https://github.com/icebob/fastest-validator#uuid */ - object?: string; - /** - * The object '{field}' contains forbidden keys: '{actual}'. - */ - objectStrict?: string; + export interface RuleUUID extends RuleCustom { + /** + * Name of built-in validator + */ + type: "uuid"; + /** + * UUID version in range 0-6 + */ + version?: 0 | 1 | 2 | 3 | 4 | 5 | 6; + } /** - * The '{field}' field must be a valid URL. + * Validation schema definition for custom inline validator + * @see https://github.com/icebob/fastest-validator#custom-validator */ - url?: string; + export interface RuleCustomInline extends RuleCustom { + /** + * Name of built-in validator + */ + type: "custom"; + /** + * Custom checker function + */ + check: CheckerFunction; + } /** - * The '{field}' field must be a valid UUID. - */ - uuid?: string; - /** - * The '{field}' field must be a valid UUID version provided. + * Validation schema definition for custom validator + * @see https://github.com/icebob/fastest-validator#custom-validator */ - uuidVersion?: string; -} + export interface RuleCustom { + /** + * Name of custom validator that will be used in validation rules + */ + type: string; + /** + * Every field in the schema will be required by default. If you'd like to define optional fields, set optional: true. + * @default false + */ + optional?: boolean; -/** - * Type with description of custom error messages - */ -export type MessagesType = BuiltInMessages & { [key: string]: string }; - -/** - * Union type of all possible built-in validators - */ -export type ValidationRuleObject = - | RuleAny - | RuleArray - | RuleBoolean - | RuleClass - | RuleCurrency - | RuleDate - | RuleEmail - | RuleEqual - | RuleEnum - | RuleForbidden - | RuleFunction - | RuleLuhn - | RuleMac - | RuleMulti - | RuleNumber - | RuleObject - | RuleObjectID - | RuleRecord - | RuleString - | RuleTuple - | RuleURL - | RuleUUID - | RuleCustom - | RuleCustomInline; - -/** - * Description of validation rule definition for a some property - */ -export type ValidationRule = - | ValidationRuleObject - | ValidationRuleObject[] - | ValidationRuleName; - -/** - * Definition for validation schema based on validation rules - */ -export type ValidationSchema = { - /** - * Object properties which are not specified on the schema are ignored by default. - * If you set the $$strict option to true any additional properties will result in an strictObject error. - * @default false - */ - $$strict?: boolean | "remove"; + /** + * If you want disallow `undefined` value but allow `null` value, use `nullable` instead of `optional`. + * @default false + */ + nullable?: boolean; - /** - * Enable asynchronous functionality. In this case the `validate` and `compile` methods return a `Promise`. - * @default false - */ - $$async?: boolean; + /** + * You can set your custom messages in the validator constructor + * Sometimes the standard messages are too generic. You can customise messages per validation type per field + */ + messages?: MessagesType; - /** - * Basically the validator expects that you want to validate a Javascript object. - * If you want others, you can define the root level schema. - * @default false - */ - $$root?: boolean; -} & { /** - * List of validation rules for each defined field + * Default value */ - [key in keyof T]: ValidationRule | undefined | any; - }; + default?: any; -/** - * Structure with description of validation error message - */ -export interface ValidationError { - /** - * Name of validation rule that generates this message - */ - type: keyof BuiltInMessages | string; - /** - * Field that catch validation error - */ - field: string; - /** - * Description of current validation error - */ - message?: string; - /** - * Expected value from validation rule - */ - expected?: any; - /** - * Actual value received by validation rule - */ - actual?: any; -} + /** + * Custom checker function + */ + custom?: CheckerFunction; -/** - * List of possible validator constructor options - */ -export interface ValidatorConstructorOptions { - debug?: boolean; - /** - * List of possible error messages - */ - messages?: MessagesType; + /** + * You can define any additional options for custom validators + */ + [key: string]: any; + } /** - * using checker function v2? + * List of all possible keys that can be used for error message override */ - useNewCustomCheckerFunction?: boolean; + export interface BuiltInMessages { + /** + * The '{field}' field is required. + */ + required?: string; + /** + * The '{field}' field must be a string. + */ + string?: string; + /** + * The '{field}' field must not be empty. + */ + stringEmpty?: string; + /** + * The '{field}' field length must be greater than or equal to {expected} characters long. + */ + stringMin?: string; + /** + * The '{field}' field length must be less than or equal to {expected} characters long. + */ + stringMax?: string; + /** + * The '{field}' field length must be {expected} characters long. + */ + stringLength?: string; + /** + * The '{field}' field fails to match the required pattern. + */ + stringPattern?: string; + /** + * The '{field}' field must contain the '{expected}' text. + */ + stringContains?: string; + /** + * The '{field}' field does not match any of the allowed values. + */ + stringEnum?: string; + /** + * The '{field}' field must be a numeric string. + */ + stringNumeric?: string; + /** + * The '{field}' field must be an alphabetic string. + */ + stringAlpha?: string; + /** + * The '{field}' field must be an alphanumeric string. + */ + stringAlphanum?: string; + /** + * The '{field}' field must be an alphadash string. + */ + stringAlphadash?: string; - /** - * consider null as a value? - */ - considerNullAsAValue?: boolean; + /** + * The '{field}' field must be a number. + */ + number?: string; + /** + * The '{field}' field must be greater than or equal to {expected}. + */ + numberMin?: string; + /** + * The '{field}' field must be less than or equal to {expected}. + */ + numberMax?: string; + /** + * The '{field}' field must be equal with {expected}. + */ + numberEqual?: string; + /** + * The '{field}' field can't be equal with {expected}. + */ + numberNotEqual?: string; + /** + * The '{field}' field must be an integer. + */ + numberInteger?: string; + /** + * The '{field}' field must be a positive number. + */ + numberPositive?: string; + /** + * The '{field}' field must be a negative number. + */ + numberNegative?: string; - /** - * Default settings for rules - */ - defaults?: { - [key in ValidationRuleName]: ValidationSchema; - }; + /** + * The '{field}' field must be an array. + */ + array?: string; + /** + * The '{field}' field must not be an empty array. + */ + arrayEmpty?: string; + /** + * The '{field}' field must contain at least {expected} items. + */ + arrayMin?: string; + /** + * The '{field}' field must contain less than or equal to {expected} items. + */ + arrayMax?: string; + /** + * The '{field}' field must contain {expected} items. + */ + arrayLength?: string; + /** + * The '{field}' field must contain the '{expected}' item. + */ + arrayContains?: string; + /** + * The '{field} field value '{expected}' does not match any of the allowed values. + */ + arrayEnum?: string; - /** - * For set aliases - */ - aliases?: { - [key: string]: ValidationRuleObject; - }; + /** + * The '{field}' field must be a boolean. + */ + boolean?: string; - /** - * For set custom rules. - */ - customRules?: { - [key: string]: CompilationFunction; - }; + /** + * The '{field}' field must be a Date. + */ + date?: string; + /** + * The '{field}' field must be greater than or equal to {expected}. + */ + dateMin?: string; + /** + * The '{field}' field must be less than or equal to {expected}. + */ + dateMax?: string; - /** - * For set plugins. - */ - plugins?: PluginFn[]; -} + /** + * The '{field}' field value '{expected}' does not match any of the allowed values. + */ + enumValue?: string; -export interface CompilationRule { - index: number; - ruleFunction: CompilationFunction; - schema: ValidationSchema; - messages: MessagesType; -} + /** + * The '{field}' field value must be equal to '{expected}'. + */ + equalValue?: string; + /** + * The '{field}' field value must be equal to '{expected}' field value. + */ + equalField?: string; -export interface Context { - index: number; - async: boolean; - rules: ValidationRuleObject[]; - fn: Function[]; - customs: { - [ruleName: string]: { schema: RuleCustom; messages: MessagesType }; - }; - meta?: object; - data: DATA; -} + /** + * The '{field}' field is forbidden. + */ + forbidden?: string; -export interface CheckerFunctionError { - type: string; - expected?: unknown; - actual?: unknown; - field?: string; -} + /** + * The '{field}' field must be a function. + */ + function?: string; -export type CheckerFunctionV1 = ( - value: T, - ruleSchema: ValidationRuleObject, - path: string, - parent: object | null, - context: Context -) => true | ValidationError[]; -export type CheckerFunctionV2 = ( - value: T, - errors: CheckerFunctionError[], - ruleSchema: ValidationRuleObject, - path: string, - parent: object | null, - context: Context -) => T; - -export type CheckerFunction = - | CheckerFunctionV1 - | CheckerFunctionV2; - -export type CompilationFunction = ( - rule: CompilationRule, - path: string, - context: Context -) => { sanitized?: boolean; source: string }; - -export type PluginFn = (validator: Validator) => T; - -export interface CheckFunctionOptions { - meta?: object | null; -} + /** + * The '{field}' field must be a valid e-mail. + */ + email?: string; -export interface SyncCheckFunction { - (value: any, opts?: CheckFunctionOptions): true | ValidationError[] - async: false -} + /** + * The '{field}' field must be a valid checksum luhn. + */ + luhn?: string; -export interface AsyncCheckFunction { - (value: any, opts?: CheckFunctionOptions): Promise - async: true -} + /** + * The '{field}' field must be a valid MAC address. + */ + mac?: string; -export default class Validator { - /** - * List of possible error messages - */ - messages: MessagesType; + /** + * The '{field}' must be an Object. + */ + object?: string; + /** + * The object '{field}' contains forbidden keys: '{actual}'. + */ + objectStrict?: string; - /** - * List of rules attached to current validator - */ - rules: { [key: string]: ValidationRuleObject }; + /** + * The '{field}' field must be a valid URL. + */ + url?: string; - /** - * List of aliases attached to current validator - */ - aliases: { [key: string]: ValidationRule }; + /** + * The '{field}' field must be a valid UUID. + */ + uuid?: string; + /** + * The '{field}' field must be a valid UUID version provided. + */ + uuidVersion?: string; + } /** - * Constructor of validation class - * @param {ValidatorConstructorOptions} opts List of possible validator constructor options + * Type with description of custom error messages */ - constructor(opts?: ValidatorConstructorOptions); + export type MessagesType = BuiltInMessages & { [key: string]: string }; /** - * Register a custom validation rule in validation object - * @param {string} type - * @param fn + * Union type of all possible built-in validators */ - add(type: string, fn: CompilationFunction): void; + export type ValidationRuleObject = + | RuleAny + | RuleArray + | RuleBoolean + | RuleClass + | RuleCurrency + | RuleDate + | RuleEmail + | RuleEqual + | RuleEnum + | RuleForbidden + | RuleFunction + | RuleLuhn + | RuleMac + | RuleMulti + | RuleNumber + | RuleObject + | RuleObjectID + | RuleRecord + | RuleString + | RuleTuple + | RuleURL + | RuleUUID + | RuleCustom + | RuleCustomInline; /** - * Add a message - * - * @param {String} name - * @param {String} message + * Description of validation rule definition for a some property */ - addMessage(name: string, message: string): void; + export type ValidationRule = + | ValidationRuleObject + | ValidationRuleObject[] + | ValidationRuleName; /** - * Register an alias in validation object - * @param {string} name - * @param {ValidationRuleObject} validationRule + * Definition for validation schema based on validation rules */ - alias(name: string, validationRule: ValidationRuleObject): void; + export type ValidationSchema = { + /** + * Object properties which are not specified on the schema are ignored by default. + * If you set the $$strict option to true any additional properties will result in an strictObject error. + * @default false + */ + $$strict?: boolean | "remove"; - /** - * Add a plugin - * - * @param {Function} fn - */ - plugin(fn: PluginFn): T; + /** + * Enable asynchronous functionality. In this case the `validate` and `compile` methods return a `Promise`. + * @default false + */ + $$async?: boolean; + + /** + * Basically the validator expects that you want to validate a Javascript object. + * If you want others, you can define the root level schema. + * @default false + */ + $$root?: boolean; + } & { + /** + * List of validation rules for each defined field + */ + [key in keyof T]: ValidationRule | undefined | any; + }; /** - * Build error message - * @return {ValidationError} - * @param {Object} opts - * @param {String} opts.type - * @param {String} opts.field - * @param {any=} opts.expected - * @param {any=} opts.actual - * @param {MessagesType} opts.messages + * Structure with description of validation error message */ - makeError(opts: { - type: keyof MessagesType; - field?: string; + export interface ValidationError { + /** + * Name of validation rule that generates this message + */ + type: keyof BuiltInMessages | string; + /** + * Field that catch validation error + */ + field: string; + /** + * Description of current validation error + */ + message?: string; + /** + * Expected value from validation rule + */ expected?: any; + /** + * Actual value received by validation rule + */ actual?: any; - messages: MessagesType; - }): string; + } /** - * Compile validator functions that working up 100 times faster that native validation process - * @param {ValidationSchema | ValidationSchema[]} schema Validation schema definition that should be used for validation - * @return {(value: any) => (true | ValidationError[])} function that can be used next for validation of current schema + * List of possible validator constructor options */ - compile( - schema: ValidationSchema | ValidationSchema[] - ): SyncCheckFunction | AsyncCheckFunction; + export interface ValidatorConstructorOptions { + debug?: boolean; + /** + * List of possible error messages + */ + messages?: MessagesType; - /** - * Native validation method to validate obj - * @param {any} value that should be validated - * @param {ValidationSchema} schema Validation schema definition that should be used for validation - * @return {{true} | ValidationError[]} - */ - validate( - value: any, - schema: ValidationSchema - ): true | ValidationError[] | Promise; + /** + * using checker function v2? + */ + useNewCustomCheckerFunction?: boolean; - /** - * Get defined in validator rule - * @param {ValidationRuleName | ValidationRuleName[]} name List or name of defined rule - * @return {ValidationRule} - */ - getRuleFromSchema( - name: ValidationRuleName | ValidationRuleName[] | { [key: string]: unknown } - ): { - messages: MessagesType; + /** + * consider null as a value? + */ + considerNullAsAValue?: boolean; + + /** + * Default settings for rules + */ + defaults?: { + [key in ValidationRuleName]: ValidationSchema; + }; + + /** + * For set aliases + */ + aliases?: { + [key: string]: ValidationRuleObject; + }; + + /** + * For set custom rules. + */ + customRules?: { + [key: string]: CompilationFunction; + }; + + /** + * For set plugins. + */ + plugins?: PluginFn[]; + } + + export interface CompilationRule { + index: number; + ruleFunction: CompilationFunction; schema: ValidationSchema; - ruleFunction: Function; - }; + messages: MessagesType; + } + + export interface Context { + index: number; + async: boolean; + rules: ValidationRuleObject[]; + fn: Function[]; + customs: { + [ruleName: string]: { schema: RuleCustom; messages: MessagesType }; + }; + meta?: object; + data: DATA; + } + + export interface CheckerFunctionError { + type: string; + expected?: unknown; + actual?: unknown; + field?: string; + } + + export type CheckerFunctionV1 = ( + value: T, + ruleSchema: ValidationRuleObject, + path: string, + parent: object | null, + context: Context + ) => true | ValidationError[]; + export type CheckerFunctionV2 = ( + value: T, + errors: CheckerFunctionError[], + ruleSchema: ValidationRuleObject, + path: string, + parent: object | null, + context: Context + ) => T; + + export type CheckerFunction = + | CheckerFunctionV1 + | CheckerFunctionV2; + + export type CompilationFunction = ( + rule: CompilationRule, + path: string, + context: Context + ) => { sanitized?: boolean; source: string }; + + export type PluginFn = (validator: Validator) => T; + + export interface CheckFunctionOptions { + meta?: object | null; + } + + export interface SyncCheckFunction { + (value: any, opts?: CheckFunctionOptions): true | ValidationError[] + async: false + } + + export interface AsyncCheckFunction { + (value: any, opts?: CheckFunctionOptions): Promise + async: true + } + + export default class Validator { + /** + * List of possible error messages + */ + messages: MessagesType; - /** - * Normalize a schema, type or short hand definition by expanding it to a full form. The 'normalized' - * form is the equivalent schema with any short hands undone. This ensure that each rule; always includes - * a 'type' key, arrays always have an 'items' key, 'multi' always have a 'rules' key and objects always - * have their properties defined in a 'props' key - * - * @param { ValidationSchema | string | any } value The value to normalize - * @return {ValidationRule | ValidationSchema } The normalized form of the given rule or schema - */ - normalize( - value: ValidationSchema | string | any - ): ValidationRule | ValidationSchema + /** + * List of rules attached to current validator + */ + rules: { [key: string]: ValidationRuleObject }; + + /** + * List of aliases attached to current validator + */ + aliases: { [key: string]: ValidationRule }; + + /** + * Constructor of validation class + * @param {ValidatorConstructorOptions} opts List of possible validator constructor options + */ + constructor(opts?: ValidatorConstructorOptions); + + /** + * Register a custom validation rule in validation object + * @param {string} type + * @param fn + */ + add(type: string, fn: CompilationFunction): void; + + /** + * Add a message + * + * @param {String} name + * @param {String} message + */ + addMessage(name: string, message: string): void; + + /** + * Register an alias in validation object + * @param {string} name + * @param {ValidationRuleObject} validationRule + */ + alias(name: string, validationRule: ValidationRuleObject): void; + + /** + * Add a plugin + * + * @param {Function} fn + */ + plugin(fn: PluginFn): T; + + /** + * Build error message + * @return {ValidationError} + * @param {Object} opts + * @param {String} opts.type + * @param {String} opts.field + * @param {any=} opts.expected + * @param {any=} opts.actual + * @param {MessagesType} opts.messages + */ + makeError(opts: { + type: keyof MessagesType; + field?: string; + expected?: any; + actual?: any; + messages: MessagesType; + }): string; + + /** + * Compile validator functions that working up 100 times faster that native validation process + * @param {ValidationSchema | ValidationSchema[]} schema Validation schema definition that should be used for validation + * @return {(value: any) => (true | ValidationError[])} function that can be used next for validation of current schema + */ + compile( + schema: ValidationSchema | ValidationSchema[] + ): SyncCheckFunction | AsyncCheckFunction; + + /** + * Native validation method to validate obj + * @param {any} value that should be validated + * @param {ValidationSchema} schema Validation schema definition that should be used for validation + * @return {{true} | ValidationError[]} + */ + validate( + value: any, + schema: ValidationSchema + ): true | ValidationError[] | Promise; + + /** + * Get defined in validator rule + * @param {ValidationRuleName | ValidationRuleName[]} name List or name of defined rule + * @return {ValidationRule} + */ + getRuleFromSchema( + name: ValidationRuleName | ValidationRuleName[] | { [key: string]: unknown } + ): { + messages: MessagesType; + schema: ValidationSchema; + ruleFunction: Function; + }; + + /** + * Normalize a schema, type or short hand definition by expanding it to a full form. The 'normalized' + * form is the equivalent schema with any short hands undone. This ensure that each rule; always includes + * a 'type' key, arrays always have an 'items' key, 'multi' always have a 'rules' key and objects always + * have their properties defined in a 'props' key + * + * @param { ValidationSchema | string | any } value The value to normalize + * @return {ValidationRule | ValidationSchema } The normalized form of the given rule or schema + */ + normalize( + value: ValidationSchema | string | any + ): ValidationRule | ValidationSchema + } }