Class: WithUid::UidValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/with_uid/uid_validator.rb

Overview

Validates uid

Examples:

required uid

validates :id, 'with_uid/uid' => true

optional uid

validates :id, 'with_uid/uid' => { }allow_blank: true }

Constant Summary collapse

REGEX =
/\A[0-9a-z\-_\.]+\z/i

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



14
15
16
17
18
19
# File 'lib/with_uid/uid_validator.rb', line 14

def validate_each(record, attribute, value)
  return if options.fetch(:allow_blank, false) && value.blank?
  return if value =~ REGEX

  record.errors[attribute] << options.fetch(:message, 'invalid format')
end