Class: UuidValidator

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

Constant Summary collapse

CASE_SENSITIVE =
/\A#{PATTERN}\z/.freeze
CASE_INSENSITIVE =
/\A#{PATTERN}\z/i.freeze

Instance Method Summary collapse

Instance Method Details

#validate_each(model, attribute, value) ⇒ Object



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

def validate_each(model, attribute, value)
  case_sensitive = options.fetch(:case_sensitive, true)

  return model.errors.add(attribute, :blank, **options) if value.blank?

  regexp = case_sensitive ? CASE_SENSITIVE : CASE_INSENSITIVE

  return if value.is_a?(String) && value.match?(regexp)

  model.errors.add(attribute, :invalid, **options)
end