Class: Grape::Validations::Validators::Base
- Inherits:
-
Object
- Object
- Grape::Validations::Validators::Base
- Includes:
- Util::Translation
- Defined in:
- lib/grape/validations/validators/base.rb
Overview
Base class for all parameter validators.
Freeze contract
Validator instances are shared across requests and are frozen after initialization (via .new). All inputs (options, opts, attrs) arrive pre-frozen from the DSL boundary, so subclass ivars derived from them are frozen by construction. Lazy ivar assignment (e.g. memoize, ||=) will raise FrozenError at request time.
Direct Known Subclasses
AllowBlankValidator, AsValidator, CoerceValidator, DefaultValidator, ExceptValuesValidator, LengthValidator, MultipleParamsBase, PresenceValidator, RegexpValidator, SameAsValidator, ValuesValidator
Instance Attribute Summary collapse
-
#attrs ⇒ Object
readonly
Returns the value of attribute attrs.
Class Method Summary collapse
-
.default_message_key(key = nil) ⇒ Object
Declares the default I18n message key used by
validation_error!. - .inherited(klass) ⇒ Object
- .new ⇒ Object
Instance Method Summary collapse
- #fail_fast? ⇒ Boolean
-
#initialize(attrs, options, required, scope, opts) ⇒ Base
constructor
Creates a new Validator from options specified by a
requiresoroptionaldirective during parameter definition. -
#validate(request) ⇒ void
Validates a given request.
-
#validate!(params) ⇒ void
Validates a given parameter hash.
Constructor Details
#initialize(attrs, options, required, scope, opts) ⇒ Base
Creates a new Validator from options specified by a requires or optional directive during parameter definition.
56 57 58 59 60 61 62 63 64 |
# File 'lib/grape/validations/validators/base.rb', line 56 def initialize(attrs, , required, scope, opts) @attrs = Array(attrs).freeze @options = Grape::Util::DeepFreeze.deep_freeze() @option = @options # TODO: remove in next major release @required = required @scope = scope @fail_fast, @allow_blank = opts.values_at(:fail_fast, :allow_blank) @exception_message = (self.class.) if self.class. end |
Instance Attribute Details
#attrs ⇒ Object (readonly)
Returns the value of attribute attrs.
17 18 19 |
# File 'lib/grape/validations/validators/base.rb', line 17 def attrs @attrs end |
Class Method Details
.default_message_key(key = nil) ⇒ Object
Declares the default I18n message key used by validation_error!. Subclasses that only need a single fixed error message can declare it at the class level instead of overriding initialize:
class MyValidator < Grape::Validations::Validators::Base
:my_error
end
The key is resolved through message, so a per-option :message override still takes precedence.
30 31 32 33 34 35 36 |
# File 'lib/grape/validations/validators/base.rb', line 30 def (key = nil) if key @default_message_key = key else @default_message_key || (superclass.respond_to?(:default_message_key) ? superclass. : nil) end end |
.inherited(klass) ⇒ Object
42 43 44 45 |
# File 'lib/grape/validations/validators/base.rb', line 42 def inherited(klass) super Validations.register(klass) end |
.new ⇒ Object
38 39 40 |
# File 'lib/grape/validations/validators/base.rb', line 38 def new(...) super.freeze end |
Instance Method Details
#fail_fast? ⇒ Boolean
77 78 79 |
# File 'lib/grape/validations/validators/base.rb', line 77 def fail_fast? @fail_fast end |
#validate(request) ⇒ void
Override #validate! unless you need to access the entire request.
This method returns an undefined value.
Validates a given request.
71 72 73 74 75 |
# File 'lib/grape/validations/validators/base.rb', line 71 def validate(request) return unless @scope.should_validate?(request.params) validate!(request.params) end |
#validate!(params) ⇒ void
Override #validate_param! for per-parameter validation, or #validate if you need access to the entire request.
This method returns an undefined value.
Validates a given parameter hash.
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/grape/validations/validators/base.rb', line 87 def validate!(params) attributes = SingleAttributeIterator.new(@attrs, @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 || (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.any? end |