chushihua
This commit is contained in:
+29
@@ -0,0 +1,29 @@
|
||||
import rules from '../rule/';
|
||||
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) {
|
||||
var errors = [];
|
||||
var validate = rule.required || !rule.required && source.hasOwnProperty(rule.field);
|
||||
if (validate) {
|
||||
if (isEmptyValue(value, 'array') && !rule.required) {
|
||||
return callback();
|
||||
}
|
||||
rules.required(rule, value, source, errors, options, 'array');
|
||||
if (!isEmptyValue(value, 'array')) {
|
||||
rules.type(rule, value, source, errors, options);
|
||||
rules.range(rule, value, source, errors, options);
|
||||
}
|
||||
}
|
||||
callback(errors);
|
||||
}
|
||||
|
||||
export default array;
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
import { isEmptyValue } from '../util';
|
||||
import rules from '../rule/';
|
||||
|
||||
/**
|
||||
* 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) {
|
||||
var errors = [];
|
||||
var validate = rule.required || !rule.required && source.hasOwnProperty(rule.field);
|
||||
if (validate) {
|
||||
if (isEmptyValue(value) && !rule.required) {
|
||||
return callback();
|
||||
}
|
||||
rules.required(rule, value, source, errors, options);
|
||||
if (value !== undefined) {
|
||||
rules.type(rule, value, source, errors, options);
|
||||
}
|
||||
}
|
||||
callback(errors);
|
||||
}
|
||||
|
||||
export default boolean;
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
import rules from '../rule/';
|
||||
import { isEmptyValue } from '../util';
|
||||
|
||||
function date(rule, value, callback, source, options) {
|
||||
// console.log('integer rule called %j', rule);
|
||||
var errors = [];
|
||||
var validate = rule.required || !rule.required && source.hasOwnProperty(rule.field);
|
||||
// console.log('validate on %s value', value);
|
||||
if (validate) {
|
||||
if (isEmptyValue(value) && !rule.required) {
|
||||
return callback();
|
||||
}
|
||||
rules.required(rule, value, source, errors, options);
|
||||
if (!isEmptyValue(value)) {
|
||||
var dateObject = void 0;
|
||||
|
||||
if (typeof value === 'number') {
|
||||
dateObject = new Date(value);
|
||||
} else {
|
||||
dateObject = value;
|
||||
}
|
||||
|
||||
rules.type(rule, dateObject, source, errors, options);
|
||||
if (dateObject) {
|
||||
rules.range(rule, dateObject.getTime(), source, errors, options);
|
||||
}
|
||||
}
|
||||
}
|
||||
callback(errors);
|
||||
}
|
||||
|
||||
export default date;
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
import rules from '../rule/';
|
||||
import { isEmptyValue } from '../util';
|
||||
var ENUM = 'enum';
|
||||
|
||||
/**
|
||||
* 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) {
|
||||
var errors = [];
|
||||
var validate = rule.required || !rule.required && source.hasOwnProperty(rule.field);
|
||||
if (validate) {
|
||||
if (isEmptyValue(value) && !rule.required) {
|
||||
return callback();
|
||||
}
|
||||
rules.required(rule, value, source, errors, options);
|
||||
if (value) {
|
||||
rules[ENUM](rule, value, source, errors, options);
|
||||
}
|
||||
}
|
||||
callback(errors);
|
||||
}
|
||||
|
||||
export default enumerable;
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
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) {
|
||||
var errors = [];
|
||||
var validate = rule.required || !rule.required && source.hasOwnProperty(rule.field);
|
||||
if (validate) {
|
||||
if (isEmptyValue(value) && !rule.required) {
|
||||
return callback();
|
||||
}
|
||||
rules.required(rule, value, source, errors, options);
|
||||
if (value !== undefined) {
|
||||
rules.type(rule, value, source, errors, options);
|
||||
rules.range(rule, value, source, errors, options);
|
||||
}
|
||||
}
|
||||
callback(errors);
|
||||
}
|
||||
|
||||
export default floatFn;
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
import string from './string';
|
||||
import method from './method';
|
||||
import number from './number';
|
||||
import boolean from './boolean';
|
||||
import regexp from './regexp';
|
||||
import integer from './integer';
|
||||
import float from './float';
|
||||
import array from './array';
|
||||
import object from './object';
|
||||
import enumValidator from './enum';
|
||||
import pattern from './pattern';
|
||||
import date from './date';
|
||||
import required from './required';
|
||||
import type from './type';
|
||||
|
||||
export default {
|
||||
string: string,
|
||||
method: method,
|
||||
number: number,
|
||||
boolean: boolean,
|
||||
regexp: regexp,
|
||||
integer: integer,
|
||||
float: float,
|
||||
array: array,
|
||||
object: object,
|
||||
'enum': enumValidator,
|
||||
pattern: pattern,
|
||||
date: date,
|
||||
url: type,
|
||||
hex: type,
|
||||
email: type,
|
||||
required: required
|
||||
};
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
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) {
|
||||
var errors = [];
|
||||
var validate = rule.required || !rule.required && source.hasOwnProperty(rule.field);
|
||||
if (validate) {
|
||||
if (isEmptyValue(value) && !rule.required) {
|
||||
return callback();
|
||||
}
|
||||
rules.required(rule, value, source, errors, options);
|
||||
if (value !== undefined) {
|
||||
rules.type(rule, value, source, errors, options);
|
||||
rules.range(rule, value, source, errors, options);
|
||||
}
|
||||
}
|
||||
callback(errors);
|
||||
}
|
||||
|
||||
export default integer;
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
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) {
|
||||
var errors = [];
|
||||
var validate = rule.required || !rule.required && source.hasOwnProperty(rule.field);
|
||||
if (validate) {
|
||||
if (isEmptyValue(value) && !rule.required) {
|
||||
return callback();
|
||||
}
|
||||
rules.required(rule, value, source, errors, options);
|
||||
if (value !== undefined) {
|
||||
rules.type(rule, value, source, errors, options);
|
||||
}
|
||||
}
|
||||
callback(errors);
|
||||
}
|
||||
|
||||
export default method;
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
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) {
|
||||
var errors = [];
|
||||
var validate = rule.required || !rule.required && source.hasOwnProperty(rule.field);
|
||||
if (validate) {
|
||||
if (isEmptyValue(value) && !rule.required) {
|
||||
return callback();
|
||||
}
|
||||
rules.required(rule, value, source, errors, options);
|
||||
if (value !== undefined) {
|
||||
rules.type(rule, value, source, errors, options);
|
||||
rules.range(rule, value, source, errors, options);
|
||||
}
|
||||
}
|
||||
callback(errors);
|
||||
}
|
||||
|
||||
export default number;
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
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) {
|
||||
var errors = [];
|
||||
var validate = rule.required || !rule.required && source.hasOwnProperty(rule.field);
|
||||
if (validate) {
|
||||
if (isEmptyValue(value) && !rule.required) {
|
||||
return callback();
|
||||
}
|
||||
rules.required(rule, value, source, errors, options);
|
||||
if (value !== undefined) {
|
||||
rules.type(rule, value, source, errors, options);
|
||||
}
|
||||
}
|
||||
callback(errors);
|
||||
}
|
||||
|
||||
export default object;
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
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) {
|
||||
var errors = [];
|
||||
var validate = rule.required || !rule.required && source.hasOwnProperty(rule.field);
|
||||
if (validate) {
|
||||
if (isEmptyValue(value, 'string') && !rule.required) {
|
||||
return callback();
|
||||
}
|
||||
rules.required(rule, value, source, errors, options);
|
||||
if (!isEmptyValue(value, 'string')) {
|
||||
rules.pattern(rule, value, source, errors, options);
|
||||
}
|
||||
}
|
||||
callback(errors);
|
||||
}
|
||||
|
||||
export default pattern;
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
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) {
|
||||
var errors = [];
|
||||
var validate = rule.required || !rule.required && source.hasOwnProperty(rule.field);
|
||||
if (validate) {
|
||||
if (isEmptyValue(value) && !rule.required) {
|
||||
return callback();
|
||||
}
|
||||
rules.required(rule, value, source, errors, options);
|
||||
if (!isEmptyValue(value)) {
|
||||
rules.type(rule, value, source, errors, options);
|
||||
}
|
||||
}
|
||||
callback(errors);
|
||||
}
|
||||
|
||||
export default regexp;
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
import _typeof from 'babel-runtime/helpers/typeof';
|
||||
import rules from '../rule/';
|
||||
|
||||
function required(rule, value, callback, source, options) {
|
||||
var errors = [];
|
||||
var type = Array.isArray(value) ? 'array' : typeof value === 'undefined' ? 'undefined' : _typeof(value);
|
||||
rules.required(rule, value, source, errors, options, type);
|
||||
callback(errors);
|
||||
}
|
||||
|
||||
export default required;
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
import rules from '../rule/';
|
||||
import { isEmptyValue } from '../util';
|
||||
|
||||
/**
|
||||
* Performs validation for string types.
|
||||
*
|
||||
* @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 string(rule, value, callback, source, options) {
|
||||
var errors = [];
|
||||
var validate = rule.required || !rule.required && source.hasOwnProperty(rule.field);
|
||||
if (validate) {
|
||||
if (isEmptyValue(value, 'string') && !rule.required) {
|
||||
return callback();
|
||||
}
|
||||
rules.required(rule, value, source, errors, options, 'string');
|
||||
if (!isEmptyValue(value, 'string')) {
|
||||
rules.type(rule, value, source, errors, options);
|
||||
rules.range(rule, value, source, errors, options);
|
||||
rules.pattern(rule, value, source, errors, options);
|
||||
if (rule.whitespace === true) {
|
||||
rules.whitespace(rule, value, source, errors, options);
|
||||
}
|
||||
}
|
||||
}
|
||||
callback(errors);
|
||||
}
|
||||
|
||||
export default string;
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
import rules from '../rule/';
|
||||
import { isEmptyValue } from '../util';
|
||||
|
||||
function type(rule, value, callback, source, options) {
|
||||
var ruleType = rule.type;
|
||||
var errors = [];
|
||||
var validate = rule.required || !rule.required && source.hasOwnProperty(rule.field);
|
||||
if (validate) {
|
||||
if (isEmptyValue(value, ruleType) && !rule.required) {
|
||||
return callback();
|
||||
}
|
||||
rules.required(rule, value, source, errors, options, ruleType);
|
||||
if (!isEmptyValue(value, ruleType)) {
|
||||
rules.type(rule, value, source, errors, options);
|
||||
}
|
||||
}
|
||||
callback(errors);
|
||||
}
|
||||
|
||||
export default type;
|
||||
Reference in New Issue
Block a user