-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvalidator.js
executable file
·186 lines (138 loc) · 3.91 KB
/
validator.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
/**
* @name
* @author Eduardo Ottaviani
*/
(function (root, factory) {
if(typeof exports === 'object' && exports) {
// CommonJS
module.exports = factory( require('./src/validator.rules'), require('./src/validator.messages'), require('./src/validator.plugins') );
}else{
if(typeof define === 'function' && define.amd) {
define('validator', ['validator.rules', 'validator.messages', 'validator.plugins'], factory);
}else{
root.Validator = factory(root.Rules, root.Messages, root.Plugins); // <script>
delete root.Rules;
delete root.Messages;
delete root.Plugins;
}
}
}(this, function( Rules, Messages, Plugins ){
var V = {
_class :function(o){
var
holder,
hash = {}, _self = this;
holder = (o && o.holder && o.holder.length)? o.holder :$('<div />');
holder.on('validator.instance', get);
holder.on('validator:instance', get);
this.test = function(el){
var options = { rules :{} };
el = el.get? el :$(el);
$.each(hash, function(query, value){
if( el.is(query) || el.hasClass(query) ){
$.extend( options.rules, value.rules );
}
});
return validate( el, { rules :options.rules }, { list: [], map :{} } );
};
this.get = function(key){
return hash[key];
};
this.on = function(name, method){
holder.on( name, bind( method ) );
};
this.add = function( query, rules ){
hash[ query ] = rules;
Plugins.initialize( holder, rules, query );
holder.trigger('validator.add', holder, rules, query);
holder.trigger('validator:add', holder, rules, query);
return this;
};
this.add_all = function(rules){
hash = rules;
Plugins.initialize( holder, rules );
holder.trigger('validator.add', holder, rules);
holder.trigger('validator:add', holder, rules);
return this;
};
this.remove = function( name ){
delete hash[ name ];
holder.trigger('validator.remove', name);
holder.trigger('validator:remove', name);
};
this.validate = function(){
var errors = check( hash, holder );
if( errors.list.length ){
holder.trigger('validator.error', errors);
holder.trigger('validator:error', errors);
}else{
holder.trigger('validator.success');
holder.trigger('validator:success');
}
return !(!!errors.list.length);
};
this.is_valid = function(){
return !(!!check( hash, holder ).list.length);
};
function get(e, callback){
callback? callback( _self ) :null;
}
},
add_messages :function(o){
Messages.add_all(o);
},
add_rule :function(name, method){
Rules.add(name, method);
},
internal :function(o){
return({ Rule :Rules, Message :Messages, Plugin :Plugins })[o];
},
create :function(o){ return new this._class(o); }
};
function create(el, options){
return{
element :el.get? el.get(0) :el,
rules :$.extend({}, options.rules),
invalid_rules:{},
messages:$.extend({ map:{}, list :[] }, { map: options.messages }),
is_valid:true
};
}
function validate( element, options, errors, scope ){
var el = create( element, options );
Rules.validate( el, options, add_error( errors ) );
if( errors.list.length ){
Messages.write_all( el, options );
}
Plugins.each_elements( el, options );
return el;
}
function check( hash, scope ){
var errors = { list: [], map :{} };
$.each( hash, each_hash(scope, errors) );
return errors;
}
function bind(method){
return function(e, error){
method( error );
};
}
function each_hash(scope, errors){
return function(selector, options){
$(selector, scope).each( each_elements(options, errors, scope) );
};
}
function each_elements(options, errors, scope){
return function(i, element){
validate( element, options, errors, scope );
};
}
function add_error(errors){
return function(el, name){
errors.list.push(el);
errors.map[name] = errors.map[name] || [];
errors.map[name].push(el);
};
}
return V;
}));