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

Add ArrayOf validator #444

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Add ArrayOf validator #444

wants to merge 1 commit into from

Conversation

ywen
Copy link

@ywen ywen commented Mar 17, 2016

The intended usage

param :ids, ArrayOfValidator.for_class, of: Integer

Explanation

Instead of simply checking the value is an Integer, it will try to find
a validator for that type. It means if there is a validator processing Integer that validator will be used

Motivation

Rails often pass Integers as string. For example, instead of ids: [1, 2]
it could be ids: ["1", "2"]. In this case, we would still think this
is valid input.

But if we do

param :ids, Array, of: Integer

an error will be raised for "1" is not an number

Miscs

I realize the existence of #189 but that PR doesn't solve this particular problem. Nor did ArrayClassValidator

If you fee this PR is worthy I will add README section

Thanks!

The intended usage

```ruby
param :ids, ArrayOfValidator.for_class, of: Integer
```

Explanation

Instead of simply checking the value is an Integer, it will try to find
a validator for that type.

Motivation

Rails of pass Integers as string. For example, instead of `ids: [1, 2]`
it could be `ids: ["1", "2"]`. In this case, we would still think this
is valid input.

But if we do

```ruby
param :ids, Array, of: Integer
```

an error will be raised for `"1"` is not an number
@squidget
Copy link

When you have an Array input, the main use cases are:

  • an array of fixed length, where each index has its own validator. This is covered by NestedValidator
  • an array of variable length, where all elements must be of the same standard type (String, Integer, etc.) This is covered by Array validator with the of: option.
  • an array of variable length, where each element must pass some custom validation (eg. an Integer between 0 and 100). There is no way to do this currently using the Array or NestedValidator.

Say I have written a custom validator that responds to :numeric to validate numbers that has optional parameters :min and :max to do optional validation of the range. I could do something like this:

param :percent, :numeric, :min => 0, :max => 100, :desc => 'Percentage value'

But what if I had an array, for which all values must be a number between 0 and 100? The of: parameter doesn't work in this case.

I think this solution goes some way towards covering this scenario, but not completely. What you'd ideally want is a way to specify another validator for all the elements of the array, including custom parameters to configure that validator

@iNecas
Copy link
Member

iNecas commented Mar 28, 2017

I like the idea of this PR and if I understand @squidget, the suggestion is not to limit the params to just the :of, but actually taking the rest of params (perhaps except the ones like :desc) and pass it to the sub-validator. That would make sense as well.

@ofedoren ofedoren force-pushed the master branch 2 times, most recently from 69442b9 to 8215e30 Compare July 25, 2021 19:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants