Class: Grape::Validations::Base
- Inherits:
-
Object
- Object
- Grape::Validations::Base
- Defined in:
- lib/grape/validations/validators/base.rb
Direct Known Subclasses
AllowBlankValidator, CoerceValidator, DefaultValidator, MultipleParamsBase, PresenceValidator, RegexpValidator, ValuesValidator
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) ⇒ Base
constructor
Creates a new Validator from options specified by a
requires
oroptional
directive during parameter definition. - #validate!(params) ⇒ Object
Constructor Details
#initialize(attrs, options, required, scope) ⇒ Base
Creates a new Validator from options specified by a requires
or optional
directive during parameter definition.
13 14 15 16 17 18 |
# File 'lib/grape/validations/validators/base.rb', line 13 def initialize(attrs, , required, scope) @attrs = Array(attrs) @option = @required = required @scope = scope 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
29 30 31 32 33 34 35 36 |
# File 'lib/grape/validations/validators/base.rb', line 29 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
38 39 40 41 |
# File 'lib/grape/validations/validators/base.rb', line 38 def self.inherited(klass) short_name = convert_to_short_name(klass) Validations.register_validator(short_name, klass) end |
Instance Method Details
#validate!(params) ⇒ Object
20 21 22 23 24 25 26 27 |
# File 'lib/grape/validations/validators/base.rb', line 20 def validate!(params) attributes = AttributesIterator.new(self, @scope, params) attributes.each do |resource_params, attr_name| if @required || (resource_params.respond_to?(:key?) && resource_params.key?(attr_name)) validate_param!(attr_name, resource_params) end end end |