Class: Circuitdata::JsonValidator::JsonSchemaErrorParser

Inherits:
Object
  • Object
show all
Defined in:
lib/circuitdata/json_validator/json_schema_error_parser.rb

Class Method Summary collapse

Class Method Details

.extract_data(message, failed_attribute) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/circuitdata/json_validator/json_schema_error_parser.rb', line 25

def extract_data(message, failed_attribute)
  case failed_attribute
  when "Required"
    field = message.match(/of '(.*)'/)[1]
    if !field
      fail "Unable to extract field from #{message.inspect}"
    end
    return {field: field, problem: "required_property_missing"}
  when "TypeV4"
    if message.include?("did not match the following type")
      matches = message.match(/of type (\S*) did not match the following type: (\S*)/)
      actual, expected = matches[1..2]
      return {actual: actual, expected: expected, problem: "type_mismatch"}
    end
  when "AdditionalProperties"
    matches = message.match(/contains additional properties (\[.*\]) outside/)
    additional_properties = JSON.parse(matches[1])
    return {additional_properties: additional_properties, problem: "additional_properties"}
  when "Enum"
    return {problem: "not_in_enum"}
  when "Pattern"
    regex = message.match(/did not match the regex '(\S*)' /)[1]
    return {problem: "pattern_mismatch", pattern: regex}
  else
    if message.match?(/is not a uuid/)
      return {problem: "format_mismatch", expected: "uuid"}
    end
  end
end

.translate(error) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/circuitdata/json_validator/json_schema_error_parser.rb', line 12

def translate(error)
  additional_data = extract_data(error[:message], error[:failed_attribute])
  if additional_data.nil?
    fail "Unhandled error: #{error.inspect}"
  end

  path = error[:fragment].gsub("#", "")
  {
    source_path: path,
    field: path.split("/").last,
  }.merge(additional_data)
end

.translate_all(errors) ⇒ Object



5
6
7
8
9
10
# File 'lib/circuitdata/json_validator/json_schema_error_parser.rb', line 5

def translate_all(errors)
  errors.map(&method(:translate)).reject do |error|
    error[:problem] == "pattern_mismatch" &&
      error[:pattern] == "^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$"
  end
end