Skip to content

Commit

Permalink
chore: all ts
Browse files Browse the repository at this point in the history
  • Loading branch information
zombieJ committed May 31, 2021
1 parent afa2d12 commit 8c1c08e
Show file tree
Hide file tree
Showing 17 changed files with 88 additions and 172 deletions.
20 changes: 19 additions & 1 deletion src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,25 @@ export type ExecuteRule = (
source: Values,
errors: string[],
options: ValidateOption,
type?: RuleType,
type?: string,
) => void;

/**
* Performs validation for any type.
*
* @param rule The validation rule.
* @param value The value of the field on the source object.
* @param callback The callback function.
* @param source The source object being validated.
* @param options The validation options.
* @param options.messages The validation messages.
*/
export type ExecuteValidator = (
rule: InternalRuleItem,
value: Value,
callback: (error?: string[]) => void,
source: Values,
options: ValidateOption,
) => void;

// >>>>> Message
Expand Down
2 changes: 1 addition & 1 deletion src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ function isNativeStringType(type: RuleType) {
);
}

export function isEmptyValue(value: Value, type: RuleType) {
export function isEmptyValue(value: Value, type?: RuleType) {
if (value === undefined || value === null) {
return true;
}
Expand Down
17 changes: 4 additions & 13 deletions src/validator/any.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,9 @@
import { ExecuteValidator } from '../interface';
import rules from '../rule';
import { isEmptyValue } from '../util';

/**
* Performs validation for any type.
*
* @param rule The validation rule.
* @param value The value of the field on the source object.
* @param callback The callback function.
* @param source The source object being validated.
* @param options The validation options.
* @param options.messages The validation messages.
*/
function any(rule, value, callback, source, options) {
const errors = [];
const any: ExecuteValidator = (rule, value, callback, source, options) => {
const errors: string[] = [];
const validate =
rule.required || (!rule.required && source.hasOwnProperty(rule.field));
if (validate) {
Expand All @@ -22,6 +13,6 @@ function any(rule, value, callback, source, options) {
rules.required(rule, value, source, errors, options);
}
callback(errors);
}
};

export default any;
19 changes: 5 additions & 14 deletions src/validator/array.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,8 @@
import { ExecuteValidator } from '../interface';
import rules from '../rule/index';
import { isEmptyValue } from '../util';
/**
* Validates an array.
*
* @param rule The validation rule.
* @param value The value of the field on the source object.
* @param callback The callback function.
* @param source The source object being validated.
* @param options The validation options.
* @param options.messages The validation messages.
*/
function array(rule, value, callback, source, options) {
const errors = [];

const array: ExecuteValidator = (rule, value, callback, source, options) => {
const errors: string[] = [];
const validate =
rule.required || (!rule.required && source.hasOwnProperty(rule.field));
if (validate) {
Expand All @@ -25,6 +16,6 @@ function array(rule, value, callback, source, options) {
}
}
callback(errors);
}
};

export default array;
17 changes: 4 additions & 13 deletions src/validator/boolean.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,9 @@
import { isEmptyValue } from '../util';
import rules from '../rule';
import { ExecuteValidator } from '../interface';

/**
* Validates a boolean.
*
* @param rule The validation rule.
* @param value The value of the field on the source object.
* @param callback The callback function.
* @param source The source object being validated.
* @param options The validation options.
* @param options.messages The validation messages.
*/
function boolean(rule, value, callback, source, options) {
const errors = [];
const boolean: ExecuteValidator = (rule, value, callback, source, options) => {
const errors: string[] = [];
const validate =
rule.required || (!rule.required && source.hasOwnProperty(rule.field));
if (validate) {
Expand All @@ -25,6 +16,6 @@ function boolean(rule, value, callback, source, options) {
}
}
callback(errors);
}
};

export default boolean;
7 changes: 4 additions & 3 deletions src/validator/date.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { ExecuteValidator } from '../interface';
import rules from '../rule';
import { isEmptyValue } from '../util';

function date(rule, value, callback, source, options) {
const date: ExecuteValidator = (rule, value, callback, source, options) => {
// console.log('integer rule called %j', rule);
const errors = [];
const errors: string[] = [];
const validate =
rule.required || (!rule.required && source.hasOwnProperty(rule.field));
// console.log('validate on %s value', value);
Expand All @@ -28,6 +29,6 @@ function date(rule, value, callback, source, options) {
}
}
callback(errors);
}
};

export default date;
25 changes: 11 additions & 14 deletions src/validator/enum.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
import { ExecuteValidator } from '../interface';
import rules from '../rule';
import { isEmptyValue } from '../util';

const ENUM = 'enum';
const ENUM = 'enum' as const;

/**
* Validates an enumerable list.
*
* @param rule The validation rule.
* @param value The value of the field on the source object.
* @param callback The callback function.
* @param source The source object being validated.
* @param options The validation options.
* @param options.messages The validation messages.
*/
function enumerable(rule, value, callback, source, options) {
const errors = [];
const enumerable: ExecuteValidator = (
rule,
value,
callback,
source,
options,
) => {
const errors: string[] = [];
const validate =
rule.required || (!rule.required && source.hasOwnProperty(rule.field));
if (validate) {
Expand All @@ -27,6 +24,6 @@ function enumerable(rule, value, callback, source, options) {
}
}
callback(errors);
}
};

export default enumerable;
17 changes: 4 additions & 13 deletions src/validator/float.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,9 @@
import { ExecuteValidator } from '../interface';
import rules from '../rule';
import { isEmptyValue } from '../util';

/**
* Validates a number is a floating point number.
*
* @param rule The validation rule.
* @param value The value of the field on the source object.
* @param callback The callback function.
* @param source The source object being validated.
* @param options The validation options.
* @param options.messages The validation messages.
*/
function floatFn(rule, value, callback, source, options) {
const errors = [];
const floatFn: ExecuteValidator = (rule, value, callback, source, options) => {
const errors: string[] = [];
const validate =
rule.required || (!rule.required && source.hasOwnProperty(rule.field));
if (validate) {
Expand All @@ -26,6 +17,6 @@ function floatFn(rule, value, callback, source, options) {
}
}
callback(errors);
}
};

export default floatFn;
17 changes: 4 additions & 13 deletions src/validator/integer.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,9 @@
import { ExecuteValidator } from '../interface';
import rules from '../rule';
import { isEmptyValue } from '../util';

/**
* Validates a number is an integer.
*
* @param rule The validation rule.
* @param value The value of the field on the source object.
* @param callback The callback function.
* @param source The source object being validated.
* @param options The validation options.
* @param options.messages The validation messages.
*/
function integer(rule, value, callback, source, options) {
const errors = [];
const integer: ExecuteValidator = (rule, value, callback, source, options) => {
const errors: string[] = [];
const validate =
rule.required || (!rule.required && source.hasOwnProperty(rule.field));
if (validate) {
Expand All @@ -26,6 +17,6 @@ function integer(rule, value, callback, source, options) {
}
}
callback(errors);
}
};

export default integer;
17 changes: 4 additions & 13 deletions src/validator/method.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,9 @@
import { ExecuteValidator } from '../interface';
import rules from '../rule';
import { isEmptyValue } from '../util';

/**
* Validates a function.
*
* @param rule The validation rule.
* @param value The value of the field on the source object.
* @param callback The callback function.
* @param source The source object being validated.
* @param options The validation options.
* @param options.messages The validation messages.
*/
function method(rule, value, callback, source, options) {
const errors = [];
const method: ExecuteValidator = (rule, value, callback, source, options) => {
const errors: string[] = [];
const validate =
rule.required || (!rule.required && source.hasOwnProperty(rule.field));
if (validate) {
Expand All @@ -25,6 +16,6 @@ function method(rule, value, callback, source, options) {
}
}
callback(errors);
}
};

export default method;
17 changes: 4 additions & 13 deletions src/validator/number.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,9 @@
import { ExecuteValidator } from '../interface';
import rules from '../rule';
import { isEmptyValue } from '../util';

/**
* Validates a number.
*
* @param rule The validation rule.
* @param value The value of the field on the source object.
* @param callback The callback function.
* @param source The source object being validated.
* @param options The validation options.
* @param options.messages The validation messages.
*/
function number(rule, value, callback, source, options) {
const errors = [];
const number: ExecuteValidator = (rule, value, callback, source, options) => {
const errors: string[] = [];
const validate =
rule.required || (!rule.required && source.hasOwnProperty(rule.field));
if (validate) {
Expand All @@ -29,6 +20,6 @@ function number(rule, value, callback, source, options) {
}
}
callback(errors);
}
};

export default number;
17 changes: 4 additions & 13 deletions src/validator/object.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,9 @@
import { ExecuteValidator } from '../interface';
import rules from '../rule';
import { isEmptyValue } from '../util';

/**
* Validates an object.
*
* @param rule The validation rule.
* @param value The value of the field on the source object.
* @param callback The callback function.
* @param source The source object being validated.
* @param options The validation options.
* @param options.messages The validation messages.
*/
function object(rule, value, callback, source, options) {
const errors = [];
const object: ExecuteValidator = (rule, value, callback, source, options) => {
const errors: string[] = [];
const validate =
rule.required || (!rule.required && source.hasOwnProperty(rule.field));
if (validate) {
Expand All @@ -25,6 +16,6 @@ function object(rule, value, callback, source, options) {
}
}
callback(errors);
}
};

export default object;
20 changes: 4 additions & 16 deletions src/validator/pattern.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,9 @@
import { ExecuteValidator } from '../interface';
import rules from '../rule';
import { isEmptyValue } from '../util';

/**
* Validates a regular expression pattern.
*
* Performs validation when a rule only contains
* a pattern property but is not declared as a string type.
*
* @param rule The validation rule.
* @param value The value of the field on the source object.
* @param callback The callback function.
* @param source The source object being validated.
* @param options The validation options.
* @param options.messages The validation messages.
*/
function pattern(rule, value, callback, source, options) {
const errors = [];
const pattern: ExecuteValidator = (rule, value, callback, source, options) => {
const errors: string[] = [];
const validate =
rule.required || (!rule.required && source.hasOwnProperty(rule.field));
if (validate) {
Expand All @@ -28,6 +16,6 @@ function pattern(rule, value, callback, source, options) {
}
}
callback(errors);
}
};

export default pattern;
17 changes: 4 additions & 13 deletions src/validator/regexp.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,9 @@
import { ExecuteValidator } from '../interface';
import rules from '../rule';
import { isEmptyValue } from '../util';

/**
* Validates the regular expression type.
*
* @param rule The validation rule.
* @param value The value of the field on the source object.
* @param callback The callback function.
* @param source The source object being validated.
* @param options The validation options.
* @param options.messages The validation messages.
*/
function regexp(rule, value, callback, source, options) {
const errors = [];
const regexp: ExecuteValidator = (rule, value, callback, source, options) => {
const errors: string[] = [];
const validate =
rule.required || (!rule.required && source.hasOwnProperty(rule.field));
if (validate) {
Expand All @@ -25,6 +16,6 @@ function regexp(rule, value, callback, source, options) {
}
}
callback(errors);
}
};

export default regexp;
Loading

0 comments on commit 8c1c08e

Please sign in to comment.