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

Add :property_fragment to errors hash, to show which required property was missing #420

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/json-schema/attribute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ def self.build_fragment(fragments)
"#/#{fragments.join('/')}"
end

def self.validation_error(processor, message, fragments, current_schema, failed_attribute, record_errors)
error = ValidationError.new(message, fragments, failed_attribute, current_schema)
def self.validation_error(processor, message, fragments, current_schema, failed_attribute, record_errors, property = nil)
error = ValidationError.new(message, fragments, failed_attribute, current_schema, property)
if record_errors
processor.validation_error(error)
else
Expand Down
2 changes: 1 addition & 1 deletion lib/json-schema/attributes/dependencies.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def self.validate(current_schema, data, fragments, processor, validator, options
def self.validate_dependency(schema, data, property, value, fragments, processor, attribute, options)
return if data.key?(value.to_s)
message = "The property '#{build_fragment(fragments)}' has a property '#{property}' that depends on a missing property '#{value}'"
validation_error(processor, message, fragments, schema, attribute, options[:record_errors])
validation_error(processor, message, fragments, schema, attribute, options[:record_errors], property)
end

def self.accept_value?(value)
Expand Down
2 changes: 1 addition & 1 deletion lib/json-schema/attributes/properties.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def self.validate(current_schema, data, fragments, processor, validator, options

if required?(property_schema, options) && !data.has_key?(property)
message = "The property '#{build_fragment(fragments)}' did not contain a required property of '#{property}'"
validation_error(processor, message, fragments, current_schema, self, options[:record_errors])
validation_error(processor, message, fragments, current_schema, self, options[:record_errors], property)
end

if data.has_key?(property)
Expand Down
2 changes: 1 addition & 1 deletion lib/json-schema/attributes/properties_optional.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def self.validate(current_schema, data, fragments, processor, validator, options

if !property_schema['optional'] && !data.key?(property)
message = "The property '#{build_fragment(fragments)}' did not contain a required property of '#{property}'"
validation_error(processor, message, fragments, current_schema, self, options[:record_errors])
validation_error(processor, message, fragments, current_schema, self, options[:record_errors], property)
end

if data.has_key?(property)
Expand Down
2 changes: 1 addition & 1 deletion lib/json-schema/attributes/required.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def self.validate(current_schema, data, fragments, processor, validator, options

if !prop_defaults
message = "The property '#{build_fragment(fragments)}' did not contain a required property of '#{property}'"
validation_error(processor, message, fragments, current_schema, self, options[:record_errors])
validation_error(processor, message, fragments, current_schema, self, options[:record_errors], property)
end
end
end
Expand Down
21 changes: 18 additions & 3 deletions lib/json-schema/errors/validation_error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ module JSON
class Schema
class ValidationError < StandardError
INDENT = " "
attr_accessor :fragments, :schema, :failed_attribute, :sub_errors, :message
attr_accessor :fragments, :schema, :failed_attribute, :sub_errors, :message, :property

def initialize(message, fragments, failed_attribute, schema)
def initialize(message, fragments, failed_attribute, schema, property = nil)
@fragments = fragments.clone
@property = property
@schema = schema
@sub_errors = {}
@failed_attribute = failed_attribute
Expand All @@ -27,7 +28,21 @@ def to_string(subschema_level = 0)
end

def to_hash
base = {:schema => @schema.uri, :fragment => ::JSON::Schema::Attribute.build_fragment(fragments), :message => message_with_schema, :failed_attribute => @failed_attribute.to_s.split(":").last.split("Attribute").first}
fragment_string = ::JSON::Schema::Attribute.build_fragment(fragments)
if property
property_fragments = fragments + [property]
property_fragment_string = ::JSON::Schema::Attribute.build_fragment(property_fragments)
else
property_fragment_string = fragment_string
end

base = {
:schema => @schema.uri,
:fragment => fragment_string,
:property_fragment => property_fragment_string,
:message => message_with_schema,
:failed_attribute => @failed_attribute.to_s.split(":").last.split("Attribute").first
}
if !@sub_errors.empty?
base[:errors] = @sub_errors.inject({}) do |hsh, (subschema, errors)|
subschema_sym = subschema.downcase.gsub(/\W+/, '_').to_sym
Expand Down
16 changes: 11 additions & 5 deletions test/full_validation_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,13 @@ def test_full_validation_with_object_errors

errors = JSON::Validator.fully_validate(schema,data,:errors_as_objects => true)

assert(errors.length == 2)
assert(errors[0][:failed_attribute] == "Required")
assert(errors[0][:fragment] == "#/")
assert(errors[1][:failed_attribute] == "TypeV4")
assert(errors[1][:fragment] == "#/c")
assert_equal errors.length, 2
assert_equal errors[0][:failed_attribute], "Required"
assert_equal errors[0][:fragment], "#/"
assert_equal errors[0][:property_fragment], "#/b"
assert_equal errors[1][:failed_attribute], "TypeV4"
assert_equal errors[1][:fragment], "#/c"
assert_equal errors[1][:property_fragment], "#/c"
end

def test_full_validation_with_nested_required_properties
Expand All @@ -163,8 +165,10 @@ def test_full_validation_with_nested_required_properties
errors = JSON::Validator.fully_validate(schema,data,:errors_as_objects => true)
assert_equal 2, errors.length
assert_equal '#/x', errors[0][:fragment]
assert_equal '#/x/b', errors[0][:property_fragment]
assert_equal 'Required', errors[0][:failed_attribute]
assert_equal '#/x/e', errors[1][:fragment]
assert_equal '#/x/e', errors[1][:property_fragment]
assert_equal 'TypeV4', errors[1][:failed_attribute]
end

Expand Down Expand Up @@ -196,8 +200,10 @@ def test_full_validation_with_nested_required_propertiesin_array
errors = JSON::Validator.fully_validate(schema,data,:errors_as_objects => true)
assert_equal 2, errors.length
assert_equal '#/x/0', errors[0][:fragment]
assert_equal '#/x/0/b', errors[0][:property_fragment]
assert_equal 'Required', errors[0][:failed_attribute]
assert_equal '#/x/1/e', errors[1][:fragment]
assert_equal '#/x/1/e', errors[1][:property_fragment]
assert_equal 'TypeV4', errors[1][:failed_attribute]
end
end