Sha256: 0b6001740257bf074234809da00f6276f64d2961945d30afca6f160f1451537d

Contents?: true

Size: 1.32 KB

Versions: 3

Compression:

Stored size: 1.32 KB

Contents

# frozen_string_literal: true

require_relative 'field_validator'

module Dragnet
  module Validators
    module Fields
      # Validates the SHA1 field of a Manual Test Record
      class SHA1Validator < Dragnet::Validators::Fields::FieldValidator
        SHA1_MIN_LENGTH = 7
        SHA1_MAX_LENGTH = 40
        SHA1_REGEX = /\A[0-9a-f]+\Z/.freeze

        # Validates the SHA1 of the MTR
        # @param [String] key The name of the key
        # @param [Object] value The value of the key
        # @raise [Dragnet::Errors::ValidationError] If the SHA1 is missing, is not
        #   and string, is too short or too long or is not a valid hexadecimal
        #   string.
        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
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
dragnet-5.3.1 lib/dragnet/validators/fields/sha1_validator.rb
dragnet-5.3.0 lib/dragnet/validators/fields/sha1_validator.rb
dragnet-5.2.1 lib/dragnet/validators/fields/sha1_validator.rb