Class: IsValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/solid/validators/is_validator.rb

Instance Method Summary collapse

Instance Method Details

#validate_each(model, attribute, value) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/solid/validators/is_validator.rb', line 6

def validate_each(model, attribute, value)
  with_option = Array.wrap(options[:with] || options[:in])

  return if with_option.all? do |predicate|
    raise ArgumentError, "expected a predicate method, got #{predicate.inspect}" unless predicate.end_with?("?")

    value.try(predicate)
  end

  message = "does not satisfy the predicate#{"s" if with_option.size > 1}: #{with_option.join(" & ")}"

  Solid::Validators.add_error(model, attribute, message, options)
end