78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
# File 'lib/openc/json_schema/validator.rb', line 78
def convert_error(error)
path = fragment_to_path(error[:fragment])
= {}
case error[:failed_attribute]
when 'Required'
match = error[:message].match(/required property of '(.*)'/)
missing_property = match[1]
path = fragment_to_path("#{error[:fragment]}/#{missing_property}")
type = :missing
message = "Missing required property: #{path}"
when 'AdditionalProperties'
match = error[:message].match(/contains additional properties \["(.*)"\] outside of the schema/)
additional_property = match[1].split('", "')[0]
path = fragment_to_path("#{error[:fragment]}/#{additional_property}")
type = :additional
message = "Disallowed additional property: #{path}"
when 'OneOf'
if error[:message].match(/did not match any/)
type = :one_of_no_matches
message = "No match for property: #{path}"
else
type = :one_of_many_matches
message = "Multiple possible matches for property: #{path}"
end
when 'AnyOf'
type = :any_of_no_matches
message = "No match for property: #{path}"
when 'MinLength'
match = error[:message].match(/minimum string length of (\d+) in/)
min_length = match[1].to_i
type = :too_short
message = "Property too short: #{path} (must be at least #{min_length} characters)"
= {:length => min_length}
when 'MaxLength'
match = error[:message].match(/maximum string length of (\d+) in/)
max_length = match[1].to_i
type = :too_long
message = "Property too long: #{path} (must be at most #{max_length} characters)"
= {:length => max_length}
when 'TypeV4'
match = error[:message].match(/the following types?: ([\w\s,]+) in schema/)
allowed_types = match[1].split(',').map(&:strip)
type = :type_mismatch
message = "Property of wrong type: #{path} (must be of type #{allowed_types.join(', ')})"
= {:allowed_types => allowed_types}
when 'Enum'
match = error[:message].match(/the following values: ([\w\s,]+) in schema/)
allowed_values = match[1].split(',').map(&:strip)
type = :enum_mismatch
if allowed_values.size == 1
message = "Property must have value #{allowed_values[0]}: #{path}"
else
message = "Property not an allowed value: #{path} (must be one of #{allowed_values.join(', ')})"
end
= {:allowed_values => allowed_values}
else
if error[:message].match(/must be of format yyyy-mm-dd/)
type = :format_mismatch
message = "Property not of expected format: #{path} (must be of format yyyy-mm-dd)"
= {:expected_format => 'yyyy-mm-dd'}
else
type = :unknown
message = "Error of unknown type: #{path} (#{error[:message]})"
end
end
{:type => type, :path => path, :message => message}.merge()
end
|