Sha256: ba2dadf01747fa8086c2eb149a414b88a7d54e05edff4e0ab2d7a25c858d1abb

Contents?: true

Size: 776 Bytes

Versions: 2

Compression:

Stored size: 776 Bytes

Contents

class UuidValidator < ActiveModel::EachValidator

  def validate_each(record, attribute, value)
    unless valid?(value, options)
      record.errors[attribute] << (options[:message] || I18n.t('errors.messages.uuid'))
    end
  end

  private

  def valid_format?(value, options)
    case options[:version]
    when 3
      value =~ /^[0-9A-F]{8}-[0-9A-F]{4}-3[0-9A-F]{3}-[0-9A-F]{4}-[0-9A-F]{12}$/i
    when 4
      value =~ /^[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i
    when 5
      value =~ /^[0-9A-F]{8}-[0-9A-F]{4}-5[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i
    else
      value =~ /^[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}$/i
    end
  end

  def valid?(value, options)
    valid_format?(value, options)
  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
flash_validators-3.0.1 lib/flash_validators/validators/uuid_validator.rb
flash_validators-3.0.0 lib/flash_validators/validators/uuid_validator.rb