Skip to content

Commit

Permalink
Merge pull request #180 from davishmcclurg/compare-instance-locations…
Browse files Browse the repository at this point in the history
…-by-identity

Compare property default instance locations by id
  • Loading branch information
davishmcclurg committed Mar 14, 2024
2 parents 8be18df + b92e1c2 commit 002633b
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
1 change: 0 additions & 1 deletion lib/json_schemer/configuration.rb
@@ -1,5 +1,4 @@
# frozen_string_literal: true

module JSONSchemer
Configuration = Struct.new(
:base_uri, :meta_schema, :vocabulary, :format, :formats, :content_encodings, :content_media_types, :keywords,
Expand Down
1 change: 1 addition & 0 deletions lib/json_schemer/result.rb
Expand Up @@ -191,6 +191,7 @@ def classic

def insert_property_defaults(context)
instance_locations = {}
instance_locations.compare_by_identity

results = [[self, true]]
while (result, valid = results.pop)
Expand Down
48 changes: 48 additions & 0 deletions test/hooks_test.rb
Expand Up @@ -668,4 +668,52 @@ def test_insert_property_defaults_ref_depth_first
assert(JSONSchemer.schema(schema, insert_property_defaults: true).valid?(data))
assert_equal('ref', data.fetch('x'))
end

def test_insert_property_defaults_compare_by_identity
data = JSON.parse(%q({
"fieldname": [
{ "aaaa": "item0", "bbbb": "val1", "cccc": true },
{ "aaaa": "item1", "cccc": true, "buggy": true, "dddd": 0 },
{ "aaaa": "item2", "cccc": true, "buggy": true, "dddd": 0 },
{ "aaaa": "item3", "buggy": true },
{ "aaaa": "item4", "cccc": true },
{ "aaaa": "item5", "cccc": true }
]
}))
schema = %q({
"type": "object",
"properties": {
"fieldname": {
"type": "array",
"items": {
"$ref": "#/$defs/Record"
}
}
},
"$defs": {
"Enumerated": {
"enum": [ "val1", "val2" ]
},
"Record": {
"type": "object",
"properties": {
"aaaa": { "type": "string" },
"cccc": { "type": "boolean", "default": false },
"buggy": { "type": "boolean", "default": false },
"bbbb": { "$ref": "#/$defs/Enumerated", "default": "val2" },
"dddd": { "type": "number", "default": 0 },
"eeee": { "type": "boolean", "default": false },
"ffff": { "type": "boolean", "default": false }
}
}
}
})
assert(JSONSchemer.schema(schema, insert_property_defaults: true).valid?(data))
assert_equal('val2', data.dig('fieldname', 2, 'bbbb'))
assert_equal(false, data.dig('fieldname', 3, 'cccc'))
assert_equal(false, data.dig('fieldname', 4, 'buggy'))
assert_equal(0, data.dig('fieldname', 0, 'dddd'))
assert_equal(false, data.dig('fieldname', 1, 'eeee'))
assert_equal(false, data.dig('fieldname', 5, 'ffff'))
end
end

0 comments on commit 002633b

Please sign in to comment.