Class: Axn::Validators::ModelValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/axn/core/validation/validators/model_validator.rb

Class Method Summary collapse

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 |options|
    # Set default klass based on field name if not provided
    options[:klass] = nil if options[:klass] == true
    options[:klass] ||= fields.first.to_s.classify

    # Constantize string klass names
    options[:klass] = options[:klass].constantize if options[:klass].is_a?(String)

    # Set default finder if not provided
    options[:finder] ||= :find
  end
end

Instance Method Details

#check_validity!Object

Raises:

  • (ArgumentError)


23
24
25
26
27
# File 'lib/axn/core/validation/validators/model_validator.rb', line 23

def check_validity!
  return unless options[: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], **options)
  type_validator.validate_each(record, attribute, value)
end