Skip to content

Commit

Permalink
fix(types): use right types for rules (#2296)
Browse files Browse the repository at this point in the history
  • Loading branch information
orblazer authored and logaretm committed Sep 5, 2019
1 parent 4aff7da commit caf1a88
Show file tree
Hide file tree
Showing 27 changed files with 45 additions and 37 deletions.
2 changes: 1 addition & 1 deletion 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<string, any> = {}): boolean => {
if (Array.isArray(value)) {
return value.every(val => validate(val, { locale }));
}
Expand Down
2 changes: 1 addition & 1 deletion 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<string, any> = {}): boolean => {
if (Array.isArray(value)) {
return value.every(val => validate(val, { locale }));
}
Expand Down
2 changes: 1 addition & 1 deletion 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<string, any> = {}): boolean => {
if (Array.isArray(value)) {
return value.every(val => validate(val, { locale }));
}
Expand Down
2 changes: 1 addition & 1 deletion 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<string, any> = {}): boolean => {
if (Array.isArray(value)) {
return value.every(val => validate(val, { locale }));
}
Expand Down
4 changes: 2 additions & 2 deletions 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<string, any> = {}): boolean => {
if (Array.isArray(value)) {
return value.every(val => !!validate(val, { min, max }));
}
Expand Down
4 changes: 2 additions & 2 deletions 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, any>) => String(value) === String(target);

const params: RuleParamSchema[] = [
{
Expand Down
4 changes: 2 additions & 2 deletions 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<string, any>): boolean => {
if (Array.isArray(value)) {
return value.every(val => validate(val, { length }));
}
Expand Down
2 changes: 1 addition & 1 deletion src/rules/dimensions.ts
Expand Up @@ -12,7 +12,7 @@ const validateImage = (file: File, width: number, height: number): Promise<boole
});
};

const validate = (files: File | File[], { width, height }: any) => {
const validate = (files: File | File[], { width, height }: Record<string, any>) => {
const list = [];
files = Array.isArray(files) ? files : [files];
for (let i = 0; i < files.length; i++) {
Expand Down
2 changes: 1 addition & 1 deletion 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<string, any> = {}) => {
// 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)) {
Expand Down
3 changes: 2 additions & 1 deletion 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);
};

Expand Down
2 changes: 1 addition & 1 deletion src/rules/ext.ts
@@ -1,4 +1,4 @@
const validate = (files: File | File[], extensions: string[]) => {
const validate = (files: File | File[], extensions: string[] | Record<string, any>) => {
const regex = new RegExp(`.(${extensions.join('|')})$`, 'i');
if (Array.isArray(files)) {
return files.every(file => regex.test(file.name));
Expand Down
4 changes: 3 additions & 1 deletion 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)));
}
Expand Down
2 changes: 1 addition & 1 deletion src/rules/is.ts
@@ -1,6 +1,6 @@
import { RuleParamSchema } from '../types';

const validate = (value: any, { other }: any) => {
const validate = (value: any, { other }: Record<string, any>) => {
return value === other;
};

Expand Down
2 changes: 1 addition & 1 deletion 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<string, any>) => {
return value !== other;
};

Expand Down
2 changes: 1 addition & 1 deletion 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<string, any>) => {
if (isNullOrUndefined(value)) {
return false;
}
Expand Down
4 changes: 2 additions & 2 deletions 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<string, any>): boolean => {
if (isNullOrUndefined(value)) {
return length >= 0;
}
Expand Down
4 changes: 2 additions & 2 deletions 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<string, any>) => {
if (isNullOrUndefined(value) || value === '') {
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion src/rules/mimes.ts
@@ -1,4 +1,4 @@
const validate = (files: File | File[], mimes: any) => {
const validate = (files: File | File[], mimes: string[] | Record<string, any>) => {
const regex = new RegExp(`${mimes.join('|').replace('*', '.+')}$`, 'i');
if (Array.isArray(files)) {
return files.every(file => regex.test(file.type));
Expand Down
4 changes: 2 additions & 2 deletions 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<string, any>): boolean => {
if (isNullOrUndefined(value)) {
return false;
}
Expand Down
4 changes: 2 additions & 2 deletions 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<string, any>): boolean => {
if (isNullOrUndefined(value) || value === '') {
return false;
}
Expand Down
6 changes: 4 additions & 2 deletions 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);
Expand Down
5 changes: 3 additions & 2 deletions 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;
});
Expand Down
4 changes: 2 additions & 2 deletions 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<string, any>): boolean => {
if (Array.isArray(value)) {
return value.every(val => validate(val, { regex: regex }));
}
Expand Down
2 changes: 1 addition & 1 deletion 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<string, any> = { allowFalse: true }) => {
const result = {
valid: false,
required: true
Expand Down
4 changes: 2 additions & 2 deletions 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<string, any>) => {
const required = values.includes(String(target).trim());

if (!required) {
return {
Expand Down
2 changes: 1 addition & 1 deletion 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<string, any>) => {
if (isNaN(size)) {
return false;
}
Expand Down
2 changes: 2 additions & 0 deletions src/types.ts
Expand Up @@ -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<T> = {
Expand Down

0 comments on commit caf1a88

Please sign in to comment.