Class: Dragnet::Validators::DataValidator

Inherits:
Validator
  • Object
show all
Defined in:
lib/dragnet/validators/data_validator.rb

Overview

Validates the data (key-value pairs) inside an MTR file. Verifies the structure, the required keys and their types.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, source_file) ⇒ DataValidator

Creates a new instance of the class

Parameters:

  • data (Hash)

    The data inside the YAML (after parsing)

  • source_file (Pathname)

    The path to the file from which the MTR data was loaded.



20
21
22
23
# File 'lib/dragnet/validators/data_validator.rb', line 20

def initialize(data, source_file)
  @data = data
  @source_file = source_file
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



14
15
16
# File 'lib/dragnet/validators/data_validator.rb', line 14

def data
  @data
end

#source_fileObject (readonly)

Returns the value of attribute source_file.



14
15
16
# File 'lib/dragnet/validators/data_validator.rb', line 14

def source_file
  @source_file
end

Instance Method Details

#validateDragnet::TestRecord

Validates the given data

Returns:

  • (Dragnet::TestRecord)

    A TestRecord object created from the given data (if the data was valid).

Raises:



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/dragnet/validators/data_validator.rb', line 31

def validate
  yaml_format_error("Incompatible data structure. Expecting a Hash, got a #{data.class}") unless data.is_a?(Hash)
  data.deep_symbolize_keys!

  # A call to chomp for strings is needed because the following YAML
  # syntax:
  #
  # findings: >
  #      no findings
  #
  # causes the string values to end with a newline ("\n"):
  data.transform_values! { |value| value.is_a?(String) ? value.chomp : value }
  test_record = create_mtr(data)
  validate_mtr(test_record)
end