class Grape::Validations::Validators::Base
(e.g. memoize, ||=) will raise FrozenError at request time.
from them are frozen by construction. Lazy ivar assignment
arrive pre-frozen from the DSL boundary, so subclass ivars derived
initialization (via .new). All inputs (options, opts, attrs)
Validator instances are shared across requests and are frozen after
== Freeze contract
Base class for all parameter validators.
def default_message_key(key = nil)
The key is resolved through +message+, so a per-option +:message+
end
default_message_key :my_error
class MyValidator < Grape::Validations::Validators::Base
at the class level instead of overriding +initialize+:
Subclasses that only need a single fixed error message can declare it
Declares the default I18n message key used by +validation_error!+.
def default_message_key(key = nil) if key @default_message_key = key else @default_message_key || (superclass.respond_to?(:default_message_key) ? superclass.default_message_key : nil) end end
def hash_like?(obj)
def hash_like?(obj) obj.respond_to?(:key?) end
def inherited(klass)
def inherited(klass) super Validations.register(klass) end
def initialize(attrs, options, required, scope, opts)
-
opts(Hash) -- shared validator options; only +:allow_blank+ and -
scope(ParamsScope) -- parent scope for this Validator -
required(Boolean) -- attribute(s) are required or optional -
options(Object) -- implementation-dependent Validator options; deep-frozen on assignment -
attrs(Array) -- names of attributes to which the Validator applies
def initialize(attrs, options, required, scope, opts) @attrs = Array(attrs).freeze @options = Grape::Util::DeepFreeze.deep_freeze(options) @option = @options # TODO: remove in next major release @required = required @scope = scope @opts = SharedOptions.new(**opts.slice(:allow_blank, :fail_fast)) @exception_message = message(self.class.default_message_key) if self.class.default_message_key @iterator = iterator_class.new(@attrs, @scope).freeze end
def iterator_class
The AttributesIterator subclass used to walk this validator's
def iterator_class SingleAttributeIterator end
def message(default_key = nil)
@exception_message = message(:presence) # symbol key or custom message
@example
useful for validators that build a message Hash for deferred i18n interpolation.
If both are nil, the block (if given) is called to compute a fallback —
Prefers an explicit +:message+ option, then +default_key+.
Returns the effective message for a validation error.
def message(default_key = nil) key = options_key?(:message) ? options[:message] : default_key return key unless key.nil? yield if block_given? end
def new(...)
def new(...) super.freeze end
def option_value
def option_value options_key?(:value) ? options[:value] : options end
def options_key?(key, given_options = nil)
def options_key?(key, given_options = nil) current_options = given_options || options hash_like?(current_options) && current_options.key?(key) && !current_options[key].nil? end
def scrub(value)
def scrub(value) return value if !value.respond_to?(:valid_encoding?) || value.valid_encoding? value.scrub 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_param! for per-parameter validation,
def validate!(params) # we collect errors inside array because # there may be more than one error per field array_errors = nil @iterator.each(params) 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? || (hash_like?(val) && val.key?(attr_name)) rescue Grape::Exceptions::Validation => e (array_errors ||= []) << e end raise Grape::Exceptions::ValidationArrayErrors.new(array_errors) if array_errors end
def validate_param!(attr_name, params)
-
(void)-
Raises:
-
(Grape::Exceptions::Validation)- if validation failed
Parameters:
-
params(Hash) -- the parameter hash containing the attribute -
attr_name(Symbol, String) -- the attribute name
def validate_param!(attr_name, params) raise NotImplementedError end
def validation_error!(attr_name_or_params, message = exception_message)
def validation_error!(attr_name_or_params, message = exception_message) params = attr_name_or_params.is_a?(Array) ? attr_name_or_params : scope.full_name(attr_name_or_params) raise Grape::Exceptions::Validation.new(params:, message:) end