5 return new Blob([x
]).size
;
8 var Validator
= baseclass
.extend({
9 __name__
: 'Validation',
11 __init__: function(field
, type
, optional
, vfunc
, validatorFactory
) {
13 this.optional
= optional
;
15 this.vstack
= validatorFactory
.compile(type
);
16 this.factory
= validatorFactory
;
19 assert: function(condition
, message
) {
21 this.field
.classList
.add('cbi-input-invalid');
26 this.field
.classList
.remove('cbi-input-invalid');
31 apply: function(name
, value
, args
) {
34 if (typeof(name
) === 'function')
36 else if (typeof(this.factory
.types
[name
]) === 'function')
37 func
= this.factory
.types
[name
];
44 return func
.apply(this, args
);
47 validate: function() {
48 /* element is detached */
49 if (!findParent(this.field
, 'body') && !findParent(this.field
, '[data-field]'))
52 this.field
.classList
.remove('cbi-input-invalid');
53 this.value
= (this.field
.value
!= null) ? this.field
.value
: '';
58 if (this.value
.length
=== 0)
59 valid
= this.assert(this.optional
, _('non-empty value'));
61 valid
= this.vstack
[0].apply(this, this.vstack
[1]);
64 this.field
.setAttribute('data-tooltip', _('Expecting: %s').format(this.error
));
65 this.field
.setAttribute('data-tooltip-style', 'error');
66 this.field
.dispatchEvent(new CustomEvent('validation-failure', { bubbles
: true }));
70 if (typeof(this.vfunc
) == 'function')
71 valid
= this.vfunc(this.value
);
74 this.assert(false, valid
);
75 this.field
.setAttribute('data-tooltip', valid
);
76 this.field
.setAttribute('data-tooltip-style', 'error');
77 this.field
.dispatchEvent(new CustomEvent('validation-failure', { bubbles
: true }));
81 this.field
.removeAttribute('data-tooltip');
82 this.field
.removeAttribute('data-tooltip-style');
83 this.field
.dispatchEvent(new CustomEvent('validation-success', { bubbles
: true }));
89 var ValidatorFactory
= baseclass
.extend({
90 __name__
: 'ValidatorFactory',
92 create: function(field
, type
, optional
, vfunc
) {
93 return new Validator(field
, type
, optional
, vfunc
, this);
96 compile: function(code
) {
104 for (var i
= 0; i
< code
.length
; i
++) {
110 switch (code
.charCodeAt(i
))
120 var label
= code
.substring(pos
, i
);
121 label
= label
.replace(/\\(.)/g, '$1');
122 label
= label
.replace(/^[ \t]+/g, '');
123 label
= label
.replace(/[ \t]+$/g, '');
125 if (label
&& !isNaN(label
)) {
126 stack
.push(parseFloat(label
));
128 else if (label
.match(/^(['"]).*\1$/)) {
129 stack
.push(label
.replace(/^(['"])(.*)\1$/, '$2'));
131 else if (typeof this.types
[label
] == 'function') {
132 stack
.push(this.types
[label
]);
136 L
.raise('SyntaxError', 'Unhandled token "%s"', label
);
143 depth
+= (code
.charCodeAt(i
) == 40);
148 if (typeof stack
[stack
.length
-2] != 'function')
149 L
.raise('SyntaxError', 'Argument list follows non-function');
151 stack
[stack
.length
-1] = this.compile(code
.substring(pos
, i
));
162 parseInteger: function(x
) {
163 return (/^-?\d+$/.test(x
) ? +x
: NaN
);
166 parseDecimal: function(x
) {
167 return (/^-?\d+(?:\.\d+)?$/.test(x
) ? +x
: NaN
);
170 parseIPv4: function(x
) {
171 if (!x
.match(/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/))
174 if (RegExp
.$1 > 255 || RegExp
.$2 > 255 || RegExp
.$3 > 255 || RegExp
.$4 > 255)
177 return [ +RegExp
.$1, +RegExp
.$2, +RegExp
.$3, +RegExp
.$4 ];
180 parseIPv6: function(x
) {
181 if (x
.match(/^([a-fA-F0-9:]+):(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})$/)) {
182 var v6
= RegExp
.$1, v4
= this.parseIPv4(RegExp
.$2);
187 x
= v6
+ ':' + (v4
[0] * 256 + v4
[1]).toString(16)
188 + ':' + (v4
[2] * 256 + v4
[3]).toString(16);
191 if (!x
.match(/^[a-fA-F0-9:]+$/))
194 var prefix_suffix
= x
.split(/::/);
196 if (prefix_suffix
.length
> 2)
199 var prefix
= (prefix_suffix
[0] || '0').split(/:/);
200 var suffix
= prefix_suffix
.length
> 1 ? (prefix_suffix
[1] || '0').split(/:/) : [];
202 if (suffix
.length
? (prefix
.length
+ suffix
.length
> 7)
203 : ((prefix_suffix
.length
< 2 && prefix
.length
< 8) || prefix
.length
> 8))
209 for (i
= 0, word
= parseInt(prefix
[0], 16); i
< prefix
.length
; word
= parseInt(prefix
[++i
], 16))
210 if (prefix
[i
].length
<= 4 && !isNaN(word
) && word
<= 0xFFFF)
215 for (i
= 0; i
< (8 - prefix
.length
- suffix
.length
); i
++)
218 for (i
= 0, word
= parseInt(suffix
[0], 16); i
< suffix
.length
; word
= parseInt(suffix
[++i
], 16))
219 if (suffix
[i
].length
<= 4 && !isNaN(word
) && word
<= 0xFFFF)
228 integer: function() {
229 return this.assert(!isNaN(this.factory
.parseInteger(this.value
)), _('valid integer value'));
232 uinteger: function() {
233 return this.assert(this.factory
.parseInteger(this.value
) >= 0, _('positive integer value'));
237 return this.assert(!isNaN(this.factory
.parseDecimal(this.value
)), _('valid decimal value'));
241 return this.assert(this.factory
.parseDecimal(this.value
) >= 0, _('positive decimal value'));
244 ipaddr: function(nomask
) {
245 return this.assert(this.apply('ip4addr', null, [nomask
]) || this.apply('ip6addr', null, [nomask
]),
246 nomask
? _('valid IP address') : _('valid IP address or prefix'));
249 ip4addr: function(nomask
) {
250 var re
= nomask
? /^(\d+\.\d+\.\d+\.\d+)$/ : /^(\d+\.\d+\.\d+\.\d+)(?:\/(\d+\.\d+\.\d+\.\d+)|\/(\d{1,2}))?$/,
251 m
= this.value
.match(re
);
253 return this.assert(m
&& this.factory
.parseIPv4(m
[1]) && (m
[2] ? this.factory
.parseIPv4(m
[2]) : (m
[3] ? this.apply('ip4prefix', m
[3]) : true)),
254 nomask
? _('valid IPv4 address') : _('valid IPv4 address or network'));
257 ip6addr: function(nomask
) {
258 var re
= nomask
? /^([0-9a-fA-F:.]+)$/ : /^([0-9a-fA-F:.]+)(?:\/(\d{1,3}))?$/,
259 m
= this.value
.match(re
);
261 return this.assert(m
&& this.factory
.parseIPv6(m
[1]) && (m
[2] ? this.apply('ip6prefix', m
[2]) : true),
262 nomask
? _('valid IPv6 address') : _('valid IPv6 address or prefix'));
265 ip4prefix: function() {
266 return this.assert(!isNaN(this.value
) && this.value
>= 0 && this.value
<= 32,
267 _('valid IPv4 prefix value (0-32)'));
270 ip6prefix: function() {
271 return this.assert(!isNaN(this.value
) && this.value
>= 0 && this.value
<= 128,
272 _('valid IPv6 prefix value (0-128)'));
275 cidr: function(negative
) {
276 return this.assert(this.apply('cidr4', null, [negative
]) || this.apply('cidr6', null, [negative
]),
277 _('valid IPv4 or IPv6 CIDR'));
280 cidr4: function(negative
) {
281 var m
= this.value
.match(/^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\/(-)?(\d{1,2})$/);
282 return this.assert(m
&& this.factory
.parseIPv4(m
[1]) && (negative
|| !m
[2]) && this.apply('ip4prefix', m
[3]),
283 _('valid IPv4 CIDR'));
286 cidr6: function(negative
) {
287 var m
= this.value
.match(/^([0-9a-fA-F:.]+)\/(-)?(\d{1,3})$/);
288 return this.assert(m
&& this.factory
.parseIPv6(m
[1]) && (negative
|| !m
[2]) && this.apply('ip6prefix', m
[3]),
289 _('valid IPv6 CIDR'));
293 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})$/);
294 return this.assert(m
&& this.factory
.parseIPv4(m
[1]) && this.factory
.parseIPv4(m
[2]), _('IPv4 network in address/netmask notation'));
298 var m
= this.value
.match(/^([0-9a-fA-F:.]+)\/([0-9a-fA-F:.]+)$/);
299 return this.assert(m
&& this.factory
.parseIPv6(m
[1]) && this.factory
.parseIPv6(m
[2]), _('IPv6 network in address/netmask notation'));
302 ip6hostid: function() {
303 if (this.value
== "eui64" || this.value
== "random")
306 var v6
= this.factory
.parseIPv6(this.value
);
307 return this.assert(!(!v6
|| v6
[0] || v6
[1] || v6
[2] || v6
[3]), _('valid IPv6 host id'));
310 ipmask: function(negative
) {
311 return this.assert(this.apply('ipmask4', null, [negative
]) || this.apply('ipmask6', null, [negative
]),
312 _('valid network in address/netmask notation'));
315 ipmask4: function(negative
) {
316 return this.assert(this.apply('cidr4', null, [negative
]) || this.apply('ipnet4') || this.apply('ip4addr'),
317 _('valid IPv4 network'));
320 ipmask6: function(negative
) {
321 return this.assert(this.apply('cidr6', null, [negative
]) || this.apply('ipnet6') || this.apply('ip6addr'),
322 _('valid IPv6 network'));
326 var p
= this.factory
.parseInteger(this.value
);
327 return this.assert(p
>= 0 && p
<= 65535, _('valid port value'));
330 portrange: function() {
331 if (this.value
.match(/^(\d+)-(\d+)$/)) {
334 return this.assert(p1
<= p2
&& p2
<= 65535,
335 _('valid port or port range (port1-port2)'));
338 return this.assert(this.apply('port'), _('valid port or port range (port1-port2)'));
341 macaddr: function(multicast
) {
342 var m
= this.value
.match(/^([a-fA-F0-9]{2}):([a-fA-F0-9]{2}:){4}[a-fA-F0-9]{2}$/);
343 return this.assert(m
!= null && !(+m
[1] & 1) == !multicast
,
344 multicast
? _('valid multicast MAC address') : _('valid MAC address'));
347 host: function(ipv4only
) {
348 return this.assert(this.apply('hostname') || this.apply(ipv4only
== 1 ? 'ip4addr' : 'ipaddr', null, ['nomask']),
349 _('valid hostname or IP address'));
352 hostname: function(strict
) {
353 if (this.value
.length
<= 253)
355 (this.value
.match(/^[a-zA-Z0-9_]+$/) != null ||
356 (this.value
.match(/^[a-zA-Z0-9_][a-zA-Z0-9_\-.]*[a-zA-Z0-9]$/) &&
357 this.value
.match(/[^0-9.]/))) &&
358 (!strict
|| !this.value
.match(/^_/)),
359 _('valid hostname'));
361 return this.assert(false, _('valid hostname'));
364 network: function() {
365 return this.assert(this.apply('uciname') || this.apply('hostname') || this.apply('ip4addr') || this.apply('ip6addr'),
366 _('valid UCI identifier, hostname or IP address range'));
369 hostport: function(ipv4only
) {
370 var hp
= this.value
.split(/:/);
371 return this.assert(hp
.length
== 2 && this.apply('host', hp
[0], [ipv4only
]) && this.apply('port', hp
[1]),
372 _('valid host:port'));
375 ip4addrport: function() {
376 var hp
= this.value
.split(/:/);
377 return this.assert(hp
.length
== 2 && this.apply('ip4addr', hp
[0], [true]) && this.apply('port', hp
[1]),
378 _('valid IPv4 address:port'));
381 ipaddrport: function(bracket
) {
382 var m4
= this.value
.match(/^([^\[\]:]+):(\d+)$/),
383 m6
= this.value
.match((bracket
== 1) ? /^\[(.+)\]:(\d+)$/ : /^([^\[\]]+):(\d+)$/);
386 return this.assert(this.apply('ip4addr', m4
[1], [true]) && this.apply('port', m4
[2]),
387 _('valid address:port'));
389 return this.assert(m6
&& this.apply('ip6addr', m6
[1], [true]) && this.apply('port', m6
[2]),
390 _('valid address:port'));
397 return this.assert(v
.match(/^[a-fA-F0-9]{64}$/), _('valid hexadecimal WPA key'));
399 return this.assert((v
.length
>= 8) && (v
.length
<= 63), _('key between 8 and 63 characters'));
405 if (v
.substr(0, 2) === 's:')
408 if ((v
.length
== 10) || (v
.length
== 26))
409 return this.assert(v
.match(/^[a-fA-F0-9]{10,26}$/), _('valid hexadecimal WEP key'));
411 return this.assert((v
.length
=== 5) || (v
.length
=== 13), _('key with either 5 or 13 characters'));
414 uciname: function() {
415 return this.assert(this.value
.match(/^[a-zA-Z0-9_]+$/), _('valid UCI identifier'));
418 range: function(min
, max
) {
419 var val
= this.factory
.parseDecimal(this.value
);
420 return this.assert(val
>= +min
&& val
<= +max
, _('value between %f and %f').format(min
, max
));
424 return this.assert(this.factory
.parseDecimal(this.value
) >= +min
, _('value greater or equal to %f').format(min
));
428 return this.assert(this.factory
.parseDecimal(this.value
) <= +max
, _('value smaller or equal to %f').format(max
));
431 length: function(len
) {
432 return this.assert(bytelen(this.value
) == +len
,
433 _('value with %d characters').format(len
));
436 rangelength: function(min
, max
) {
437 var len
= bytelen(this.value
);
438 return this.assert((len
>= +min
) && (len
<= +max
),
439 _('value between %d and %d characters').format(min
, max
));
442 minlength: function(min
) {
443 return this.assert(bytelen(this.value
) >= +min
,
444 _('value with at least %d characters').format(min
));
447 maxlength: function(max
) {
448 return this.assert(bytelen(this.value
) <= +max
,
449 _('value with at most %d characters').format(max
));
455 for (var i
= 0; i
< arguments
.length
; i
+= 2) {
456 if (typeof arguments
[i
] != 'function') {
457 if (arguments
[i
] == this.value
)
458 return this.assert(true);
459 errors
.push('"%s"'.format(arguments
[i
]));
462 else if (arguments
[i
].apply(this, arguments
[i
+1])) {
463 return this.assert(true);
466 errors
.push(this.error
);
470 var t
= _('One of the following: %s');
472 return this.assert(false, t
.format('\n - ' + errors
.join('\n - ')));
476 for (var i
= 0; i
< arguments
.length
; i
+= 2) {
477 if (typeof arguments
[i
] != 'function') {
478 if (arguments
[i
] != this.value
)
479 return this.assert(false, '"%s"'.format(arguments
[i
]));
482 else if (!arguments
[i
].apply(this, arguments
[i
+1])) {
483 return this.assert(false, this.error
);
487 return this.assert(true);
491 this.value
= this.value
.replace(/^[ \t]*![ \t]*/, '');
493 if (arguments
[0].apply(this, arguments
[1]))
494 return this.assert(true);
496 return this.assert(false, _('Potential negation of: %s').format(this.error
));
499 list: function(subvalidator
, subargs
) {
500 this.field
.setAttribute('data-is-list', 'true');
502 var tokens
= this.value
.match(/[^ \t]+/g);
503 for (var i
= 0; i
< tokens
.length
; i
++)
504 if (!this.apply(subvalidator
, tokens
[i
], subargs
))
505 return this.assert(false, this.error
);
507 return this.assert(true);
510 phonedigit: function() {
511 return this.assert(this.value
.match(/^[0-9\*#!\.]+$/),
512 _('valid phone digit (0-9, "*", "#", "!" or ".")'));
515 timehhmmss: function() {
516 return this.assert(this.value
.match(/^[0-6][0-9]:[0-6][0-9]:[0-6][0-9]$/),
517 _('valid time (HH:MM:SS)'));
520 dateyyyymmdd: function() {
521 if (this.value
.match(/^(\d\d\d\d)-(\d\d)-(\d\d)/)) {
522 var year
= +RegExp
.$1,
525 days_in_month
= [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ];
527 var is_leap_year = function(year
) {
528 return ((!(year
% 4) && (year
% 100)) || !(year
% 400));
531 var get_days_in_month = function(month
, year
) {
532 return (month
=== 2 && is_leap_year(year
)) ? 29 : days_in_month
[month
- 1];
535 /* Firewall rules in the past don't make sense */
536 return this.assert(year
>= 2015 && month
&& month
<= 12 && day
&& day
<= get_days_in_month(month
, year
),
537 _('valid date (YYYY-MM-DD)'));
541 return this.assert(false, _('valid date (YYYY-MM-DD)'));
544 unique: function(subvalidator
, subargs
) {
546 option
= findParent(ctx
.field
, '[data-widget][data-name]'),
547 section
= findParent(option
, '.cbi-section'),
548 query
= '[data-widget="%s"][data-name="%s"]'.format(option
.getAttribute('data-widget'), option
.getAttribute('data-name')),
551 section
.querySelectorAll(query
).forEach(function(sibling
) {
552 if (sibling
=== option
)
555 var input
= sibling
.querySelector('[data-type]'),
556 values
= input
? (input
.getAttribute('data-is-list') ? input
.value
.match(/[^ \t]+/g) : [ input
.value
]) : null;
558 if (values
!== null && values
.indexOf(ctx
.value
) !== -1)
563 return this.assert(false, _('unique value'));
565 if (typeof(subvalidator
) === 'function')
566 return this.apply(subvalidator
, null, subargs
);
568 return this.assert(true);
571 hexstring: function() {
572 return this.assert(this.value
.match(/^([a-f0-9][a-f0-9]|[A-F0-9][A-F0-9])+$/),
573 _('hexadecimal encoded value'));
582 return ValidatorFactory
;