Class: Axn::Validators::ValidateValidator

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.apply_syntactic_sugar(value, _fields) ⇒ Object



8
9
10
11
12
# File 'lib/axn/core/validation/validators/validate_validator.rb', line 8

def self.apply_syntactic_sugar(value, _fields)
  return value if value.is_a?(Hash)

  { with: value }
end

Instance Method Details

#check_validity!Object

Raises:

  • (ArgumentError)


14
15
16
# File 'lib/axn/core/validation/validators/validate_validator.rb', line 14

def check_validity!
  raise ArgumentError, "must supply :with" if options[:with].nil?
end

#validate_each(record, attribute, value) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/axn/core/validation/validators/validate_validator.rb', line 18

def validate_each(record, attribute, value)
  msg = begin
    options[:with].call(value)
  rescue StandardError => e
    Axn::Internal::PipingError.swallow("applying custom validation on field '#{attribute}'", exception: e)

    "failed validation: #{e.message}"
  end

  record.errors.add(attribute, msg) if msg.present?
end