Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate dynamic enumerated #1769

Merged
merged 2 commits into from
Jun 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/lib/coerce.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,20 @@ exports.valObjects = {
if(opts.coerceNumber) v = +v;
if(opts.values.indexOf(v) === -1) propOut.set(dflt);
else propOut.set(v);
},
validateFunction: function(v, opts) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This thing in only called during Lib.validate. We need this so that make enumerated attributes like xaxis.anchor that declare regex work with Lib.validate.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh and I should add, I didn't want to add regex support in the (regular) coerceFunction defined above for performance reasons.

if(opts.coerceNumber) v = +v;

var values = opts.values;
for(var i = 0; i < values.length; i++) {
var k = String(values[i]);

if((k.charAt(0) === '/' && k.charAt(k.length - 1) === '/')) {
var regex = new RegExp(k.substr(1, k.length - 2));
if(regex.test(v)) return true;
} else if(v === values[i]) return true;
}
return false;
}
},
'boolean': {
Expand Down
21 changes: 18 additions & 3 deletions src/plot_api/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,11 @@ function crawl(objIn, objOut, schema, list, base, path) {
else if(!Lib.validate(valIn, nestedSchema)) {
list.push(format('value', base, p, valIn));
}
else if(nestedSchema.valType === 'enumerated' &&
((nestedSchema.coerceNumber && valIn !== +valOut) || valIn !== valOut)
) {
list.push(format('dynamic', base, p, valIn, valOut));
}
}

return list;
Expand Down Expand Up @@ -267,6 +272,16 @@ var code2msgFunc = {

return inBase(base) + target + ' ' + astr + ' did not get coerced';
},
dynamic: function(base, astr, valIn, valOut) {
return [
inBase(base) + 'key',
astr,
'(set to \'' + valIn + '\')',
'got reset to',
'\'' + valOut + '\'',
'during defaults.'
].join(' ');
},
invisible: function(base) {
return 'Trace ' + base[1] + ' got defaulted to be not visible';
},
Expand All @@ -284,7 +299,7 @@ function inBase(base) {
return 'In ' + base + ', ';
}

function format(code, base, path, valIn) {
function format(code, base, path, valIn, valOut) {
path = path || '';

var container, trace;
Expand All @@ -301,8 +316,8 @@ function format(code, base, path, valIn) {
trace = null;
}

var astr = convertPathToAttributeString(path),
msg = code2msgFunc[code](base, astr, valIn);
var astr = convertPathToAttributeString(path);
var msg = code2msgFunc[code](base, astr, valIn, valOut);

// log to console if logger config option is enabled
Lib.log(msg);
Expand Down
6 changes: 6 additions & 0 deletions test/jasmine/tests/lib_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -978,6 +978,12 @@ describe('Test lib.js:', function() {
arrayOk: true,
dflt: 'a'
});

assert(['x', 'x2'], ['xx', 'x0', undefined], {
valType: 'enumerated',
values: ['/^x([2-9]|[1-9][0-9]+)?$/'],
dflt: 'x'
});
});

it('should work for valType \'boolean\' where', function() {
Expand Down
41 changes: 41 additions & 0 deletions test/jasmine/tests/validate_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -392,4 +392,45 @@ describe('Plotly.validate', function() {
'In data trace 2, key transforms[0].type is set to an invalid value (no gonna work)'
);
});

it('should catch input errors for attribute with dynamic defaults', function() {
var out = Plotly.validate([], {
xaxis: {
constrain: 'domain',
constraintoward: 'bottom'
},
yaxis: {
constrain: 'domain',
constraintoward: 'left'
},
xaxis2: {
anchor: 'x3'
},
yaxis2: {
overlaying: 'x'
}
});

expect(out.length).toBe(4);
assertErrorContent(
out[0], 'dynamic', 'layout', null,
['xaxis', 'constraintoward'], 'xaxis.constraintoward',
'In layout, key xaxis.constraintoward (set to \'bottom\') got reset to \'center\' during defaults.'
);
assertErrorContent(
out[1], 'dynamic', 'layout', null,
['yaxis', 'constraintoward'], 'yaxis.constraintoward',
'In layout, key yaxis.constraintoward (set to \'left\') got reset to \'middle\' during defaults.'
);
assertErrorContent(
out[2], 'dynamic', 'layout', null,
['xaxis2', 'anchor'], 'xaxis2.anchor',
'In layout, key xaxis2.anchor (set to \'x3\') got reset to \'y\' during defaults.'
);
assertErrorContent(
out[3], 'dynamic', 'layout', null,
['yaxis2', 'overlaying'], 'yaxis2.overlaying',
'In layout, key yaxis2.overlaying (set to \'x\') got reset to \'false\' during defaults.'
);
});
});