Class: Axn::Validators::ModelValidator
- Inherits:
-
ActiveModel::EachValidator
- Object
- ActiveModel::EachValidator
- Axn::Validators::ModelValidator
- Defined in:
- lib/axn/core/validation/validators/model_validator.rb
Class Method Summary collapse
-
.apply_syntactic_sugar(value, fields) ⇒ Object
Syntactic sugar: model: User -> model: { klass: User }.
Instance Method Summary collapse
Class Method Details
.apply_syntactic_sugar(value, fields) ⇒ Object
Syntactic sugar: model: User -> model: { klass: User }
9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/axn/core/validation/validators/model_validator.rb', line 9 def self.apply_syntactic_sugar(value, fields) (value.is_a?(Hash) ? value.dup : { klass: value }).tap do || # Set default klass based on field name if not provided [:klass] = nil if [:klass] == true [:klass] ||= fields.first.to_s.classify # Constantize string klass names [:klass] = [:klass].constantize if [:klass].is_a?(String) # Set default finder if not provided [:finder] ||= :find end end |
Instance Method Details
#check_validity! ⇒ Object
23 24 25 26 27 |
# File 'lib/axn/core/validation/validators/model_validator.rb', line 23 def check_validity! return unless [:klass].nil? raise ArgumentError, "must supply :klass" end |
#validate_each(record, attribute, value) ⇒ Object
29 30 31 32 33 |
# File 'lib/axn/core/validation/validators/model_validator.rb', line 29 def validate_each(record, attribute, value) # The value is already resolved by the facade, just validate the type type_validator = TypeValidator.new(attributes: [attribute], **) type_validator.validate_each(record, attribute, value) end |