busypeoples / spected

Validation library
MIT License
703 stars 32 forks source link

Add support for validation of array of non-object values #85

Closed jakubkoci closed 7 years ago

jakubkoci commented 7 years ago

At first, thanks for your good job with this library I really like it.

I would like to be able to validate property which has array of strings for example.

const input = {
  users: [
    'foobar', 
    'longerstring'
  ]
}

const validationRules = {
  users: [[ 
    array => array.every(item => item.length > 6), 
    'Every item in array must be string with at least 5 characters' 
  ]],
}

const validationResult = spected(validationRules, input)

And it would return this:

{ users: true }

or this:

{ users: [ 'Every item in array must be string with at least 5 characters' ] }

depending on validation predicate and input data.

Currently, it retuns error:

TypeError: Invalid attempt to destructure non-iterable instance

I have tried to avoid validation of such property by omitting it from validation rules (that would be also nice to be able to do that, such as with other properties according to docs), but it also returns error:

TypeError: Cannot read property '0' of undefined
busypeoples commented 7 years ago

I will have a look at the issue.

busypeoples commented 7 years ago

Yes, will add support for this use case in 0.5. Thanks for the very valuable input!

busypeoples commented 7 years ago

Updated to 0.5, this should work now.