Sha256: a1273d39eca168f4be4939b5b1f1387164e961403729c09b35c1c7976683a4d4

Contents?: true

Size: 662 Bytes

Versions: 3

Compression:

Stored size: 662 Bytes

Contents

# frozen_string_literal: true

class UuidValidator < ActiveModel::EachValidator
  PATTERN = "[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}"
  CASE_SENSITIVE = /\A#{PATTERN}\z/.freeze
  CASE_INSENSITIVE = /\A#{PATTERN}\z/i.freeze

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

    regexp = case_sensitive ? CASE_SENSITIVE : CASE_INSENSITIVE

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

    message = options[:message] || "is not a valid UUID (case #{case_sensitive ? "sensitive" : "insensitive"})"

    obj.errors.add(attribute, message)
  end

  private_constant :PATTERN
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
solid-process-0.3.0 lib/solid/validators/uuid_validator.rb
solid-process-0.2.0 lib/solid/validators/uuid_validator.rb
solid-process-0.1.0 lib/solid/validators/uuid_validator.rb