Skip to content

Commit

Permalink
Made valiation errors refer to json schema types not ruby types
Browse files Browse the repository at this point in the history
  • Loading branch information
marshall-lee authored and iainbeeston committed Feb 7, 2017
1 parent 7f42797 commit 853d0dc
Show file tree
Hide file tree
Showing 13 changed files with 27 additions and 27 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
- Deprecated `JSON::Validator#validator_for` in favor of `JSON::Validator#validator_for_uri`
- Deprecated `JSON::Validator.validate2` in favor of `JSON::Validator.validate!`
- Deprecated `JSON::Schema::Validator#extend_schema_definition` in favour of subclassing
- Made validation errors refer to json schema types not ruby types

## [2.7.0] - 2016-09-29

Expand Down
10 changes: 10 additions & 0 deletions lib/json-schema/attribute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ def self.data_valid_for_type?(data, type)
valid_classes = TYPE_CLASS_MAPPINGS.fetch(type) { return true }
Array(valid_classes).any? { |c| data.is_a?(c) }
end

# Lookup Schema type of given class instance
def self.type_of_data(data)
type, _ = TYPE_CLASS_MAPPINGS.map { |k,v| [k,v] }.sort_by { |(_, v)|
-Array(v).map { |klass| klass.ancestors.size }.max
}.find { |(_, v)|
Array(v).any? { |klass| data.kind_of?(klass) }
}
type
end
end
end
end
2 changes: 1 addition & 1 deletion lib/json-schema/attributes/allof.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def self.validate(current_schema, data, fragments, processor, validator, options
end

if !valid || !errors.empty?
message = "The property '#{build_fragment(fragments)}' of type #{data.class} 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
2 changes: 1 addition & 1 deletion lib/json-schema/attributes/anyof.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def self.validate(current_schema, data, fragments, processor, validator, options
end

if !valid
message = "The property '#{build_fragment(fragments)}' of type #{data.class} did not match one or more of the required schemas"
message = "The property '#{build_fragment(fragments)}' of type #{type_of_data(data)} did not match one or more 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
2 changes: 1 addition & 1 deletion lib/json-schema/attributes/not.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def self.validate(current_schema, data, fragments, processor, validator, options
if options[:record_errors] && errors_copy.length != processor.validation_errors.length
processor.validation_errors.replace(errors_copy)
else
message = "The property '#{build_fragment(fragments)}' of type #{data.class} matched the disallowed schema"
message = "The property '#{build_fragment(fragments)}' of type #{type_of_data(data)} matched the disallowed schema"
failed = false
end
rescue ValidationError
Expand Down
4 changes: 2 additions & 2 deletions lib/json-schema/attributes/oneof.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ def self.validate(current_schema, data, fragments, processor, validator, options
end

if validation_error_count == one_of.length
message = "The property '#{build_fragment(fragments)}' of type #{data.class} did not match any of the required schemas"
message = "The property '#{build_fragment(fragments)}' of type #{type_of_data(data)} did not match any of the required schemas"
else
message = "The property '#{build_fragment(fragments)}' of type #{data.class} matched more than one of the required schemas"
message = "The property '#{build_fragment(fragments)}' of type #{type_of_data(data)} matched more than one of the required schemas"
end

validation_error(processor, message, fragments, current_schema, self, options[:record_errors]) if message
Expand Down
10 changes: 0 additions & 10 deletions lib/json-schema/attributes/type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,6 @@ def self.validate(current_schema, data, fragments, processor, validator, options
def self.list_types(types)
types.map { |type| type.is_a?(String) ? type : '(schema)' }.join(', ')
end

# Lookup Schema type of given class instance
def self.type_of_data(data)
type, _ = TYPE_CLASS_MAPPINGS.map { |k,v| [k,v] }.sort_by { |(_, v)|
-Array(v).map { |klass| klass.ancestors.size }.max
}.find { |(_, v)|
Array(v).any? { |klass| data.kind_of?(klass) }
}
type
end
end
end
end
2 changes: 1 addition & 1 deletion lib/json-schema/attributes/type_v4.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def self.validate(current_schema, data, fragments, processor, validator, options
message = format(
"The property '%s' of type %s did not match %s: %s",
build_fragment(fragments),
data.class,
type_of_data(data),
union ? 'one or more of the following types' : 'the following type',
types
)
Expand Down
6 changes: 3 additions & 3 deletions test/all_of_ref_schema_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ def test_all_of_ref_subschema_errors
errors = JSON::Validator.fully_validate(schema, data, :errors_as_objects => true)
nested_errors = errors[0][:errors]
assert_equal([:allof_0], nested_errors.keys, 'should have nested errors for each allOf subschema')
assert_match(/the property '#\/name' of type String did not match the following type: integer/i, nested_errors[:allof_0][0][:message])
assert_match(/the property '#\/name' of type string did not match the following type: integer/i, nested_errors[:allof_0][0][:message])
end

def test_all_of_ref_message
errors = JSON::Validator.fully_validate(schema, data)
expected_message = """The property '#/' of type Hash did not match all of the required schemas. The schema specific errors were:
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"""
- The property '#/name' of type string did not match the following type: integer"""
assert_equal(expected_message, errors[0])
end
end
2 changes: 1 addition & 1 deletion test/any_of_ref_schema_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def test_any_of_ref_subschema_errors
def test_any_of_ref_message
data = %({"names": ["jack"]})
errors = JSON::Validator.fully_validate(schema, data)
expected_message = """The property '#/names/0' of type String did not match one or more of the required schemas. The schema specific errors were:
expected_message = """The property '#/names/0' of type string did not match one or more of the required schemas. The schema specific errors were:
- anyOf #0:
- The property '#/names/0' value \"jack\" did not match the regex 'john'
Expand Down
2 changes: 1 addition & 1 deletion test/custom_format_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def test_format_validation
data["a"] = 23
errors = JSON::Validator.fully_validate(schema, data)
assert_equal(errors.count, 1)
assert_match(/The property '#\/a' of type (?:Integer|Fixnum) did not match the following type: string/i, errors.first, "#{prefix} records no format error on type mismatch")
assert_match(/The property '#\/a' of type integer did not match the following type: string/i, errors.first, "#{prefix} records no format error on type mismatch")
end
end

Expand Down
2 changes: 1 addition & 1 deletion test/min_items_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ def test_minitems_nils
errors = JSON::Validator.fully_validate(schema, [nil])
assert_equal(errors.length, 1)
assert(errors[0] !~ /minimum/)
assert(errors[0] =~ /NilClass/)
assert(errors[0] =~ /null/)
end
end
9 changes: 4 additions & 5 deletions test/one_of_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def test_one_of_sub_errors
errors = JSON::Validator.fully_validate(schema, { "a" => 5 }, :errors_as_objects => true)
nested_errors = errors[0][:errors]
assert_equal([:oneof_0,:oneof_1,:oneof_2], nested_errors.keys, 'should have nested errors for each allOf subschema')
assert_match(/the property '#\/a' of type (?:Integer|Fixnum) did not match the following type: string/i, nested_errors[:oneof_0][0][:message])
assert_match(/the property '#\/a' of type Integer did not match the following type: string/i, nested_errors[:oneof_0][0][:message])
assert_match(/the property '#\/a' did not have a minimum value of 10, inclusively/i, nested_errors[:oneof_2][0][:message])
end

Expand All @@ -69,13 +69,12 @@ def test_one_of_sub_errors_message
}

errors = JSON::Validator.fully_validate(schema, { "a" => 5 })
integer_type = RUBY_VERSION.to_f >= 2.4 ? 'Integer' : 'Fixnum'
expected_message = """The property '#/' of type Hash did not match any of the required schemas. The schema specific errors were:
expected_message = """The property '#/' of type object did not match any of the required schemas. The schema specific errors were:
- oneOf #0:
- The property '#/a' of type #{integer_type} did not match the following type: string
- The property '#/a' of type integer did not match the following type: string
- oneOf #1:
- The property '#/a' of type #{integer_type} did not match the following type: string
- The property '#/a' of type integer did not match the following type: string
- oneOf #2:
- The property '#/a' did not have a minimum value of 10, inclusively"""

Expand Down

0 comments on commit 853d0dc

Please sign in to comment.