/
index.js
232 lines (203 loc) · 4.52 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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
const EMAIL_REGEXP = /^(?=.{1,254}$)(?=.{1,64}@)[-!#$%&'*+/0-9=?A-Z^_`a-z{|}~]+(\.[-!#$%&'*+/0-9=?A-Z^_`a-z{|}~]+)*@[A-Za-z0-9]([A-Za-z0-9-]{0,61}[A-Za-z0-9])?(\.[A-Za-z0-9]([A-Za-z0-9-]{0,61}[A-Za-z0-9])?)*$/;
export class Model {
error = {};
value = '';
/**
* [constructor description]
* @param {Functions[]} validators [description]
*/
constructor(validators){
this.validators = validators;
this.setValue('');
}
/**
* [setValue description]
* @param {Any} val [description]
*/
setValue (val){
this.val = val;
this.error = {};
for (var i = this.validators.length - 1; i >= 0; i--) {
var error = this.validators[i](this.value);
if(error){
Object.assign(this.error, error);
}
}
for (var i in this.error) {
this.invalid = !!this.error[i];
}
return this;
}
get valid(){
return !this.invalid;
}
/**
* [hasError description]
* @param {String} name [description]
* @return {Boolean} [description]
*/
hasError(name){
return !!this.error[name];
}
}
/**
*
* const myValidate = (val) => {
* return { isFail : 'ForMe' }
* };
*
* var models = {
* name : [ Validator.require(true), Validator.minLength(2), Validator.maxLength(5) ],
* email : [ Validator.require(false), Validator.email() ],
* years : [ Validator.min(18), Validator.min(100) ],
* url : [ Validator.patern('[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)') ]
* coment: [ Validator.free() ]
* };
*
* var form = new Validator(models);
* form.is('email', 'thisIsNotAEmail');
* form.invalid // TRUE
* form.valid // FALSE
* form.email.valid // FALSE
* form.email.hasError('email');
* form.email.validators.push(Validator.require(true));
*
*/
export default class Validator {
/**
* [require description]
* @param {Boolean} is [description]
* @return {Any}
*/
static require (is=true){
return (val) => is && ( val == null || ( val + '' ).length === 0 ) ?
{ require : true } : false;
}
/**
* [free description]
* @param {Boolean} is [description]
* @return {Any}
*/
static free (is=false){
return Validator.require(is);
}
/**
* [email description]
* @return {Any}
*/
static email (){
return (val) => EMAIL_REGEXP.test( val + '' ) ?
false : { email : true };
}
/**
* [minLength description]
* @param {number} num
* @return {Any}
*/
static minLength (num){
return (val) => ( val + '' ).length < num ?
{ minLength : true } : false;
}
/**
* [maxLength description]
* @param {number} num
* @return {Any}
*/
static maxLength (num){
return (val) => ( val + '' ).length > num ?
{ maxLength : true } : false;
}
/**
* [patern description]
* @param {String|RegExp} part
* @return {Any}
*/
static patern (part){
let pats = typeof part.test == 'function' ? part : new RegExp(part);
return (val) => pats.test(val) ?
false : { 'pattern': pats.toString() };
}
/**
* [max description]
* @param {number} num
* @return {Any}
*/
static max (num){
return (val) => {
let vax = parseFloat(val);
return vax != NaN && vax > num ? { 'max': num } : false;
};
}
/**
* [min description]
* @param {number} num [description]
* @return {Any}
*/
static min (num){
return (val) => {
let vax = parseFloat(val);
return vax != NaN && vax < num ?
{ 'max' : num } : false;
};
}
/**
* [select description]
* @param {Object} obj
* @return {Any}
*/
static select (obj){
return (val) => ( val == null || ( val + '' ).length !== 0 ) && obj[val] != null ?
false : { 'select': true };
}
model = {};
values = {};
valid = true;
/**
* [constructor description]
* @param {Object} models [description]
*/
constructor (models){
for (let i in models) {
this.model[i] = new Model(models[i]);
}
}
/**
* [is description]
* @param {String} name Name Model
* @param {Any} val Value
* @return {Validator}
*/
is (name, val){
this.model[name].setValue(val);
this.values = {};
this.valid = true;
for (let i in this) {
if(this.model[i] instanceof Model ){
this.values[i] = this.model[i].value;
this.valid = this.model[i].valid && this.valid;
}
}
return this;
}
/**
* [hasError description]
* @param {String} model Model Name
* @param {String} name Error name
* @return {Any}
*/
hasError (model, error){
return this.model[model].error[error];
}
get names (){
let name = [];
for (let i in this) {
if(this.model[i] instanceof Model ){
name.push(i);
}
}
return name;
}
get invalid(){
return !this.valid;
}
}