Skip to content

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
3 participants