Skip to content

Commit

Permalink
fix(multi-field-validation): Make sure that data for multiple fields …
Browse files Browse the repository at this point in the history
…can be validated
  • Loading branch information
akariv committed May 16, 2016
1 parent fd16314 commit 14084c3
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ class TypeProcessor {
});
// ... and data samples match the selected datatype ...
valid = valid &&
_.every(fields, (f) => {
_.every(_.map(fields, (f) => {
if ( !f.type ) { return true; }
if ( f.data ) {
var options = _.pick(f.options,
Expand All @@ -121,7 +121,7 @@ class TypeProcessor {
} else {
return true;
}
});
}));
return valid;
}

Expand Down
40 changes: 40 additions & 0 deletions src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,46 @@ describe('os-types', function() {
}
});
});
it('detects bad data samples and raises proper errors for multiple fields', function() {
var cases = [
['value', {}, ['abcz'], true],
['value', {}, ['123', 'abcz'], true],
['value', {}, ['abcz', '123'], true],
['value', {}, ['123'], false],
['value', {}, ['12.3'], false],
['value', {}, ['12.3'], false],
['value', {}, ['12.3'], false],
['date:generic', {}, ['1978-12-31'], false],
['date:generic', {format:'fmt:YYYY/MM/DD'}, ['1978-12-31'], true],
['date:generic', {format:'fmt:YYYY/MM/DD'}, ['1978/12/31'], false],
['value', {}, ['1,234'], false],
['value', {}, ['1,234.56'], false],
['value', { groupChar:' ', decimalChar:','}, ['1 234,56'], false],
['', {}, ['100'], false],
];
var fields = [
{
name: 'dummy1',
type: 'value',
options: {},
data: ['123','fsdsd','456']
},
{
name: 'dummy2',
type: 'date:fiscal-year',
options: {},
data: ['2012','2013','xxx']
}
];
var model = tp.fieldsToModel(fields);
expect(model).to.be.ok;
expect(model.errors).to.be.ok;
expect(model.errors.perField).to.be.ok;
expect(model.errors.perField.dummy1).to.be.ok;
expect(model.errors.perField.dummy1).to.match(/^Data cannot be cast to this type/);
expect(model.errors.perField.dummy2).to.be.ok;
expect(model.errors.perField.dummy2).to.match(/^Data cannot be cast to this type/);
});
it('creates correctly dimensions & measures', function () {
var fields = _.map(tp.getAllTypes(), (type) => {
var name = type.replace(/:/g, ' ');
Expand Down

0 comments on commit 14084c3

Please sign in to comment.