Class: DPN::Bagit::UUID4Validator

Inherits:
Object
  • Object
show all
Defined in:
lib/dpn/bagit/uuidv4_validator.rb

Overview

A class that validates whether a string is a v4 UUID or not.

Instance Method Summary collapse

Constructor Details

#initialize(dashes = nil) ⇒ UUID4Validator

Create a new validator.

Parameters:

  • dashes (Boolean, NilClass) (defaults to: nil)

    If set to true, dashes are required. If set to false, dashes are disallowed. If set to nil, dashes are optional.


9
10
11
12
13
14
15
16
17
# File 'lib/dpn/bagit/uuidv4_validator.rb', line 9

def initialize(dashes = nil)
  if dashes == nil
    @pattern = /^[A-Fa-f0-9]{8}-?[A-Fa-f0-9]{4}-?[A-Fa-f0-9]{4}-?[A-Fa-f0-9]{4}-?[A-Fa-f0-9]{12}\Z/
  elsif dashes == true
    @pattern = /^[A-Fa-f0-9]{8}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{12}\Z/
  else
    @pattern = /^[A-Fa-f0-9]{8}[A-Fa-f0-9]{4}[A-Fa-f0-9]{4}[A-Fa-f0-9]{4}[A-Fa-f0-9]{12}\Z/
  end
end

Instance Method Details

#isValid?(uuid) ⇒ Boolean

Check if the given string is valid according to this validator. This test is case-insensitive.

Parameters:

  • uuid (String)

    The uuid to test.

Returns:

  • (Boolean)

23
24
25
26
27
28
29
30
# File 'lib/dpn/bagit/uuidv4_validator.rb', line 23

def isValid?(uuid)
  if @pattern.match(uuid)
    #matches a v4 uuid, case-insensitive, with or without dashes.
    return true
  else
    return false
  end
end