Class: Grape::Validations::Validators::Base
- Inherits:
-
Object
- Object
- Grape::Validations::Validators::Base
- Defined in:
- lib/grape/validations/validators/base.rb
Direct Known Subclasses
AllowBlankValidator, AsValidator, CoerceValidator, ContractScopeValidator, 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
Instance Method Summary collapse
- #fail_fast? ⇒ Boolean
-
#initialize(attrs, options, required, scope, opts) ⇒ Base
constructor
Creates a new Validator from options specified by a
requires
oroptional
directive during parameter definition. - #message(default_key = nil) ⇒ Object
- #options_key?(key, options = nil) ⇒ Boolean
-
#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.
17 18 19 20 21 22 23 24 |
# File 'lib/grape/validations/validators/base.rb', line 17 def initialize(attrs, , required, scope, opts) @attrs = Array(attrs) @option = @required = required @scope = scope @fail_fast = opts[:fail_fast] @allow_blank = opts[:allow_blank] end |
Instance Attribute Details
#attrs ⇒ Object (readonly)
Returns the value of attribute attrs.
7 8 9 |
# File 'lib/grape/validations/validators/base.rb', line 7 def attrs @attrs end |
Class Method Details
.inherited(klass) ⇒ Object
60 61 62 63 |
# File 'lib/grape/validations/validators/base.rb', line 60 def self.inherited(klass) super Validations.register(klass) end |
Instance Method Details
#fail_fast? ⇒ Boolean
75 76 77 |
# File 'lib/grape/validations/validators/base.rb', line 75 def fail_fast? @fail_fast end |
#message(default_key = nil) ⇒ Object
65 66 67 68 |
# File 'lib/grape/validations/validators/base.rb', line 65 def (default_key = nil) = instance_variable_get(:@option) (:message) ? [:message] : default_key end |
#options_key?(key, options = nil) ⇒ Boolean
70 71 72 73 |
# File 'lib/grape/validations/validators/base.rb', line 70 def (key, = nil) = instance_variable_get(:@option) if .nil? .try(:key?, key) && ![key].nil? end |
#validate(request) ⇒ void
Override #validate! unless you need to access the entire request.
This method returns an undefined value.
Validates a given request.
31 32 33 34 35 |
# File 'lib/grape/validations/validators/base.rb', line 31 def validate(request) return unless @scope.should_validate?(request.params) validate!(request.params) end |
#validate!(params) ⇒ void
Override #validate if you need to access the entire request.
This method returns an undefined value.
Validates a given parameter hash.
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/grape/validations/validators/base.rb', line 42 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.try(:key?, attr_name) rescue Grape::Exceptions::Validation => e array_errors << e end raise Grape::Exceptions::ValidationArrayErrors.new(array_errors) if array_errors.any? end |