0544e2f68005833d2ad0b31fabe961c535e1855f
3 var Validator
= L
.Class
.extend({
4 __name__
: 'Validation',
6 __init__: function(field
, type
, optional
, vfunc
, validatorFactory
) {
8 this.optional
= optional
;
10 this.vstack
= validatorFactory
.compile(type
);
11 this.factory
= validatorFactory
;
14 assert: function(condition
, message
) {
16 this.field
.classList
.add('cbi-input-invalid');
21 this.field
.classList
.remove('cbi-input-invalid');
26 apply: function(name
, value
, args
) {
29 if (typeof(name
) === 'function')
31 else if (typeof(this.factory
.types
[name
]) === 'function')
32 func
= this.factory
.types
[name
];
39 return func
.apply(this, args
);
42 validate: function() {
43 /* element is detached */
44 if (!findParent(this.field
, 'body') && !findParent(this.field
, '[data-field]'))
47 this.field
.classList
.remove('cbi-input-invalid');
48 this.value
= (this.field
.value
!= null) ? this.field
.value
: '';
53 if (this.value
.length
=== 0)
54 valid
= this.assert(this.optional
, _('non-empty value'));
56 valid
= this.vstack
[0].apply(this, this.vstack
[1]);
59 this.field
.setAttribute('data-tooltip', _('Expecting: %s').format(this.error
));
60 this.field
.setAttribute('data-tooltip-style', 'error');
61 this.field
.dispatchEvent(new CustomEvent('validation-failure', { bubbles
: true }));
65 if (typeof(this.vfunc
) == 'function')
66 valid
= this.vfunc(this.value
);
69 this.assert(false, valid
);
70 this.field
.setAttribute('data-tooltip', valid
);
71 this.field
.setAttribute('data-tooltip-style', 'error');
72 this.field
.dispatchEvent(new CustomEvent('validation-failure', { bubbles
: true }));
76 this.field
.removeAttribute('data-tooltip');
77 this.field
.removeAttribute('data-tooltip-style');
78 this.field
.dispatchEvent(new CustomEvent('validation-success', { bubbles
: true }));
84 var ValidatorFactory
= L
.Class
.extend({
85 __name__
: 'ValidatorFactory',
87 create: function(field
, type
, optional
, vfunc
) {
88 return new Validator(field
, type
, optional
, vfunc
, this);
91 compile: function(code
) {
99 for (var i
= 0; i
< code
.length
; i
++) {
105 switch (code
.charCodeAt(i
))
115 var label
= code
.substring(pos
, i
);
116 label
= label
.replace(/\\(.)/g, '$1');
117 label
= label
.replace(/^[ \t]+/g, '');
118 label
= label
.replace(/[ \t]+$/g, '');
120 if (label
&& !isNaN(label
)) {
121 stack
.push(parseFloat(label
));
123 else if (label
.match(/^(['"]).*\1$/)) {
124 stack
.push(label
.replace(/^(['"])(.*)\1$/, '$2'));
126 else if (typeof this.types
[label
] == 'function') {
127 stack
.push(this.types
[label
]);
131 L
.raise('SyntaxError', 'Unhandled token "%s"', label
);
138 depth
+= (code
.charCodeAt(i
) == 40);
143 if (typeof stack
[stack
.length
-2] != 'function')
144 L
.raise('SyntaxError', 'Argument list follows non-function');
146 stack
[stack
.length
-1] = this.compile(code
.substring(pos
, i
));
157 parseInteger: function(x
) {
158 return (/^-?\d+$/.test(x
) ? +x
: NaN
);
161 parseDecimal: function(x
) {
162 return (/^-?\d+(?:\.\d+)?$/.test(x
) ? +x
: NaN
);
165 parseIPv4: function(x
) {
166 if (!x
.match(/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/))
169 if (RegExp
.$1 > 255 || RegExp
.$2 > 255 || RegExp
.$3 > 255 || RegExp
.$4 > 255)
172 return [ +RegExp
.$1, +RegExp
.$2, +RegExp
.$3, +RegExp
.$4 ];
175 parseIPv6: function(x
) {
176 if (x
.match(/^([a-fA-F0-9:]+):(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})$/)) {
177 var v6
= RegExp
.$1, v4
= this.parseIPv4(RegExp
.$2);
182 x
= v6
+ ':' + (v4
[0] * 256 + v4
[1]).toString(16)
183 + ':' + (v4
[2] * 256 + v4
[3]).toString(16);
186 if (!x
.match(/^[a-fA-F0-9:]+$/))
189 var prefix_suffix
= x
.split(/::/);
191 if (prefix_suffix
.length
> 2)
194 var prefix
= (prefix_suffix
[0] || '0').split(/:/);
195 var suffix
= prefix_suffix
.length
> 1 ? (prefix_suffix
[1] || '0').split(/:/) : [];
197 if (suffix
.length
? (prefix
.length
+ suffix
.length
> 7)
198 : ((prefix_suffix
.length
< 2 && prefix
.length
< 8) || prefix
.length
> 8))
204 for (i
= 0, word
= parseInt(prefix
[0], 16); i
< prefix
.length
; word
= parseInt(prefix
[++i
], 16))
205 if (prefix
[i
].length
<= 4 && !isNaN(word
) && word
<= 0xFFFF)
210 for (i
= 0; i
< (8 - prefix
.length
- suffix
.length
); i
++)
213 for (i
= 0, word
= parseInt(suffix
[0], 16); i
< suffix
.length
; word
= parseInt(suffix
[++i
], 16))
214 if (suffix
[i
].length
<= 4 && !isNaN(word
) && word
<= 0xFFFF)
223 integer: function() {
224 return this.assert(!isNaN(this.factory
.parseInteger(this.value
)), _('valid integer value'));
227 uinteger: function() {
228 return this.assert(this.factory
.parseInteger(this.value
) >= 0, _('positive integer value'));
232 return this.assert(!isNaN(this.factory
.parseDecimal(this.value
)), _('valid decimal value'));
236 return this.assert(this.factory
.parseDecimal(this.value
) >= 0, _('positive decimal value'));
239 ipaddr: function(nomask
) {
240 return this.assert(this.apply('ip4addr', null, [nomask
]) || this.apply('ip6addr', null, [nomask
]),
241 nomask
? _('valid IP address') : _('valid IP address or prefix'));
244 ip4addr: function(nomask
) {
245 var re
= nomask
? /^(\d+\.\d+\.\d+\.\d+)$/ : /^(\d+\.\d+\.\d+\.\d+)(?:\/(\d+\.\d+\.\d+\.\d+)|\/(\d{1,2}))?$/,
246 m
= this.value
.match(re
);
248 return this.assert(m
&& this.factory
.parseIPv4(m
[1]) && (m
[2] ? this.factory
.parseIPv4(m
[2]) : (m
[3] ? this.apply('ip4prefix', m
[3]) : true)),
249 nomask
? _('valid IPv4 address') : _('valid IPv4 address or network'));
252 ip6addr: function(nomask
) {
253 var re
= nomask
? /^([0-9a-fA-F:.]+)$/ : /^([0-9a-fA-F:.]+)(?:\/(\d{1,3}))?$/,
254 m
= this.value
.match(re
);
256 return this.assert(m
&& this.factory
.parseIPv6(m
[1]) && (m
[2] ? this.apply('ip6prefix', m
[2]) : true),
257 nomask
? _('valid IPv6 address') : _('valid IPv6 address or prefix'));
260 ip4prefix: function() {
261 return this.assert(!isNaN(this.value
) && this.value
>= 0 && this.value
<= 32,
262 _('valid IPv4 prefix value (0-32)'));
265 ip6prefix: function() {
266 return this.assert(!isNaN(this.value
) && this.value
>= 0 && this.value
<= 128,
267 _('valid IPv6 prefix value (0-128)'));
271 return this.assert(this.apply('cidr4') || this.apply('cidr6'), _('valid IPv4 or IPv6 CIDR'));
275 var m
= this.value
.match(/^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\/(\d{1,2})$/);
276 return this.assert(m
&& this.factory
.parseIPv4(m
[1]) && this.apply('ip4prefix', m
[2]), _('valid IPv4 CIDR'));
280 var m
= this.value
.match(/^([0-9a-fA-F:.]+)\/(\d{1,3})$/);
281 return this.assert(m
&& this.factory
.parseIPv6(m
[1]) && this.apply('ip6prefix', m
[2]), _('valid IPv6 CIDR'));
285 var m
= this.value
.match(/^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\/(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})$/);
286 return this.assert(m
&& this.factory
.parseIPv4(m
[1]) && this.factory
.parseIPv4(m
[2]), _('IPv4 network in address/netmask notation'));
290 var m
= this.value
.match(/^([0-9a-fA-F:.]+)\/([0-9a-fA-F:.]+)$/);
291 return this.assert(m
&& this.factory
.parseIPv6(m
[1]) && this.factory
.parseIPv6(m
[2]), _('IPv6 network in address/netmask notation'));
294 ip6hostid: function() {
295 if (this.value
== "eui64" || this.value
== "random")
298 var v6
= this.factory
.parseIPv6(this.value
);
299 return this.assert(!(!v6
|| v6
[0] || v6
[1] || v6
[2] || v6
[3]), _('valid IPv6 host id'));
303 return this.assert(this.apply('ipmask4') || this.apply('ipmask6'),
304 _('valid network in address/netmask notation'));
307 ipmask4: function() {
308 return this.assert(this.apply('cidr4') || this.apply('ipnet4') || this.apply('ip4addr'),
309 _('valid IPv4 network'));
312 ipmask6: function() {
313 return this.assert(this.apply('cidr6') || this.apply('ipnet6') || this.apply('ip6addr'),
314 _('valid IPv6 network'));
318 var p
= this.factory
.parseInteger(this.value
);
319 return this.assert(p
>= 0 && p
<= 65535, _('valid port value'));
322 portrange: function() {
323 if (this.value
.match(/^(\d+)-(\d+)$/)) {
326 return this.assert(p1
<= p2
&& p2
<= 65535,
327 _('valid port or port range (port1-port2)'));
330 return this.assert(this.apply('port'), _('valid port or port range (port1-port2)'));
333 macaddr: function() {
334 return this.assert(this.value
.match(/^([a-fA-F0-9]{2}:){5}[a-fA-F0-9]{2}$/) != null,
335 _('valid MAC address'));
338 host: function(ipv4only
) {
339 return this.assert(this.apply('hostname') || this.apply(ipv4only
== 1 ? 'ip4addr' : 'ipaddr'),
340 _('valid hostname or IP address'));
343 hostname: function(strict
) {
344 if (this.value
.length
<= 253)
346 (this.value
.match(/^[a-zA-Z0-9_]+$/) != null ||
347 (this.value
.match(/^[a-zA-Z0-9_][a-zA-Z0-9_\-.]*[a-zA-Z0-9]$/) &&
348 this.value
.match(/[^0-9.]/))) &&
349 (!strict
|| !this.value
.match(/^_/)),
350 _('valid hostname'));
352 return this.assert(false, _('valid hostname'));
355 network: function() {
356 return this.assert(this.apply('uciname') || this.apply('host'),
357 _('valid UCI identifier, hostname or IP address'));
360 hostport: function(ipv4only
) {
361 var hp
= this.value
.split(/:/);
362 return this.assert(hp
.length
== 2 && this.apply('host', hp
[0], [ipv4only
]) && this.apply('port', hp
[1]),
363 _('valid host:port'));
366 ip4addrport: function() {
367 var hp
= this.value
.split(/:/);
368 return this.assert(hp
.length
== 2 && this.apply('ip4addr', hp
[0], [true]) && this.apply('port', hp
[1]),
369 _('valid IPv4 address:port'));
372 ipaddrport: function(bracket
) {
373 var m4
= this.value
.match(/^([^\[\]:]+):(\d+)$/),
374 m6
= this.value
.match((bracket
== 1) ? /^\[(.+)\]:(\d+)$/ : /^([^\[\]]+):(\d+)$/);
377 return this.assert(this.apply('ip4addr', m4
[1], [true]) && this.apply('port', m4
[2]),
378 _('valid address:port'));
380 return this.assert(m6
&& this.apply('ip6addr', m6
[1], [true]) && this.apply('port', m6
[2]),
381 _('valid address:port'));
388 return this.assert(v
.match(/^[a-fA-F0-9]{64}$/), _('valid hexadecimal WPA key'));
390 return this.assert((v
.length
>= 8) && (v
.length
<= 63), _('key between 8 and 63 characters'));
396 if (v
.substr(0, 2) === 's:')
399 if ((v
.length
== 10) || (v
.length
== 26))
400 return this.assert(v
.match(/^[a-fA-F0-9]{10,26}$/), _('valid hexadecimal WEP key'));
402 return this.assert((v
.length
=== 5) || (v
.length
=== 13), _('key with either 5 or 13 characters'));
405 uciname: function() {
406 return this.assert(this.value
.match(/^[a-zA-Z0-9_]+$/), _('valid UCI identifier'));
409 range: function(min
, max
) {
410 var val
= this.factory
.parseDecimal(this.value
);
411 return this.assert(val
>= +min
&& val
<= +max
, _('value between %f and %f').format(min
, max
));
415 return this.assert(this.factory
.parseDecimal(this.value
) >= +min
, _('value greater or equal to %f').format(min
));
419 return this.assert(this.factory
.parseDecimal(this.value
) <= +max
, _('value smaller or equal to %f').format(max
));
422 length: function(len
) {
423 var val
= '' + this.value
;
424 return this.assert(val
.length
== +len
,
425 _('value with %d characters').format(len
));
428 rangelength: function(min
, max
) {
429 var val
= '' + this.value
;
430 return this.assert((val
.length
>= +min
) && (val
.length
<= +max
),
431 _('value between %d and %d characters').format(min
, max
));
434 minlength: function(min
) {
435 return this.assert((''+this.value
).length
>= +min
,
436 _('value with at least %d characters').format(min
));
439 maxlength: function(max
) {
440 return this.assert((''+this.value
).length
<= +max
,
441 _('value with at most %d characters').format(max
));
447 for (var i
= 0; i
< arguments
.length
; i
+= 2) {
448 if (typeof arguments
[i
] != 'function') {
449 if (arguments
[i
] == this.value
)
450 return this.assert(true);
451 errors
.push('"%s"'.format(arguments
[i
]));
454 else if (arguments
[i
].apply(this, arguments
[i
+1])) {
455 return this.assert(true);
458 errors
.push(this.error
);
462 var t
= _('One of the following: %s');
464 return this.assert(false, t
.format('\n - ' + errors
.join('\n - ')));
468 for (var i
= 0; i
< arguments
.length
; i
+= 2) {
469 if (typeof arguments
[i
] != 'function') {
470 if (arguments
[i
] != this.value
)
471 return this.assert(false, '"%s"'.format(arguments
[i
]));
474 else if (!arguments
[i
].apply(this, arguments
[i
+1])) {
475 return this.assert(false, this.error
);
479 return this.assert(true);
483 this.value
= this.value
.replace(/^[ \t]*![ \t]*/, '');
485 if (arguments
[0].apply(this, arguments
[1]))
486 return this.assert(true);
488 return this.assert(false, _('Potential negation of: %s').format(this.error
));
491 list: function(subvalidator
, subargs
) {
492 this.field
.setAttribute('data-is-list', 'true');
494 var tokens
= this.value
.match(/[^ \t]+/g);
495 for (var i
= 0; i
< tokens
.length
; i
++)
496 if (!this.apply(subvalidator
, tokens
[i
], subargs
))
497 return this.assert(false, this.error
);
499 return this.assert(true);
502 phonedigit: function() {
503 return this.assert(this.value
.match(/^[0-9\*#!\.]+$/),
504 _('valid phone digit (0-9, "*", "#", "!" or ".")'));
507 timehhmmss: function() {
508 return this.assert(this.value
.match(/^[0-6][0-9]:[0-6][0-9]:[0-6][0-9]$/),
509 _('valid time (HH:MM:SS)'));
512 dateyyyymmdd: function() {
513 if (this.value
.match(/^(\d\d\d\d)-(\d\d)-(\d\d)/)) {
514 var year
= +RegExp
.$1,
517 days_in_month
= [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ];
519 var is_leap_year = function(year
) {
520 return ((!(year
% 4) && (year
% 100)) || !(year
% 400));
523 var get_days_in_month = function(month
, year
) {
524 return (month
=== 2 && is_leap_year(year
)) ? 29 : days_in_month
[month
- 1];
527 /* Firewall rules in the past don't make sense */
528 return this.assert(year
>= 2015 && month
&& month
<= 12 && day
&& day
<= get_days_in_month(month
, year
),
529 _('valid date (YYYY-MM-DD)'));
533 return this.assert(false, _('valid date (YYYY-MM-DD)'));
536 unique: function(subvalidator
, subargs
) {
538 option
= findParent(ctx
.field
, '[data-widget][data-name]'),
539 section
= findParent(option
, '.cbi-section'),
540 query
= '[data-widget="%s"][data-name="%s"]'.format(option
.getAttribute('data-widget'), option
.getAttribute('data-name')),
543 section
.querySelectorAll(query
).forEach(function(sibling
) {
544 if (sibling
=== option
)
547 var input
= sibling
.querySelector('[data-type]'),
548 values
= input
? (input
.getAttribute('data-is-list') ? input
.value
.match(/[^ \t]+/g) : [ input
.value
]) : null;
550 if (values
!== null && values
.indexOf(ctx
.value
) !== -1)
555 return this.assert(false, _('unique value'));
557 if (typeof(subvalidator
) === 'function')
558 return this.apply(subvalidator
, null, subargs
);
560 return this.assert(true);
563 hexstring: function() {
564 return this.assert(this.value
.match(/^([a-f0-9][a-f0-9]|[A-F0-9][A-F0-9])+$/),
565 _('hexadecimal encoded value'));
574 return ValidatorFactory
;