Skip to content

Commit

Permalink
Merge pull request #495 from anupama-kumari/ak_all_of_error_message
Browse files Browse the repository at this point in the history
Fix issue #474: handling all of error message
  • Loading branch information
ekohl committed Jul 6, 2023
2 parents 1b801a3 + 8c8f999 commit 5dbb4df
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 4 deletions.
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" }
}
}

0 comments on commit 5dbb4df

Please sign in to comment.