Class: Grape::Validations::Base
- Inherits:
-
Object
- Object
- Grape::Validations::Base
- Defined in:
- lib/grape/validations/validators/base.rb
Direct Known Subclasses
AllowBlankValidator, AsValidator, CoerceValidator, DefaultValidator, ExceptValuesValidator, 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.
14 15 16 17 18 19 20 |
# File 'lib/grape/validations/validators/base.rb', line 14 def initialize(attrs, , required, scope, opts = {}) @attrs = Array(attrs) @option = @required = required @scope = scope @fail_fast = opts[:fail_fast] || false end |
Instance Attribute Details
#attrs ⇒ Object (readonly)
Returns the value of attribute attrs.
4 5 6 |
# File 'lib/grape/validations/validators/base.rb', line 4 def attrs @attrs end |
Class Method Details
.convert_to_short_name(klass) ⇒ Object
58 59 60 61 62 63 64 65 |
# File 'lib/grape/validations/validators/base.rb', line 58 def self.convert_to_short_name(klass) ret = klass.name.gsub(/::/, '/') ret.gsub!(/([A-Z]+)([A-Z][a-z])/, '\1_\2') ret.gsub!(/([a-z\d])([A-Z])/, '\1_\2') ret.tr!('-', '_') ret.downcase! File.basename(ret, '_validator') end |
.inherited(klass) ⇒ Object
67 68 69 70 |
# File 'lib/grape/validations/validators/base.rb', line 67 def self.inherited(klass) return unless klass.name.present? Validations.register_validator(convert_to_short_name(klass), klass) end |
Instance Method Details
#fail_fast? ⇒ Boolean
82 83 84 |
# File 'lib/grape/validations/validators/base.rb', line 82 def fail_fast? @fail_fast end |
#message(default_key = nil) ⇒ Object
72 73 74 75 |
# File 'lib/grape/validations/validators/base.rb', line 72 def (default_key = nil) = instance_variable_get(:@option) (:message) ? [:message] : default_key end |
#options_key?(key, options = nil) ⇒ Boolean
77 78 79 80 |
# File 'lib/grape/validations/validators/base.rb', line 77 def (key, = nil) = instance_variable_get(:@option) if .nil? .respond_to?(:key?) && .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.
27 28 29 30 |
# File 'lib/grape/validations/validators/base.rb', line 27 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.
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/grape/validations/validators/base.rb', line 37 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 |resource_params, attr_name| next if !@scope.required? && resource_params.empty? next unless @scope.meets_dependency?(resource_params, params) begin if @required || resource_params.respond_to?(:key?) && resource_params.key?(attr_name) validate_param!(attr_name, resource_params) end rescue Grape::Exceptions::Validation => e array_errors << e end end raise Grape::Exceptions::ValidationArrayErrors, array_errors if array_errors.any? end |