Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handling allOf error message #495

Merged
merged 1 commit into from
Jul 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions lib/json-schema/attributes/allof.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ def self.validate(current_schema, data, fragments, processor, validator, options
# Create an hash to hold errors that are generated during validation
errors = Hash.new { |hsh, k| hsh[k] = [] }
valid = true
message = nil

current_schema.schema['allOf'].each_with_index do |element, schema_index|
schema = JSON::Schema.new(element, current_schema.uri, validator)
Expand All @@ -17,8 +18,9 @@ def self.validate(current_schema, data, fragments, processor, validator, options

begin
schema.validate(data, fragments, processor, options)
rescue ValidationError
rescue ValidationError => e
valid = false
message = e.message
end

diff = validation_errors(processor).count - pre_validation_error_count
Expand All @@ -29,7 +31,7 @@ def self.validate(current_schema, data, fragments, processor, validator, options
end

if !valid || !errors.empty?
message = "The property '#{build_fragment(fragments)}' of type #{type_of_data(data)} did not match all of the required schemas"
message ||= "The property '#{build_fragment(fragments)}' of type #{type_of_data(data)} did not match all of the required schemas"
validation_error(processor, message, fragments, current_schema, self, options[:record_errors])
validation_errors(processor).last.sub_errors = errors
end
Expand Down
17 changes: 17 additions & 0 deletions test/all_of_ref_schema_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,21 @@ def test_all_of_ref_message
- The property '#/name' of type string did not match the following type: integer"''
assert_equal(expected_message, errors[0])
end

def test_all_of_ref_message_with_one_attribute_wrong
errors = JSON::Validator.fully_validate(schema, data)
expected_message = ''"The property '#/' of type object did not match all of the required schemas. The schema specific errors were:

- allOf #0:
- The property '#/name' of type string did not match the following type: integer"''
assert_equal(expected_message, errors[0])
end

def test_all_of_ref_validate_messgae
exception = assert_raises JSON::Schema::ValidationError do
JSON::Validator.validate!(schema, data)
end
expected_error_message = "The property '#/name' of type string did not match the following type: integer"
assert_equal expected_error_message, exception.message
end
end
3 changes: 2 additions & 1 deletion test/data/all_of_ref_data.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"name" : "john"
"name" : "john",
"id" : "1"
}
3 changes: 2 additions & 1 deletion test/schemas/all_of_ref_base_schema.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"type": "object",
"properties" : {
"name" : { "type": "integer" }
"name" : { "type": "integer" },
"id" : { "type": "string" }
}
}