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
- #i18n_message(type, attribute) ⇒ Object
-
#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.
15 16 17 18 19 20 21 22 23 |
# File 'lib/grape/validations.rb', line 15 def initialize(attrs, , required, scope) @attrs = Array(attrs) @required = required @scope = scope if .is_a?(Hash) && !.empty? raise "unknown options: #{.keys}" end end |
Instance Attribute Details
#attrs ⇒ Object (readonly)
Returns the value of attribute attrs.
13 14 15 |
# File 'lib/grape/validations.rb', line 13 def attrs @attrs end |
Class Method Details
.inherited(klass) ⇒ Object
65 66 67 68 |
# File 'lib/grape/validations.rb', line 65 def self.inherited(klass) short_name = convert_to_short_name(klass) Validations::register_validator(short_name, klass) end |
Instance Method Details
#i18n_message(type, attribute) ⇒ Object
35 36 37 38 |
# File 'lib/grape/validations.rb', line 35 def (type, attribute) i18n_attr = I18n.t("grape.errors.attributes.#{attribute}", :default => attribute.to_s) I18n.t("grape.errors.messages.#{type}", :attribute => i18n_attr) end |
#validate!(params) ⇒ Object
25 26 27 28 29 30 31 32 33 |
# File 'lib/grape/validations.rb', line 25 def validate!(params) params = @scope.params(params) @attrs.each do |attr_name| if @required || params.has_key?(attr_name) validate_param!(attr_name, params) end end end |