-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
63 lines (57 loc) · 1.94 KB
/
index.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
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.FIELDS = undefined;
exports.validator = validator;
var _rules = require('./rules');
var _rules2 = _interopRequireDefault(_rules);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var FIELDS = exports.FIELDS = {
DOMAIN: 'DOMAIN',
EMAIL: 'EMAIL',
PHONENUMBER: 'PHONENUMBER',
TEXT: 'TEXT',
NUMBER: 'NUMBER'
};
function validate(errors, field, check) {
var key = field.key;var value = field.value;var isValid = true;
if (value === null || !value) {
errors[key] = key + ' ' + check.onEmpty;isValid = false;
} else if (isValid && check.regex && !new RegExp(check.regex).test(value)) {
errors[key] = key + ' ' + check.notValid;isValid = false;
} else {
errors[key] = '';isValid = true;
}
return { errors: errors, isValid: isValid };
}
function validator(fieldValues) {
var errors={};
var isValid = true;
var response = { errors: '', isValid: true };
fieldValues.map(function (field) {
switch (field.type) {
case FIELDS.DOMAIN:
response = validate(errors, field, _rules2.default.Domain);
break;
case FIELDS.EMAIL:
response = validate(errors, field, _rules2.default.Email);
break;
case FIELDS.TEXT:
response = validate(errors, field, _rules2.default.Text);
break;
case FIELDS.PHONENUMBER:
response = validate(errors, field, _rules2.default.Phonenumber);
break;
case FIELDS.NUMBER:
response = validate(errors, field, _rules2.default.Number);
break;
default:
break;
}
if (!response.isValid) {
isValid = false;
}
});
return { errors: response.errors, isValid: isValid };
}