Skip to content

Commit

Permalink
feat: Add field config to result object
Browse files Browse the repository at this point in the history
This should have the same structure as the errors so the code below
should return the configuration value of the offended rule/subrule.

```
_.get(result.config, result.rule);
```
  • Loading branch information
relekang committed May 23, 2016
1 parent 710e122 commit 23af4db
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 6 deletions.
14 changes: 12 additions & 2 deletions src/__tests__/validator_config_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,18 @@ test('Validator should validate required rules', t => {
});

t.deepEqual(result, {
noValue: { field: 'noValue', rule: 'required', value: null },
notInObject: { field: 'notInObject', rule: 'required', value: undefined },
noValue: {
field: 'noValue',
rule: 'required',
value: null,
config: { required: true },
},
notInObject: {
field: 'notInObject',
rule: 'required',
value: undefined,
config: { required: true },
},
});
t.deepEqual(result, validator.getErrors());
});
12 changes: 10 additions & 2 deletions src/__tests__/validator_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ test('validate should filter out valid values', t => {
test('validateField should update errors', t => {
const validator = new Validator({ a: { required: true }, b: { required: true } });
validator.validateField('a', null);
t.deepEqual(validator.errors, { a: { field: 'a', rule: 'required', value: null } });
t.deepEqual(
validator.errors,
{ a: { field: 'a', rule: 'required', value: null, config: { required: true } } },
);
validator.validateField('a', 'c');
t.deepEqual(validator.errors, {});
});
Expand All @@ -49,7 +52,12 @@ test('validateField should return object with offending rule name', t => {

t.deepEqual(
validator.validateField('a', '2.0'),
{ field: 'a', rule: 'numeric/integerOnly', value: '2.0' }
{
field: 'a',
rule: 'numeric.integerOnly',
value: '2.0',
config: { numeric: { integerOnly: true } },
}
);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ import { arrayOf } from '../';
test('rules.arrayOf should return array of validation errors', t => {
t.deepEqual(
arrayOf('elements', [{}], { name: { required: true } }),
[{ name: { rule: 'required', value: undefined, field: 'name' } }]
[{ name: { rule: 'required', value: undefined, field: 'name', config: { required: true } } }]
);
});
2 changes: 1 addition & 1 deletion src/validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export default class Validator {
);

if (result) {
result = { field, rule: result, value };
result = { field, rule: result, value, config: this.config[field] };
this.errors = assign({}, this.errors, { [field]: result });
break;
} else {
Expand Down

0 comments on commit 23af4db

Please sign in to comment.