lib/grape/validations/validators/base.rb in grape-0.14.0 vs lib/grape/validations/validators/base.rb in grape-0.15.0
- old
+ new
@@ -15,10 +15,26 @@
@option = options
@required = required
@scope = scope
end
+ # Validates a given request.
+ # @note This method must be thread-safe.
+ # @note Override #validate! unless you need to access the entire request.
+ # @param request [Grape::Request] the request currently being handled
+ # @raise [Grape::Exceptions::Validation] if validation failed
+ # @return [void]
+ def validate(request)
+ validate!(request.params)
+ end
+
+ # Validates a given parameter hash.
+ # @note This method must be thread-safe.
+ # @note Override #validate iff you need to access the entire request.
+ # @param params [Hash] parameters to validate
+ # @raise [Grape::Exceptions::Validation] if validation failed
+ # @return [void]
def validate!(params)
attributes = AttributesIterator.new(self, @scope, params)
attributes.each do |resource_params, attr_name|
if @required || (resource_params.respond_to?(:key?) && resource_params.key?(attr_name))
validate_param!(attr_name, resource_params)
@@ -36,9 +52,19 @@
end
def self.inherited(klass)
short_name = convert_to_short_name(klass)
Validations.register_validator(short_name, klass)
+ end
+
+ def message(default_key = nil)
+ options = instance_variable_get(:@option)
+ options_key?(:message) ? options[:message] : default_key
+ end
+
+ def options_key?(key, options = nil)
+ options = instance_variable_get(:@option) if options.nil?
+ options.respond_to?(:key?) && options.key?(key) && !options[key].nil?
end
end
end
end