diff --git a/src/rules/alpha.ts b/src/rules/alpha.ts index 01326983b..6353ff2dd 100644 --- a/src/rules/alpha.ts +++ b/src/rules/alpha.ts @@ -1,7 +1,7 @@ import { alpha } from './alpha_helper'; import { RuleParamSchema } from '../types'; -const validate = (value: any, { locale = '' } = {}): boolean => { +const validate = (value: string | string[], { locale = '' }: Record = {}): boolean => { if (Array.isArray(value)) { return value.every(val => validate(val, { locale })); } diff --git a/src/rules/alpha_dash.ts b/src/rules/alpha_dash.ts index 8ba7e0114..1c1453b4c 100644 --- a/src/rules/alpha_dash.ts +++ b/src/rules/alpha_dash.ts @@ -1,7 +1,7 @@ import { alphaDash } from './alpha_helper'; import { RuleParamSchema } from '../types'; -const validate = (value: any, { locale = '' }: any = {}): boolean => { +const validate = (value: string | string[], { locale = '' }: Record = {}): boolean => { if (Array.isArray(value)) { return value.every(val => validate(val, { locale })); } diff --git a/src/rules/alpha_num.ts b/src/rules/alpha_num.ts index efddd4469..f1eb5a61d 100644 --- a/src/rules/alpha_num.ts +++ b/src/rules/alpha_num.ts @@ -1,7 +1,7 @@ import { alphanumeric } from './alpha_helper'; import { RuleParamSchema } from '../types'; -const validate = (value: any, { locale = '' }: any = {}): boolean => { +const validate = (value: string | string[], { locale = '' }: Record = {}): boolean => { if (Array.isArray(value)) { return value.every(val => validate(val, { locale })); } diff --git a/src/rules/alpha_spaces.ts b/src/rules/alpha_spaces.ts index cb96166d3..a5766f8eb 100644 --- a/src/rules/alpha_spaces.ts +++ b/src/rules/alpha_spaces.ts @@ -1,7 +1,7 @@ import { alphaSpaces } from './alpha_helper'; import { RuleParamSchema } from '../types'; -const validate = (value: any, { locale = '' } = {}): boolean => { +const validate = (value: string | string[], { locale = '' }: Record = {}): boolean => { if (Array.isArray(value)) { return value.every(val => validate(val, { locale })); } diff --git a/src/rules/between.ts b/src/rules/between.ts index 91a28fda4..078ae4570 100644 --- a/src/rules/between.ts +++ b/src/rules/between.ts @@ -1,6 +1,6 @@ -import { ValidationRuleFunction, RuleParamSchema } from '../types'; +import { RuleParamSchema, StringOrNumber } from '../types'; -const validate: ValidationRuleFunction = (value, { min, max }: any = {}) => { +const validate = (value: StringOrNumber | StringOrNumber[], { min, max }: Record = {}): boolean => { if (Array.isArray(value)) { return value.every(val => !!validate(val, { min, max })); } diff --git a/src/rules/confirmed.ts b/src/rules/confirmed.ts index c22479a14..40f8d5186 100644 --- a/src/rules/confirmed.ts +++ b/src/rules/confirmed.ts @@ -1,6 +1,6 @@ -import { ValidationRuleFunction, RuleParamSchema } from '../types'; +import { RuleParamSchema } from '../types'; -const validate: ValidationRuleFunction = (value, { target }: any) => String(value) === String(target); +const validate = (value: string, { target }: Record) => String(value) === String(target); const params: RuleParamSchema[] = [ { diff --git a/src/rules/digits.ts b/src/rules/digits.ts index ade027b66..f28ba01fe 100644 --- a/src/rules/digits.ts +++ b/src/rules/digits.ts @@ -1,6 +1,6 @@ -import { RuleParamSchema } from '../types'; +import { RuleParamSchema, StringOrNumber } from '../types'; -const validate = (value: any, { length }: any): boolean => { +const validate = (value: StringOrNumber | StringOrNumber[], { length }: Record): boolean => { if (Array.isArray(value)) { return value.every(val => validate(val, { length })); } diff --git a/src/rules/dimensions.ts b/src/rules/dimensions.ts index dc86e895f..cde8cdd82 100644 --- a/src/rules/dimensions.ts +++ b/src/rules/dimensions.ts @@ -12,7 +12,7 @@ const validateImage = (file: File, width: number, height: number): Promise { +const validate = (files: File | File[], { width, height }: Record) => { const list = []; files = Array.isArray(files) ? files : [files]; for (let i = 0; i < files.length; i++) { diff --git a/src/rules/email.ts b/src/rules/email.ts index e069fbcf7..77c8723ed 100644 --- a/src/rules/email.ts +++ b/src/rules/email.ts @@ -1,6 +1,6 @@ import { RuleParamSchema } from '../types'; -const validate = (value: string | string[], { multiple }: any = {}) => { +const validate = (value: string | string[], { multiple }: Record = {}) => { // eslint-disable-next-line const re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; if (multiple && !Array.isArray(value)) { diff --git a/src/rules/excluded.ts b/src/rules/excluded.ts index 5bb6ce2c2..30b8de446 100644 --- a/src/rules/excluded.ts +++ b/src/rules/excluded.ts @@ -1,6 +1,7 @@ import { validate as includes } from './oneOf'; +import { ValidationRuleFunction } from '../types'; -const validate = (value: any, args: any[]) => { +const validate: ValidationRuleFunction = (value, args) => { return !includes(value, args); }; diff --git a/src/rules/ext.ts b/src/rules/ext.ts index 83c0a4405..92e3c1d0d 100644 --- a/src/rules/ext.ts +++ b/src/rules/ext.ts @@ -1,4 +1,4 @@ -const validate = (files: File | File[], extensions: string[]) => { +const validate = (files: File | File[], extensions: string[] | Record) => { const regex = new RegExp(`.(${extensions.join('|')})$`, 'i'); if (Array.isArray(files)) { return files.every(file => regex.test(file.name)); diff --git a/src/rules/integer.ts b/src/rules/integer.ts index 7cdfdb02c..e718e11fb 100644 --- a/src/rules/integer.ts +++ b/src/rules/integer.ts @@ -1,4 +1,6 @@ -const validate = (value: any) => { +import { StringOrNumber } from '../types'; + +const validate = (value: StringOrNumber | StringOrNumber[]) => { if (Array.isArray(value)) { return value.every(val => /^-?[0-9]+$/.test(String(val))); } diff --git a/src/rules/is.ts b/src/rules/is.ts index 828cc576d..d7b2a6296 100644 --- a/src/rules/is.ts +++ b/src/rules/is.ts @@ -1,6 +1,6 @@ import { RuleParamSchema } from '../types'; -const validate = (value: any, { other }: any) => { +const validate = (value: any, { other }: Record) => { return value === other; }; diff --git a/src/rules/is_not.ts b/src/rules/is_not.ts index eef5ae14b..1f3462b48 100644 --- a/src/rules/is_not.ts +++ b/src/rules/is_not.ts @@ -1,6 +1,6 @@ import { RuleParamSchema } from '../types'; -const validate = (value: any, { other }: any) => { +const validate = (value: any, { other }: Record) => { return value !== other; }; diff --git a/src/rules/length.ts b/src/rules/length.ts index 4b23f8521..cfa6e1ef4 100644 --- a/src/rules/length.ts +++ b/src/rules/length.ts @@ -1,7 +1,7 @@ import { isNullOrUndefined, toArray } from '../utils'; import { RuleParamSchema } from '../types'; -const validate = (value: any, { length }: any) => { +const validate = (value: string | number | string[], { length }: Record) => { if (isNullOrUndefined(value)) { return false; } diff --git a/src/rules/max.ts b/src/rules/max.ts index 4a1c14385..b011c4bbb 100644 --- a/src/rules/max.ts +++ b/src/rules/max.ts @@ -1,7 +1,7 @@ import { isNullOrUndefined } from '../utils'; -import { RuleParamSchema } from '../types'; +import { RuleParamSchema, StringOrNumber } from '../types'; -const validate = (value: any, { length }: any): boolean => { +const validate = (value: StringOrNumber | StringOrNumber[], { length }: Record): boolean => { if (isNullOrUndefined(value)) { return length >= 0; } diff --git a/src/rules/max_value.ts b/src/rules/max_value.ts index 9df5d237b..2f1fad027 100644 --- a/src/rules/max_value.ts +++ b/src/rules/max_value.ts @@ -1,7 +1,7 @@ import { isNullOrUndefined } from '../utils'; -import { RuleParamSchema } from '../types'; +import { RuleParamSchema, ValidationRuleFunction, StringOrNumber } from '../types'; -const validate = (value: any, { max }: any): boolean => { +const validate: ValidationRuleFunction = (value: StringOrNumber | StringOrNumber[], { max }: Record) => { if (isNullOrUndefined(value) || value === '') { return false; } diff --git a/src/rules/mimes.ts b/src/rules/mimes.ts index 8814e0597..913c87ee9 100644 --- a/src/rules/mimes.ts +++ b/src/rules/mimes.ts @@ -1,4 +1,4 @@ -const validate = (files: File | File[], mimes: any) => { +const validate = (files: File | File[], mimes: string[] | Record) => { const regex = new RegExp(`${mimes.join('|').replace('*', '.+')}$`, 'i'); if (Array.isArray(files)) { return files.every(file => regex.test(file.type)); diff --git a/src/rules/min.ts b/src/rules/min.ts index 4ec4aca38..bd816a3ff 100644 --- a/src/rules/min.ts +++ b/src/rules/min.ts @@ -1,7 +1,7 @@ import { isNullOrUndefined } from '../utils'; -import { RuleParamSchema } from '../types'; +import { RuleParamSchema, StringOrNumber } from '../types'; -const validate = (value: any, { length }: any): boolean => { +const validate = (value: StringOrNumber | StringOrNumber[], { length }: Record): boolean => { if (isNullOrUndefined(value)) { return false; } diff --git a/src/rules/min_value.ts b/src/rules/min_value.ts index 894ba233f..ce610a317 100644 --- a/src/rules/min_value.ts +++ b/src/rules/min_value.ts @@ -1,7 +1,7 @@ import { isNullOrUndefined } from '../utils'; -import { RuleParamSchema } from '../types'; +import { RuleParamSchema, StringOrNumber } from '../types'; -const validate = (value: any, { min }: any): boolean => { +const validate = (value: StringOrNumber | StringOrNumber[], { min }: Record): boolean => { if (isNullOrUndefined(value) || value === '') { return false; } diff --git a/src/rules/numeric.ts b/src/rules/numeric.ts index ed40818f2..7124c1446 100644 --- a/src/rules/numeric.ts +++ b/src/rules/numeric.ts @@ -1,8 +1,10 @@ +import { StringOrNumber } from '../types'; + const ar = /^[٠١٢٣٤٥٦٧٨٩]+$/; const en = /^[0-9]+$/; -const validate = (value: any) => { - const testValue = (val: any) => { +const validate = (value: StringOrNumber | StringOrNumber[]) => { + const testValue = (val: StringOrNumber) => { const strValue = String(val); return en.test(strValue) || ar.test(strValue); diff --git a/src/rules/oneOf.ts b/src/rules/oneOf.ts index 2c24fe0d0..8266fd697 100644 --- a/src/rules/oneOf.ts +++ b/src/rules/oneOf.ts @@ -1,11 +1,12 @@ import { toArray } from '../utils'; +import { ValidationRuleFunction } from '../types'; -const validate = (value: any, options: any[]): boolean => { +const validate: ValidationRuleFunction = (value, options) => { if (Array.isArray(value)) { return value.every(val => validate(val, options)); } - return toArray(options).some(item => { + return toArray(options as any[]).some(item => { // eslint-disable-next-line return item == value; }); diff --git a/src/rules/regex.ts b/src/rules/regex.ts index 313a56208..66cdf83b0 100644 --- a/src/rules/regex.ts +++ b/src/rules/regex.ts @@ -1,6 +1,6 @@ -import { RuleParamSchema } from '../types'; +import { RuleParamSchema, StringOrNumber } from '../types'; -const validate = (value: any, { regex }: any): boolean => { +const validate = (value: StringOrNumber | StringOrNumber[], { regex }: Record): boolean => { if (Array.isArray(value)) { return value.every(val => validate(val, { regex: regex })); } diff --git a/src/rules/required.ts b/src/rules/required.ts index c9962eacc..3154c4259 100644 --- a/src/rules/required.ts +++ b/src/rules/required.ts @@ -1,7 +1,7 @@ import { isEmptyArray, isNullOrUndefined } from '../utils'; import { RuleParamSchema } from '../types'; -const validate = (value: any, { allowFalse }: any = { allowFalse: true }) => { +const validate = (value: any, { allowFalse }: Record = { allowFalse: true }) => { const result = { valid: false, required: true diff --git a/src/rules/required_if.ts b/src/rules/required_if.ts index 003e43cbe..e28521c94 100644 --- a/src/rules/required_if.ts +++ b/src/rules/required_if.ts @@ -1,8 +1,8 @@ import { isEmptyArray } from '../utils'; import { RuleParamSchema } from '../types'; -const validate = (value: any, { target, values }: any) => { - let required = values.includes(String(target).trim()); +const validate = (value: any, { target, values }: Record) => { + const required = values.includes(String(target).trim()); if (!required) { return { diff --git a/src/rules/size.ts b/src/rules/size.ts index 858028890..d6936c0ca 100644 --- a/src/rules/size.ts +++ b/src/rules/size.ts @@ -1,6 +1,6 @@ import { RuleParamSchema } from '../types'; -const validate = (files: File | File[], { size }: any) => { +const validate = (files: File | File[], { size }: Record) => { if (isNaN(size)) { return false; } diff --git a/src/types.ts b/src/types.ts index 3928f80da..710bd1c8c 100644 --- a/src/types.ts +++ b/src/types.ts @@ -50,6 +50,8 @@ export interface ValidationRuleSchema { export type ValidationRule = ValidationRuleFunction | ValidationRuleSchema; +export type StringOrNumber = string | number; + // Extracts explicit keys of an interface without index signature // https://stackoverflow.com/questions/51465182/typescript-remove-index-signature-using-mapped-types export type KnownKeys = {