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

Fix: mutually_exclusive and exactly_one_of validation error messages now label parameters as strings, consistently with requires and optional. #687

Merged
merged 2 commits into from
Jul 24, 2014

Conversation

dblock
Copy link
Member

@dblock dblock commented Jul 23, 2014

I was getting

"[:user_id, :profile_id]" => ["- exactly one parameter must be provided"]

but at the same time

"user_id" => ["is missing"]

This also enables returning JSON, ie. not translating an array of parameters into a string.

@dblock
Copy link
Member Author

dblock commented Jul 23, 2014

@oliverbarnes and @Morred, check this out pls.

@@ -29,4 +29,4 @@ en:
unknown_options: 'unknown options: %{options}'
incompatible_option_values: '%{option1}: %{value1} is incompatible with %{option2}: %{value2}'
mutual_exclusion: 'are mutually exclusive'
exactly_one: "- exactly one parameter must be provided"
exactly_one: 'are missing, exactly one parameter must be provided'
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You get a mutually exclusive error when more than one are present, so this is only used when none are present.

@oliverbarnes
Copy link
Contributor

makes sense 👍

@dblock
Copy link
Member Author

dblock commented Jul 24, 2014

For reference, I've also added a JSON formatter to the validation errors. The README now says:

The default response from a Grape::Exceptions::ValidationErrors is a humanly readable string, such as "beer, wine are mutually exclusive", in the following example.

params do
  optional :beer
  optional :wine
  optional :juice
  exactly_one_of :beer, :wine, :juice
end

You can rescue a Grape::Exceptions::ValidationErrors and respond with a custom response or turn the response into well-formatted JSON for a JSON API that separates individual parameters and the corresponding error messages. The following rescue_from example produces [{"params":["beer","wine"],"messages":["are mutually exclusive"]}].

format :json
subject.rescue_from Grape::Exceptions::ValidationErrors do |e|
  rack_response e.to_json, 400
end

@dblock
Copy link
Member Author

dblock commented Jul 24, 2014

Note that you cannot just turn ValidationErrors#errors into a hash, JSON doesn't allow multiple values in the key.

dblock added a commit that referenced this pull request Jul 24, 2014
Fix: mutually_exclusive and exactly_one_of validation error messages now label parameters as strings, consistently with requires and optional.
@dblock dblock merged commit 4efcc0b into ruby-grape:master Jul 24, 2014
@oliverbarnes
Copy link
Contributor

I like that, nice + elegant solution

@dblock dblock deleted the consistently-label-errors branch July 24, 2014 22:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants