Skip to content

Commit 40dd784

Browse files
committed
validate info_array, make grid defaults work better with validate
1 parent 446f9f9 commit 40dd784

File tree

3 files changed

+119
-5
lines changed

3 files changed

+119
-5
lines changed

src/plot_api/validate.js

+51-5
Original file line numberDiff line numberDiff line change
@@ -164,19 +164,65 @@ function crawl(objIn, objOut, schema, list, base, path) {
164164
var valIn = objIn[k],
165165
valOut = objOut[k];
166166

167-
var nestedSchema = getNestedSchema(schema, k),
168-
isInfoArray = (nestedSchema || {}).valType === 'info_array',
169-
isColorscale = (nestedSchema || {}).valType === 'colorscale';
167+
var nestedSchema = getNestedSchema(schema, k);
168+
var isInfoArray = (nestedSchema || {}).valType === 'info_array';
169+
var isColorscale = (nestedSchema || {}).valType === 'colorscale';
170+
var items = (nestedSchema || {}).items;
170171

171172
if(!isInSchema(schema, k)) {
172173
list.push(format('schema', base, p));
173174
}
174175
else if(isPlainObject(valIn) && isPlainObject(valOut)) {
175176
crawl(valIn, valOut, nestedSchema, list, base, p);
176177
}
178+
else if(isInfoArray && isArray(valIn)) {
179+
if(valIn.length > valOut.length) {
180+
list.push(format('unused', base, p.concat(valOut.length)));
181+
}
182+
var len = valOut.length;
183+
var arrayItems = Array.isArray(items);
184+
if(arrayItems) len = Math.min(len, items.length);
185+
var m, n, item, valInPart, valOutPart;
186+
if(nestedSchema.dimensions === 2) {
187+
for(n = 0; n < len; n++) {
188+
if(isArray(valIn[n])) {
189+
if(valIn[n].length > valOut[n].length) {
190+
list.push(format('unused', base, p.concat(n, valOut[n].length)));
191+
}
192+
var len2 = valOut[n].length;
193+
for(m = 0; m < (arrayItems ? Math.min(len2, items[n].length) : len2); m++) {
194+
item = arrayItems ? items[n][m] : items;
195+
valInPart = valIn[n][m];
196+
valOutPart = valOut[n][m];
197+
if(!Lib.validate(valInPart, item)) {
198+
list.push(format('value', base, p.concat(n, m), valInPart));
199+
}
200+
else if(valOutPart !== valInPart && valOutPart !== +valInPart) {
201+
list.push(format('dynamic', base, p.concat(n, m), valInPart, valOutPart));
202+
}
203+
}
204+
}
205+
else {
206+
list.push(format('array', base, p.concat(n), valIn[n]));
207+
}
208+
}
209+
}
210+
else {
211+
for(n = 0; n < len; n++) {
212+
item = arrayItems ? items[n] : items;
213+
valInPart = valIn[n];
214+
valOutPart = valOut[n];
215+
if(!Lib.validate(valInPart, item)) {
216+
list.push(format('value', base, p.concat(n), valInPart));
217+
}
218+
else if(valOutPart !== valInPart && valOutPart !== +valInPart) {
219+
list.push(format('dynamic', base, p.concat(n), valInPart, valOutPart));
220+
}
221+
}
222+
}
223+
}
177224
else if(nestedSchema.items && !isInfoArray && isArray(valIn)) {
178-
var items = nestedSchema.items,
179-
_nestedSchema = items[Object.keys(items)[0]],
225+
var _nestedSchema = items[Object.keys(items)[0]],
180226
indexList = [];
181227

182228
var j, _p;

src/plots/grid.js

+3
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,8 @@ exports.contentDefaults = function(layoutIn, layoutOut) {
276276
}
277277
else subplotId = rowIn[j];
278278

279+
rowOut[j] = '';
280+
279281
if(subplots.cartesian.indexOf(subplotId) !== -1) {
280282
yPos = subplotId.indexOf('y');
281283
xId = subplotId.slice(0, yPos);
@@ -390,6 +392,7 @@ function fillGridAxes(axesIn, axesAllowed, len, axisMap, axLetter) {
390392
out[i] = axisId;
391393
axisMap[axisId] = i;
392394
}
395+
else out[i] = '';
393396
}
394397

395398
if(Array.isArray(axesIn)) {

test/jasmine/tests/validate_test.js

+65
Original file line numberDiff line numberDiff line change
@@ -440,4 +440,69 @@ describe('Plotly.validate', function() {
440440
'In layout, key yaxis2.overlaying (set to \'x\') got reset to \'false\' during defaults.'
441441
);
442442
});
443+
444+
it('catches bad axes in grid definitions', function() {
445+
var out = Plotly.validate([
446+
{y: [1, 2]},
447+
{y: [1, 2], xaxis: 'x2', yaxis: 'y2'}
448+
], {
449+
grid: {xaxes: ['x3', '', 'x2', 4], yaxes: ['y', 3, '', 'y4']},
450+
// while we're at it check on another info_array
451+
xaxis: {range: [5, 'lots']}
452+
});
453+
454+
expect(out.length).toBe(5);
455+
assertErrorContent(
456+
out[0], 'dynamic', 'layout', null,
457+
['grid', 'xaxes', 0], 'grid.xaxes[0]',
458+
'In layout, key grid.xaxes[0] (set to \'x3\') got reset to \'\' during defaults.'
459+
);
460+
assertErrorContent(
461+
out[1], 'value', 'layout', null,
462+
['grid', 'xaxes', 3], 'grid.xaxes[3]',
463+
'In layout, key grid.xaxes[3] is set to an invalid value (4)'
464+
);
465+
assertErrorContent(
466+
out[2], 'value', 'layout', null,
467+
['grid', 'yaxes', 1], 'grid.yaxes[1]',
468+
'In layout, key grid.yaxes[1] is set to an invalid value (3)'
469+
);
470+
assertErrorContent(
471+
out[3], 'dynamic', 'layout', null,
472+
['grid', 'yaxes', 3], 'grid.yaxes[3]',
473+
'In layout, key grid.yaxes[3] (set to \'y4\') got reset to \'\' during defaults.'
474+
);
475+
assertErrorContent(
476+
out[4], 'dynamic', 'layout', null,
477+
['xaxis', 'range', 1], 'xaxis.range[1]',
478+
'In layout, key xaxis.range[1] (set to \'lots\') got reset to \'50\' during defaults.'
479+
);
480+
});
481+
482+
it('catches bad subplots in grid definitions', function() {
483+
var out = Plotly.validate([
484+
{y: [1, 2]},
485+
{y: [1, 2], xaxis: 'x2', yaxis: 'y2'},
486+
{y: [1, 2], xaxis: 'x2'}
487+
], {
488+
grid: {subplots: [['xy', 'x2y3'], ['x2y', 'x2y2'], [5, '']]},
489+
});
490+
491+
expect(out.length).toBe(3);
492+
assertErrorContent(
493+
out[0], 'dynamic', 'layout', null,
494+
['grid', 'subplots', 0, 1], 'grid.subplots[0][1]',
495+
'In layout, key grid.subplots[0][1] (set to \'x2y3\') got reset to \'\' during defaults.'
496+
);
497+
assertErrorContent(
498+
out[1], 'dynamic', 'layout', null,
499+
['grid', 'subplots', 1, 0], 'grid.subplots[1][0]',
500+
'In layout, key grid.subplots[1][0] (set to \'x2y\') got reset to \'\' during defaults.'
501+
);
502+
assertErrorContent(
503+
out[2], 'value', 'layout', null,
504+
['grid', 'subplots', 2, 0], 'grid.subplots[2][0]',
505+
'In layout, key grid.subplots[2][0] is set to an invalid value (5)'
506+
);
507+
});
443508
});

0 commit comments

Comments
 (0)