This repository has been archived by the owner on Aug 27, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
validate.js
160 lines (148 loc) · 5.85 KB
/
validate.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import isNull from 'lodash/isNull';
import isUndefined from 'lodash/isUndefined';
import isNaN from 'lodash/isNaN';
import isNumber from 'lodash/isNumber';
import isString from 'lodash/isString';
import i18n from 'i18next';
//Dependency
const translate = (str, params) => `${i18n.t(str)}`;
const EMAIL_REGEX = /^(([^<>()[\]\\.,;:\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,}))$/;
/* Function to validate that an input is a number.
* @param {string || number} numberToValidate - Number to validate with the function.
* @param {object} options = {}, Allow the caller to specify min and max values.
* @return {boolean} True if the validator works.
*/
function numberValidation(numberToValidate, options = {}) {
if (isUndefined(numberToValidate) || isNull(numberToValidate)) {
return true;
}
let castNumberToValidate = +numberToValidate; //Cast it into a number.
if (isNaN(castNumberToValidate)) {
return false;
}
if(!isNumber(castNumberToValidate)){
return false;
}
let isMin = options.min !== undefined ? castNumberToValidate >= options.min : true;
let isMax = options.max !== undefined ? castNumberToValidate <= options.max : true;
return isMin && isMax;
};
function stringLength(stringToTest, options = {}) {
if (!isString(stringToTest)) {
return false;
}
options.minLength = options.minLength || 0;
const isMinLength = options.minLength !== undefined ? stringToTest.length >= options.minLength : true;
const isMaxLength = options.maxLength !== undefined ? stringToTest.length <= options.maxLength : true;
return isMinLength && isMaxLength;
};
/**
* Email validator using a Regex.
* @param {string} emailToValidate - The email to validate.
* @return {boolean} - True if the email is valide , false otherwise.
*/
function emailValidation(emailToValidate) {
return EMAIL_REGEX.test(emailToValidate);
};
function dateValidation(dateToValidate, options) {
const moment = require('moment');
if(!moment){
console.warn('Moment library is not a part of your project, please download it : http://momentjs.com/');
}
return moment(dateToValidate, options).isValid();
};
/**
* Validae a property given validators.
* @param {object} property - Property to validate which should be as follows: `{name: "field_name",value: "field_value", validators: [{...}] }`.
* @param {array} validators - The validators to apply on the property.
* @return {object} - The validation status.
*/
function validate(property, validators) {
//console.log("validate", property, validators);
let errors = [], res, validator;
if (validators) {
for (let i = 0, _len = validators.length; i < _len; i++) {
validator = validators[i];
res = validateProperty(property, validator);
if (!isNull(res) && !isUndefined(res)) {
errors.push(res);
}
}
}
//Check what's the good type to return.
return {
name: property.name,
value: property.value,
isValid: 0 === errors.length,
errors: errors
};
}
/**
* Validate a property.
* @param {object} property - The property to validate.
* @param {function} validator - The validator to apply.
* @return {object} - The property validation status.
*/
function validateProperty(property, validator) {
let isValid;
if (!validator) {
return void 0;
}
if (!property) {
return void 0;
}
const {value} = property;
const {options} = validator;
const isValueNullOrUndefined = isNull(value) || isUndefined(value );
isValid = (() => {
switch (validator.type) {
case 'required':
const prevalidString = '' === property.value ? false : true;
const prevalidDate = true;
return true === validator.value ? (!isNull(value) && !isUndefined(value) && prevalidString && prevalidDate) : true;
case 'regex':
if (isValueNullOrUndefined) {
return true;
}
return validator.value.test(value);
case 'email':
if (isValueNullOrUndefined) {
return true;
}
return emailValidation(value, options);
case 'number':
return numberValidation(value, options);
case 'string':
const stringToValidate = value || '';
return stringLength(stringToValidate, options);
case 'date':
return dateValidation(value, options);
case 'function':
return validator.value(value, options);
case 'checkbox':
return (isUndefined(value) || isNull(value)) ? false : true;
default:
return void 0;
}
})();
if (isUndefined(isValid) || isNull(isValid)) {
console.warn(`The validator of type: ${validator.type} is not defined`);
} else if (false === isValid) {
//Add the name of the property.
return getErrorLabel(validator.type, property.modelName + '.' + property.name, options); //"The property " + property.name + " is invalid.";
}
}
/**
* Get the error label from a type and a field name.
* @param {string} type - The type name.
* @param {string} fieldName - The field name.
* @param {object} options - The options to put such as the translationKey which could be defined in the domain.
* @return {string} The formatted error.
*/
function getErrorLabel(type, fieldName, options = {}) {
options = options || {};
const translationKey = options.translationKey ? options.translationKey : `domain.validation.${type}`;
const opts = {fieldName: translate(fieldName), ...options};
return translate(translationKey, opts);
}
export default validate;