Skip to content

Commit 4002fc1

Browse files
authored
Merge pull request #1769 from plotly/validate-dynamic-enumerated
Validate dynamic enumerated
2 parents 1271382 + 3b0d829 commit 4002fc1

File tree

4 files changed

+79
-3
lines changed

4 files changed

+79
-3
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': {

src/plot_api/validate.js

+18-3
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,11 @@ function crawl(objIn, objOut, schema, list, base, path) {
219219
else if(!Lib.validate(valIn, nestedSchema)) {
220220
list.push(format('value', base, p, valIn));
221221
}
222+
else if(nestedSchema.valType === 'enumerated' &&
223+
((nestedSchema.coerceNumber && valIn !== +valOut) || valIn !== valOut)
224+
) {
225+
list.push(format('dynamic', base, p, valIn, valOut));
226+
}
222227
}
223228

224229
return list;
@@ -267,6 +272,16 @@ var code2msgFunc = {
267272

268273
return inBase(base) + target + ' ' + astr + ' did not get coerced';
269274
},
275+
dynamic: function(base, astr, valIn, valOut) {
276+
return [
277+
inBase(base) + 'key',
278+
astr,
279+
'(set to \'' + valIn + '\')',
280+
'got reset to',
281+
'\'' + valOut + '\'',
282+
'during defaults.'
283+
].join(' ');
284+
},
270285
invisible: function(base) {
271286
return 'Trace ' + base[1] + ' got defaulted to be not visible';
272287
},
@@ -284,7 +299,7 @@ function inBase(base) {
284299
return 'In ' + base + ', ';
285300
}
286301

287-
function format(code, base, path, valIn) {
302+
function format(code, base, path, valIn, valOut) {
288303
path = path || '';
289304

290305
var container, trace;
@@ -301,8 +316,8 @@ function format(code, base, path, valIn) {
301316
trace = null;
302317
}
303318

304-
var astr = convertPathToAttributeString(path),
305-
msg = code2msgFunc[code](base, astr, valIn);
319+
var astr = convertPathToAttributeString(path);
320+
var msg = code2msgFunc[code](base, astr, valIn, valOut);
306321

307322
// log to console if logger config option is enabled
308323
Lib.log(msg);

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() {

test/jasmine/tests/validate_test.js

+41
Original file line numberDiff line numberDiff line change
@@ -392,4 +392,45 @@ describe('Plotly.validate', function() {
392392
'In data trace 2, key transforms[0].type is set to an invalid value (no gonna work)'
393393
);
394394
});
395+
396+
it('should catch input errors for attribute with dynamic defaults', function() {
397+
var out = Plotly.validate([], {
398+
xaxis: {
399+
constrain: 'domain',
400+
constraintoward: 'bottom'
401+
},
402+
yaxis: {
403+
constrain: 'domain',
404+
constraintoward: 'left'
405+
},
406+
xaxis2: {
407+
anchor: 'x3'
408+
},
409+
yaxis2: {
410+
overlaying: 'x'
411+
}
412+
});
413+
414+
expect(out.length).toBe(4);
415+
assertErrorContent(
416+
out[0], 'dynamic', 'layout', null,
417+
['xaxis', 'constraintoward'], 'xaxis.constraintoward',
418+
'In layout, key xaxis.constraintoward (set to \'bottom\') got reset to \'center\' during defaults.'
419+
);
420+
assertErrorContent(
421+
out[1], 'dynamic', 'layout', null,
422+
['yaxis', 'constraintoward'], 'yaxis.constraintoward',
423+
'In layout, key yaxis.constraintoward (set to \'left\') got reset to \'middle\' during defaults.'
424+
);
425+
assertErrorContent(
426+
out[2], 'dynamic', 'layout', null,
427+
['xaxis2', 'anchor'], 'xaxis2.anchor',
428+
'In layout, key xaxis2.anchor (set to \'x3\') got reset to \'y\' during defaults.'
429+
);
430+
assertErrorContent(
431+
out[3], 'dynamic', 'layout', null,
432+
['yaxis2', 'overlaying'], 'yaxis2.overlaying',
433+
'In layout, key yaxis2.overlaying (set to \'x\') got reset to \'false\' during defaults.'
434+
);
435+
});
395436
});

0 commit comments

Comments
 (0)