Class: Grape::Validations::Validator
- Inherits:
-
Object
- Object
- Grape::Validations::Validator
- Defined in:
- lib/grape/validations.rb,
lib/grape/validations.rb
Overview
We define Validator::inherited here so SingleOptionValidator will not be considered a validator.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#attrs ⇒ Object
readonly
Returns the value of attribute attrs.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(attrs, options, required, scope) ⇒ Validator
constructor
A new instance of Validator.
- #validate!(params) ⇒ Object
Constructor Details
#initialize(attrs, options, required, scope) ⇒ Validator
Returns a new instance of Validator.
11 12 13 14 15 16 17 18 19 |
# File 'lib/grape/validations.rb', line 11 def initialize(attrs, , required, scope) @attrs = Array(attrs) @required = required @scope = scope if .is_a?(Hash) && !.empty? raise Grape::Exceptions.UnknownOptions.new(.keys) end end |
Instance Attribute Details
#attrs ⇒ Object (readonly)
Returns the value of attribute attrs.
9 10 11 |
# File 'lib/grape/validations.rb', line 9 def attrs @attrs end |
Class Method Details
.inherited(klass) ⇒ Object
58 59 60 61 |
# File 'lib/grape/validations.rb', line 58 def self.inherited(klass) short_name = convert_to_short_name(klass) Validations::register_validator(short_name, klass) end |
Instance Method Details
#validate!(params) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/grape/validations.rb', line 21 def validate!(params) params = @scope.params(params) (params.is_a?(Array) ? params : [params]).each do |resource_params| @attrs.each do |attr_name| if @required || resource_params.has_key?(attr_name) validate_param!(attr_name, resource_params) end end end end |