-
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
[Feature] Allow oneOf
in JSON schemas (with limited support)
#982
Changes from all commits
43ab5c4
fd19c43
b318983
ed264f6
beb07c4
fe6ebd6
ed60218
026a885
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,7 @@ | |
from guidance import json as gen_json | ||
from guidance import models | ||
|
||
from guidance.library._json import _to_compact_json, WHITESPACE | ||
from guidance.library._json import _to_compact_json, WHITESPACE, IGNORED_KEYS | ||
|
||
from ...utils import check_match_failure as _check_match_failure | ||
from ...utils import check_run_with_temperature | ||
|
@@ -1319,6 +1319,37 @@ def test_allOf_bad_schema(self): | |
lm += gen_json(name=CAPTURE_KEY, schema=schema_obj) | ||
assert ve.value.args[0] == "Only support allOf with exactly one item" | ||
|
||
class TestOneOf: | ||
@pytest.mark.parametrize("target_obj", [123, 42]) | ||
def test_oneOf_simple(self, target_obj): | ||
schema = """{ | ||
"oneOf" : [{ "type": "integer" }] | ||
} | ||
""" | ||
# First sanity check what we're setting up | ||
schema_obj = json.loads(schema) | ||
validate(instance=target_obj, schema=schema_obj) | ||
|
||
# The actual check | ||
generate_and_check(target_obj, schema_obj) | ||
|
||
|
||
@pytest.mark.parametrize("target_obj", [123, True]) | ||
def test_oneOf_compound(self, target_obj): | ||
schema = """{ | ||
"oneOf" : [{ "type": "integer" }, { "type": "boolean" }] | ||
} | ||
""" | ||
# First sanity check what we're setting up | ||
schema_obj = json.loads(schema) | ||
validate(instance=target_obj, schema=schema_obj) | ||
|
||
# The actual check; we expect a warning here because oneOf is not fully supported | ||
with pytest.warns() as record: | ||
generate_and_check(target_obj, schema_obj) | ||
assert len(record) == 1 | ||
assert record[0].message.args[0].startswith("oneOf not fully supported") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for including this check (wrote my 'main' comment before seeing the code). |
||
|
||
|
||
class TestEnum: | ||
simple_schema = """{ | ||
|
@@ -1950,3 +1981,13 @@ def test_no_additionalProperties(self, compact): | |
maybe_whitespace=True, | ||
compact=compact, | ||
) | ||
|
||
def test_ignored_keys_allowed_as_properties(): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @riedgar-ms is this test acceptable to you here? It doesn't explicitly test There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Even better to check them all |
||
schema_obj = { | ||
"type": "object", | ||
"properties": { | ||
key: {"type": "string"} for key in IGNORED_KEYS | ||
} | ||
} | ||
target_obj = {key: "value" for key in IGNORED_KEYS} | ||
generate_and_check(target_obj, schema_obj) |
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.
I believe that our problem with
id
(in the FHIR schema) is only at top level. I think thatid
is a perfectly fine name for a property on an object.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.
validate_json_node_keys
is only being called on dictionaries that represent full (sub)schemas, which doesn't include the dictionary specified by theproperties
key of a (sub)schema. Therefore the set of ignored keys should have no impact on what property names are valid -- worth checking if you have doubts :)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.
I'd suggest adding a test with an object with an
id
property.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.
Happy to do so :)