Created
December 24, 2013 02:11
-
-
Save lloydmeta/8107876 to your computer and use it in GitHub Desktop.
Range validator for Grape.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class RangeValidator < Grape::Validations::Validator | |
def initialize(attrs, options, required, scope) | |
@range = options | |
@required = required | |
raise ArgumentError, "Range provided (#{@range.inspect}) does not implement #cover?" unless @range.respond_to? :cover? | |
super | |
end | |
def validate_param!(attr_name, params) | |
if (params[attr_name] || @required) && [email protected]?(params[attr_name]) | |
raise Grape::Exceptions::Validation, | |
:param => attr_name.to_s, | |
:message => "#{attr_name} should be within #{@range}" | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example