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

Custom Validation error messages #1295

Closed
wants to merge 1 commit into from

Conversation

railsmith
Copy link

Provides settings for custom validation error messages. Motivated by requests to build this feature on here Grape Custom Validation error message and here #621

If custom error message options are not provided error messages fall backs to defaults specified in grape/locale/en.yml. If you want i18n for your custom error messages passing locale symbol keys will translate the messages.

presence, allow_blank, values, regexp

params do
   requires :name, values: { value: 1..10, message: 'not in range from 1 to 10' },
                            allow_blank: { value: false, message: 'cannot be blank' },
                            regexp: { value: /^[a-z]+$/, message: 'format is invalid' }, 
                            message: 'is required'
end

all_or_none_of

params do
    optional :beer
    optional :wine
    optional :juice
    all_or_none_of :beer, :wine, :juice, message: "all params are required or none is required"
end

mutually_exclusive

params do
    optional :beer
    optional :wine
    optional :juice
    mutually_exclusive :beer, :wine, :juice, message: "are mutually exclusive cannot pass both params"
 end

exactly_one_of

 params do
     optional :beer
     optional :wine
     optional :juice
     exactly_one_of :beer, :wine, :juice, message: {exactly_one: "are missing, exactly one parameter is required", mutual_exclusion: "are mutually exclusive, exactly one parameter is required"}
 end

at_least_one_of

params do
     optional :beer
     optional :wine
     optional :juice
     at_least_one_of :beer, :wine, :juice, message: "are missing, please specify at least one param"
 end

Coerce

 params do
    requires :int, type: {value: Integer, message: "type cast is invalid" }
  end

@dblock
Copy link
Member

dblock commented Feb 26, 2016

Thanks! Looking forward to code reviewing this - can you please rebase/squash it first? There're a lot of unrelated changes here to grok.

@railsmith railsmith force-pushed the custom-messages branch 2 times, most recently from 2f24881 to eb95f74 Compare February 26, 2016 22:05
@railsmith
Copy link
Author

I have squashed it. There are still one or two unrelated changes which have penetrated while rebasing. Hope it helps to review the code.

@dblock
Copy link
Member

dblock commented Feb 26, 2016

Well those two changes shouldn't be there :) This should fix it:

git checkout master
git pull upstream master
git checkout custom-messages
git rebase master
git push origin custom-messages -f

@dblock
Copy link
Member

dblock commented Feb 26, 2016

The change itself looks OK to me. It needs a README update and CHANGELOG. I am glad to see a very full test coverage too, wonder if there's a way to simplify the implementation a bit so that you don't have to copy paste the thing again and again. Like maybe enumerating something in the messages YML?

@railsmith
Copy link
Author

Originally I had a similar idea in my mind. We can certainly enumerate messages from a YML file and extract the key based on the current route description. We can first check for the message in the message settings DSL, if it doesn't exist there then we can check in the messages.yml and if it doesn't exist there too we can safely fall back to message keys in en.yml. What do you propose?

# messages.yml

grape:
   errors:
      messages:
         endpoint:
             /custom_messages:
                 verb:
                     get:
                        email:
                            presence: 'is required'
                            blank: 'has no value'
             /:
                 verb:
                     post:
                          number:
                             values: 'not in range 1 to 10'

@dblock
Copy link
Member

dblock commented Feb 27, 2016

I don't have any great suggestions wrt your question :)

Please update README and CHANGELOG and this is good to go.

@railsmith
Copy link
Author

Thanks! I am done with this.

@dblock
Copy link
Member

dblock commented Feb 29, 2016

Merged via e6a6c9c (I re-added a CHANGELOG line "Your contribution here"). Thanks!

@dblock dblock closed this Feb 29, 2016
@railsmith railsmith deleted the custom-messages branch March 1, 2016 06:08
@dblock
Copy link
Member

dblock commented Mar 7, 2016

This bit me on a real life project, returning empty messages when a Grape::Exceptions::Validation was raised explicitly. I added a note in UPGRADING in 77ec1cd to rename message_key to message. We could have deprecated message_key first with a warning, but I think it's OK to get rid of it here since it's rare to be implementing your own validators and it's easily fixed.

@railsmith
Copy link
Author

The spec for Custom Validators with an explicit message_key was indeed failing and I updated the spec by changing message_key to message but forgot to deprecate message_key and update the README for Custom Validators as I thought it was trivial but it looks OK and updated now.

przbadu pushed a commit to przbadu/grape that referenced this pull request Apr 5, 2016
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