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
AtLeastOneOfValidator, DefaultValidator, MutualExclusionValidator, PresenceValidator, SingleOptionValidator, ValuesValidator
Defined Under Namespace
Classes: AttributesIterator
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.
9 10 11 12 13 |
# File 'lib/grape/validations.rb', line 9 def initialize(attrs, , required, scope) @attrs = Array(attrs) @required = required @scope = scope end |
Instance Attribute Details
#attrs ⇒ Object (readonly)
Returns the value of attribute attrs.
7 8 9 |
# File 'lib/grape/validations.rb', line 7 def attrs @attrs end |
Class Method Details
.convert_to_short_name(klass) ⇒ Object
42 43 44 45 46 47 48 49 |
# File 'lib/grape/validations.rb', line 42 def self.convert_to_short_name(klass) ret = klass.name.gsub(/::/, '/') .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2') .gsub(/([a-z\d])([A-Z])/, '\1_\2') .tr("-", "_") .downcase File.basename(ret, '_validator') end |
.inherited(klass) ⇒ Object
64 65 66 67 |
# File 'lib/grape/validations.rb', line 64 def self.inherited(klass) short_name = convert_to_short_name(klass) Validations.register_validator(short_name, klass) end |
Instance Method Details
#validate!(params) ⇒ Object
15 16 17 18 19 20 21 22 |
# File 'lib/grape/validations.rb', line 15 def validate!(params) attributes = AttributesIterator.new(self, @scope, params) attributes.each do |resource_params, attr_name| if @required || resource_params.key?(attr_name) validate_param!(attr_name, resource_params) end end end |