class Grape::Validations::Validators::Base
def self.inherited(klass)
def self.inherited(klass) super Validations.register(klass) end
def fail_fast?
def fail_fast? @fail_fast end
def initialize(attrs, options, required, scope, opts)
-
opts
(Hash
) -- additional validation options -
scope
(ParamsScope
) -- parent scope for this Validator -
required
(Boolean
) -- attribute(s) are required or optional -
options
(Object
) -- implementation-dependent Validator options -
attrs
(Array
) -- names of attributes to which the Validator applies
def initialize(attrs, options, required, scope, opts) @attrs = Array(attrs) @option = options @required = required @scope = scope @fail_fast = opts[:fail_fast] @allow_blank = opts[:allow_blank] end
def message(default_key = nil)
def message(default_key = nil) options = instance_variable_get(:@option) options_key?(:message) ? options[:message] : default_key end
def options_key?(key, options = nil)
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
def validate(request)
-
(void)
-
Raises:
-
(Grape::Exceptions::Validation)
- if validation failed
Parameters:
-
request
(Grape::Request
) -- the request currently being handled
Other tags:
- Note: - Override #validate! unless you need to access the entire request.
def validate(request) return unless @scope.should_validate?(request.params) validate!(request.params) end
def validate!(params)
-
(void)
-
Raises:
-
(Grape::Exceptions::Validation)
- if validation failed
Parameters:
-
params
(Hash
) -- parameters to validate
Other tags:
- Note: - Override #validate if you need to access the entire request.
def validate!(params) attributes = SingleAttributeIterator.new(self, @scope, params) # we collect errors inside array because # there may be more than one error per field array_errors = [] attributes.each do |val, attr_name, empty_val| next if !@scope.required? && empty_val next unless @scope.meets_dependency?(val, params) validate_param!(attr_name, val) if @required || (val.respond_to?(:key?) && val.key?(attr_name)) rescue Grape::Exceptions::Validation => e array_errors << e end raise Grape::Exceptions::ValidationArrayErrors.new(array_errors) if array_errors.any? end