Add ArrayOf validator - #444
Conversation
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
|
When you have an Array input, the main use cases are:
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:
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 |
|
I like the idea of this PR and if I understand @squidget, the suggestion is not to limit the params to just the |
69442b9 to
8215e30
Compare
The intended usage
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
Integerthat validator will be usedMotivation
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 thisis valid input.
But if we do
an error will be raised for
"1"is not an numberMiscs
I realize the existence of #189 but that PR doesn't solve this particular problem. Nor did
ArrayClassValidatorIf you fee this PR is worthy I will add README section
Thanks!