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

Allow changing "$schema" in embedded resources #87

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

handrews
Copy link
Contributor

Fixes #59. Ultimately this was surprisingly simple (unlike several of my early attempts at it 😅 ).

As of draft 2020-12, embedded schemas (subschemas with "$id") can set "$schema" to something different from their containing resource.

2019-09 allows the presence of "$schema" in such circumstances, but leaves the behavior up to the implementation if the value differs from the containing resource. This change allows "$schema" switching to behave the same in 2019-09 as in 2020-12.

The change adds a validating_with parameter to the Result constructor and __call__ methods, which is set by JSONSchema.validate() to indicate that the evaluation is evaluating a schema against its metaschema, rather than a normal evaluation.

JSONSchema.evaluate() checks result.validating_with, and if all of the conditions are met, changes the evaluating schema from self to the schema-as-instance's metaschema before proceeding as normal. The original self schema, whether boolean or object, is not applied at all.

@codecov-commenter
Copy link

codecov-commenter commented Mar 24, 2023

Codecov Report

Patch coverage: 100.00% and project coverage change: +0.03% 🎉

Comparison is base (e6f71d0) 92.73% compared to head (5418e49) 92.77%.

Additional details and impacted files
@@            Coverage Diff             @@
##             main      #87      +/-   ##
==========================================
+ Coverage   92.73%   92.77%   +0.03%     
==========================================
  Files          23       23              
  Lines        2052     2061       +9     
  Branches      435      437       +2     
==========================================
+ Hits         1903     1912       +9     
  Misses         97       97              
  Partials       52       52              
Files Changed Coverage Δ
jschon/jsonschema.py 92.04% <100.00%> (+0.25%) ⬆️

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

As of draft 2020-12, embedded schemas (subschemas with "$id") can
set "$schema" to something different from their containing resource.

2019-09 allows the presence of "$schema" in such circumstances, but
leaves the behavior up to the implementation if the value differs
from the containing resource.  This change allows "$schema" switching
to behave the same in 2019-09 as in 2020-12.

The change adds a `validating_with` parameter to the `Result`
constructor and `__call__` methods, which is set by
`JSONSchema.validate()` to indicate that the evaluation is evaluating
a schema against its metaschema, rather than a normal evaluation.

`JSONSchema.evaluate()` checks `result.validating_with`, and if
all of the conditions are met, changes the evaluating schema from
`self` to the schema-as-instance's metaschema before proceeding
as normal.  The original `self` schema, whether boolean or object,
is not applied at all.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Embedded "$schema" with different values in draft 2020-12
2 participants