Skip to content

Commit

Permalink
Support Schema#ref in subschemas
Browse files Browse the repository at this point in the history
`resolve_ref` needs to be called on the root schema because that's where
all the `resources` are stored.

The behavior here is kind of confusing because the ref is resolved just
like the `$ref` keyword would be in the schema, so it's dependent on the
schema's base URI. That means for a subschema `ref('#')` doesn't
necessarily resolve to itself (ie, if the subschema doesn't have `$id`).
  • Loading branch information
davishmcclurg committed Sep 26, 2023
1 parent be45f04 commit e5ca2b1
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Bug Fixes

- Require discriminator `propertyName` property
- Support `Schema#ref` in subschemas

[2.1.0]: https://github.com/davishmcclurg/json_schemer/releases/tag/v2.1.0

Expand Down
2 changes: 1 addition & 1 deletion lib/json_schemer/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def validate_schema
end

def ref(value)
resolve_ref(URI.join(base_uri, value))
root.resolve_ref(URI.join(base_uri, value))
end

def validate_instance(instance, instance_location, keyword_location, context)
Expand Down
18 changes: 17 additions & 1 deletion test/json_schemer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,12 @@ def test_schema_ref
'type' => 'integer',
'$defs' => {
'foo' => {
'$id' => 'subschemer',
'$defs' => {
'bar' => {
'required' => ['z']
}
},
'type' => 'object',
'required' => ['x', 'y'],
'properties' => {
Expand All @@ -401,10 +407,20 @@ def test_schema_ref

refute(subschemer.valid?(1))
assert_equal(
[["/x", "/$defs/foo/properties/x", "string"], ["", "/$defs/foo", "required"]],
[['/x', '/$defs/foo/properties/x', 'string'], ['', '/$defs/foo', 'required']],
subschemer.validate({ 'x' => 1 }).map { |error| error.values_at('data_pointer', 'schema_pointer', 'type') }
)
assert(subschemer.valid?({ 'x' => '1', 'y' => 1 }))

subsubschemer = subschemer.ref('#/$defs/bar')
refute(subsubschemer.valid?({ 'x' => 1 }))
assert_equal(
[['', '/$defs/foo/$defs/bar', 'required']],
subsubschemer.validate({ 'x' => 1 }).map { |error| error.values_at('data_pointer', 'schema_pointer', 'type') }
)

assert_equal(subschemer, subschemer.ref('#'))
assert_equal(subschemer, subsubschemer.ref('#'))
end

def test_published_meta_schemas
Expand Down

0 comments on commit e5ca2b1

Please sign in to comment.