Sha256: 8c9cb070770beef4a36ec5eb2a77da283fe1070bb3d0fbbbb48f4d2c0a198492

Contents?: true

Size: 794 Bytes

Versions: 2

Compression:

Stored size: 794 Bytes

Contents

class UuidValidator < ActiveModel::EachValidator

  def validate_each(record, attribute, value)
    unless valid?(value, options)
      record.errors[attribute] << (options[:message] || I18n.t('active_validation.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
active_validation-1.1.0 lib/active_validation/validators/uuid_validator.rb
active_validation-1.0.0 lib/active_validation/validators/uuid_validator.rb