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

Array params with at_least_one_of having custom validation message #396

Open
milap-neupane opened this issue Apr 23, 2016 · 5 comments
Open

Comments

@milap-neupane
Copy link

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
Copy link
Author

I have added a test showing the failure in this PR #397 . This will be fixed when grape is updated to 0.16.2

@LeFnord
Copy link
Member

LeFnord commented Apr 24, 2016

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

@milap-neupane
Copy link
Author

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 ?

@LeFnord
Copy link
Member

LeFnord commented Apr 25, 2016

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 …

@milap-neupane
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants