grape-swagger icon indicating copy to clipboard operation
grape-swagger copied to clipboard

Array params with at_least_one_of having custom validation message

Open milap-neupane opened this issue 8 years ago • 5 comments

When we have array as a params with elements and a validation like at_least_one_of, mutually_exclusive, ... the generated JSON has an extra field which is undesired. This field is present only when we have a custom message filed passed in the validation (message: 'is missing')

params do
  requires :my_top_arr, type: Array, allow_blank: false do
    optional :my_elem1,
             type: Float
    optional :my_elem2,
             type: String
    at_least_one_of :my_elem2,
                    :status,
                    message: 'is missing'
  end
end

The generated JSON is:

[
  [0] {
    "description": null,
             "in": "formData",
          "items": {
      "type": "float"
    },
           "name": "my_top_arr[][my_elem1]",
       "required": false,
           "type": "array"
  },
  [1] {
    "description": null,
             "in": "formData",
          "items": {
      "type": "string"
    },
           "name": "my_top_arr[][my_elem2]",
       "required": false,
           "type": "array"
  },
  [2] {
    "description": null,
             "in": "formData",
          "items": {
      "type": "string"
    },
           "name": "my_top_arr[][my_top_arr]",
       "required": false,
           "type": "array"
  },
  [3] {
    "description": null,
             "in": "formData",
          "items": {
      "type": "string"
    },
           "name": "my_top_arr[][{:message=>\"is missing\"}]",
       "required": false,
           "type": "array"
  }
]

The last field my_top_arr[][{:message=>\"is missing\"}] seems to be extra generated.

This field is generated only when i pass message: 'is missing' to the at_least_one_of validation. *The message key is used by grape for custom validation message.

milap-neupane avatar Apr 23 '16 05:04 milap-neupane

I have added a test showing the failure in this PR https://github.com/ruby-grape/grape-swagger/pull/397 . This will be fixed when grape is updated to 0.16.2

milap-neupane avatar Apr 23 '16 06:04 milap-neupane

thanks for the spec PR …

but it is a more general problem: how should the grape validation specials, such at_least_one_of, exactly_one_of, etc be translated to swagger, there is no one-to-one possibility to doing it, or better I don't see it, maybe you have an idea, how this could be done

LeFnord avatar Apr 24 '16 11:04 LeFnord

I also think the validation specials cannot be mapped to swagger. Grape has it's validation special and is also flexible to make custom validation special. So these all cannot be handled. I think we should only consider requires and optional.

The above issue is actually when we pass message as a param in the validation specials (at_least_one_of, ...). When the message is not passed it works well. Updating Grape to 0.16.2 will fix this. What are your plans on the update of grape gem ?

milap-neupane avatar Apr 25 '16 06:04 milap-neupane

yeap, that is my meaning, at the moment, we can only support requires and optional parameter, but no grape validations …

and how should it look like? how did you run grape-swagger with grape 0.16.2?

at the moment I'm on refactoring, so other could easy dive in …

LeFnord avatar Apr 25 '16 07:04 LeFnord

I simply updated the gem and ran the test nothing more.

milap-neupane avatar Apr 25 '16 13:04 milap-neupane