How to validate query parameters in Reform::Form in Trailblazer?

305 views Asked by At

I am working on a GET API using Trailblazer which receives a list of comma separated list of numeric ids. desired-path?page_number=4&ids=765,359249,4011 How can I validate through Reform::Form that the ids query parameter has only integers in it

1

There are 1 answers

0
Eureton On

It depends on whether you are on dry-validation version 0.x or above. The name of your parameter leads me to believe that:

  • it is not backed by a model
  • it is not required

For these reasons, validate with optional and annotate the property with virtual: true (relevant docs here, here and here):

class MyContract < Reform::Form
  property :ids, virtual: true

  validation do
    # for 0.x:
    #   optional(:ids).each(:int?)
    # for 1.x:
    optional(:ids).array(:integer)
  end
end

Documentation on validating array input on version 0.x is here and the same for 1.x is here.