-
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
[Bug] [Feature] Fix issue #1039 by exposing And
, Not
regex operators from llguidance
#1043
Conversation
guidance/library/_json.py
Outdated
names = set(properties.keys()) | set(required) | ||
if len(names) > 0: | ||
# If there are any properties, we need to disallow them as additionalProperties | ||
key_grammar = as_regular_grammar( | ||
And([ | ||
lexeme(r'"([^"\\]|\\["\\/bfnrt]|\\u[0-9a-fA-F]{4})*"'), | ||
Not(lexeme('"(' + '|'.join(quote_regex(name) for name in names) + ')"')), | ||
]), | ||
lexeme = True, | ||
) | ||
else: | ||
key_grammar = _gen_json_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.
Beyond the addition of new types and a bit of reorganization of _grammar.py
, here's the meat of the bug fix.
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.
You could put the quotes outside the whole thing and use Or over String. Maybe a bit cleaner
def test_out_of_order_non_required_properties_not_validated_as_additionalProperties(self): | ||
schema = { | ||
"type": "object", | ||
"properties": {"a": {"const": "foo"}, "b": {"const": "bar"}}, | ||
"required": ["b"], | ||
} | ||
test_string = '{"b": "bar", "a": "BAD"}' | ||
grammar = gen_json(schema=schema) | ||
assert grammar.match(test_string) is None |
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.
Test for issue #1039 which fails on main
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 #1043 +/- ##
==========================================
- Coverage 71.39% 63.38% -8.02%
==========================================
Files 63 63
Lines 4670 4708 +38
==========================================
- Hits 3334 2984 -350
- Misses 1336 1724 +388 ☔ View full report in Codecov by Sentry. |
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.
Lgtm. The Or thing is a nitpick. Ignore.
Fixes issue #1039 by ensuring that the keys for JSON properties validated against
additionalProperties
aren't allowed to match any of the keys for the properties themselves.I'm still a bit unsure about the API of
And
andNot
, as well as what the right type hierarchy is (e.g. should they be subclasses ofRegularGrammar
?), but I am comfortable with punting on those questions a bit as they are very much not part of the "public" API yet.