Skip to content

Commit

Permalink
fix(validators): prevent forEach helper from throwing, when a state p…
Browse files Browse the repository at this point in the history
…roperty has no validator (#932), closes #931
  • Loading branch information
dobromir-hristov committed Sep 9, 2021
1 parent bf54c5d commit 2cd1fbb
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
45 changes: 45 additions & 0 deletions packages/validators/src/utils/__tests__/forEach.spec.js
Expand Up @@ -35,6 +35,51 @@ describe('forEach', () => {
expect(required).toHaveBeenCalledWith('Foo')
})

it('does not throw, if a property does not have a validator', () => {
expect(forEach(rules).$validator([{
name: '',
surname: '' // surname has no validator, but it should not cause errors
}])).toEqual({
$data: [
{
name: {
isFoo: false,
required: false
},
surname: {}
}
],
$errors: [
{
name: [
{
$message: 'Not Foo',
$model: '',
$params: {},
$pending: false,
$property: 'name',
$response: false,
$validator: 'isFoo'
},
{
$message: 'Is Required',
$model: '',
$params: {},
$pending: false,
$property: 'name',
$response: false,
$validator: 'required'
}
],
surname: []
}
],
$valid: false
})
expect(isFoo).toHaveBeenCalledTimes(1)
expect(required).toHaveBeenCalledTimes(1)
})

it('passes the correct this context', () => {
const context = {
foo: 'bar'
Expand Down
2 changes: 1 addition & 1 deletion packages/validators/src/utils/forEach.js
Expand Up @@ -8,7 +8,7 @@ export default function forEach (validators) {
// go over each property
const collectionEntryResult = Object.entries(object).reduce((all, [key, $model]) => {
// get the validators for this property
const innerValidators = validators[key]
const innerValidators = validators[key] || {}
// go over each validator and run it
const propertyResult = Object.entries(innerValidators).reduce((all, [validatorName, currentValidator]) => {
// extract the validator. Supports simple and extended validators.
Expand Down

0 comments on commit 2cd1fbb

Please sign in to comment.