Class: Dragnet::Validators::Fields::SHA1Validator

Inherits:
FieldValidator show all
Defined in:
lib/dragnet/validators/fields/sha1_validator.rb

Overview

Validates the SHA1 field of a Manual Test Record

Constant Summary collapse

SHA1_MIN_LENGTH =
7
SHA1_MAX_LENGTH =
40
SHA1_REGEX =
/\A[0-9a-f]+\Z/.freeze

Instance Method Summary collapse

Instance Method Details

#validate(key, value) ⇒ Object

Validates the SHA1 of the MTR

Parameters:

  • key (String)

    The name of the key

  • value (Object)

    The value of the key

Raises:



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/dragnet/validators/fields/sha1_validator.rb', line 20

def validate(key, value)
  validate_presence(key, value)
  validate_type(key, value, String)

  length = value.length
  unless length >= SHA1_MIN_LENGTH && length <= SHA1_MAX_LENGTH
    validation_error(
      "Invalid value for key #{key}: '#{value}'. Expected a string between "\
      "#{SHA1_MIN_LENGTH} and #{SHA1_MAX_LENGTH} characters"
    )
  end

  return if value.match(SHA1_REGEX)

  validation_error(
    "Invalid value for key #{key}: '#{value}'. "\
    "Doesn't seem to be a valid hexadecimal string"
  )
end