Skip to content

Commit 3b0d829

Browse files
committed
add special handler in Plotly.validate for enumerated w/ dynamic values
- e.g. axis 'anchor' declare both x and y values, but coerce x or y values depending on the container. - similarly for 'overlaying' and 'contraintoward'
1 parent 6d3a48a commit 3b0d829

File tree

2 files changed

+59
-3
lines changed

2 files changed

+59
-3
lines changed

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/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)