Module: TurbotRunner::Validator

Extended by:
Validator
Included in:
Validator
Defined in:
lib/turbot_runner/validator.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.identifying_hash(record, identifying_fields) ⇒ Object



38
39
40
41
42
43
44
45
46
# File 'lib/turbot_runner/validator.rb', line 38

def identifying_hash(record, identifying_fields)
  flattened = TurbotRunner::Utils.flatten(record)
  flattened.each do |k, v|
    identifying_fields.each do |field|
      return nil if k.start_with?("#{field}.")
    end
  end
  flattened.slice(*identifying_fields)
end

.record_uid(identifying_hash) ⇒ Object



48
49
50
# File 'lib/turbot_runner/validator.rb', line 48

def record_uid(identifying_hash)
  Digest::SHA1.hexdigest(identifying_hash.to_query)
end

.validate(data_type, record, identifying_fields, seen_uids) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/turbot_runner/validator.rb', line 8

def validate(data_type, record, identifying_fields, seen_uids)
  schema_path = TurbotRunner.schema_path(data_type)
  error = Openc::JsonSchema.validate(schema_path, record)

  if error
    return error[:message]
  end

  identifying_hash = identifying_hash(record, identifying_fields)
  if identifying_hash.nil?
    return 'The value of an identifying field may not be a hash'
  end

  identifying_attributes = identifying_hash.reject {|k, v| v.nil? || v == ''}
  if identifying_attributes.empty?
    return "There were no values provided for any of the identifying fields: #{identifying_fields.join(', ')}"
  end

  if !seen_uids.nil?
    record_uid = record_uid(identifying_hash)
    if seen_uids.include?(record_uid)
      return "Already seen record with these identifying fields: #{identifying_hash}"
    else
      seen_uids.add(record_uid)
    end
  end

  nil
end

Instance Method Details

#identifying_hash(record, identifying_fields) ⇒ Object



38
39
40
41
42
43
44
45
46
# File 'lib/turbot_runner/validator.rb', line 38

def identifying_hash(record, identifying_fields)
  flattened = TurbotRunner::Utils.flatten(record)
  flattened.each do |k, v|
    identifying_fields.each do |field|
      return nil if k.start_with?("#{field}.")
    end
  end
  flattened.slice(*identifying_fields)
end

#record_uid(identifying_hash) ⇒ Object



48
49
50
# File 'lib/turbot_runner/validator.rb', line 48

def record_uid(identifying_hash)
  Digest::SHA1.hexdigest(identifying_hash.to_query)
end

#validate(data_type, record, identifying_fields, seen_uids) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/turbot_runner/validator.rb', line 8

def validate(data_type, record, identifying_fields, seen_uids)
  schema_path = TurbotRunner.schema_path(data_type)
  error = Openc::JsonSchema.validate(schema_path, record)

  if error
    return error[:message]
  end

  identifying_hash = identifying_hash(record, identifying_fields)
  if identifying_hash.nil?
    return 'The value of an identifying field may not be a hash'
  end

  identifying_attributes = identifying_hash.reject {|k, v| v.nil? || v == ''}
  if identifying_attributes.empty?
    return "There were no values provided for any of the identifying fields: #{identifying_fields.join(', ')}"
  end

  if !seen_uids.nil?
    record_uid = record_uid(identifying_hash)
    if seen_uids.include?(record_uid)
      return "Already seen record with these identifying fields: #{identifying_hash}"
    else
      seen_uids.add(record_uid)
    end
  end

  nil
end