ruby-grape / ruby-grape/grape-swagger

Array params with at_least_one_of having custom validation message

Open
#396 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

discuss! swagger-2.0
Dominant language
Ruby
Stars
1.1k
Forks
479
Avg merge
2d 14h
Merged PRs (30d)
6

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the params definition and inspect the code that turns validation options into generated formData parameters. Confirm that supplying a custom message creates the extra parameter name, then verify that the unwanted field is absent while the valid array element parameters remain.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
api
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.