-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Support boolean JSON schemas #1015
Conversation
Codecov ReportAttention: Patch coverage is
❗ Your organization needs to install the Codecov GitHub app to enable full functionality. Additional details and impacted files@@ Coverage Diff @@
## main #1015 +/- ##
==========================================
- Coverage 70.06% 61.33% -8.73%
==========================================
Files 62 62
Lines 4443 4449 +6
==========================================
- Hits 3113 2729 -384
- Misses 1330 1720 +390 ☔ View full report in Codecov by Sentry. |
Will merge if there are no objections @Harsha-Nori & @riedgar-ms |
if json_schema is True: | ||
json_schema = {} | ||
elif json_schema is False: | ||
raise ValueError("No valid JSON can be generated from a schema of `False`") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thoughts on raising error vs. returning empty string?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Intuitively I feel like warning + empty string is more "correct", but I'm easily convinced otherwise.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The empty string isn't valid against the False
schema. I think we need to raise an exception if someone wants to generate against a schema with no valid instances. Another "bad" schema by this standard is
{
"type": "object",
"properties": {
"name": false
},
"required": ["name"]
}
Note that raising an exception upon encountering a false
subschema isn't actually correct though. See the following example:
{
"type": "object",
"properties": {
"name": false
},
"required": []
}
This should validate against any object that does not have the key "name"
. Will not support for now -- I think I'll open an issue against this just to track it...
Edit: issue #1018
We were previously supporting
True
/False
schemas only when nested in certain places likeitems
andadditionalProperties
. This expands our coverage to handle top-level boolean schemas as well.Note that we will now raise a
ValueError
forFalse
schemas, simply because there is nothing we can generate in this case. Alternatively, we return a null grammar... But this may have unforseen consequences.Added some minimal tests, as we already have decent coverage for the nested cases in the
TestEmptySchema
class.Note: developed alongside #1014, which made it much easier to tell whether I had full coverage of every type we accept :)