Skip to content

Commit 6d3a48a

Browse files
committed
add special validateFunction for enumerated valType
- so that attribute that declare possible values as regex e.g. axis anchor and overlaying pass `Lib.validate()`
1 parent 1271382 commit 6d3a48a

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

src/lib/coerce.js

+14
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,20 @@ exports.valObjects = {
4444
if(opts.coerceNumber) v = +v;
4545
if(opts.values.indexOf(v) === -1) propOut.set(dflt);
4646
else propOut.set(v);
47+
},
48+
validateFunction: function(v, opts) {
49+
if(opts.coerceNumber) v = +v;
50+
51+
var values = opts.values;
52+
for(var i = 0; i < values.length; i++) {
53+
var k = String(values[i]);
54+
55+
if((k.charAt(0) === '/' && k.charAt(k.length - 1) === '/')) {
56+
var regex = new RegExp(k.substr(1, k.length - 2));
57+
if(regex.test(v)) return true;
58+
} else if(v === values[i]) return true;
59+
}
60+
return false;
4761
}
4862
},
4963
'boolean': {

test/jasmine/tests/lib_test.js

+6
Original file line numberDiff line numberDiff line change
@@ -978,6 +978,12 @@ describe('Test lib.js:', function() {
978978
arrayOk: true,
979979
dflt: 'a'
980980
});
981+
982+
assert(['x', 'x2'], ['xx', 'x0', undefined], {
983+
valType: 'enumerated',
984+
values: ['/^x([2-9]|[1-9][0-9]+)?$/'],
985+
dflt: 'x'
986+
});
981987
});
982988

983989
it('should work for valType \'boolean\' where', function() {

0 commit comments

Comments
 (0)