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

[Feature] Constant elements in JSON schemas #886

Merged
merged 4 commits into from
Jun 6, 2024

Conversation

wjn0
Copy link
Contributor

@wjn0 wjn0 commented Jun 5, 2024

This is a super quick PR as I work toward supporting some large JSON schemas I'm working with. All it does is support the const keyword in JSON schemas, which are a way of hard coding constant elements: https://json-schema.org/understanding-json-schema/reference/const

@hudson-ai
Copy link
Collaborator

Nice! Thank you for the contribution 😄

I assume you moved the constant check to be first in order to avoid generating something else if type is specified? Would you mind adding a test to make sure that behavior is working as expected?

Also, what kinds of values can constants take on? Maybe a few more test cases for strings, objects, etc. would be good, as well as a test that consts work as expected when used as object properties.

@riedgar-ms ping for interest

@wjn0
Copy link
Contributor Author

wjn0 commented Jun 5, 2024

Happy to!

Yeah, it should come first IMO because consts can optionally be typed, e.g. {"type": "integer", "const": 1} is a valid schema (however, if 1 was instead "1" that would be an invalid schema, so we don't need to do any type checking once there is a const). I added a test to that effect.

Indeed, consts can be anything, so I added a test for each of several types (integers, strings, arrays, and complex objects), along with a test for when a const is a property of an object.

Thanks for looking!

@riedgar-ms riedgar-ms changed the title Support constant elements in JSON schemas [Feature] Constant elements in JSON schemas Jun 6, 2024
Copy link
Collaborator

@riedgar-ms riedgar-ms left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

I don't think we need to check that the value in const matches any type field; we expect the user to feed us a valid JSON schema. I'm just wondering about negative test cases... although given that all this does is call _to_compact_json(), I'm not sure there's a huge value there. WDYT @hudson-ai ?

@codecov-commenter
Copy link

codecov-commenter commented Jun 6, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 60.19%. Comparing base (325b2c8) to head (1d0547d).

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #886      +/-   ##
==========================================
+ Coverage   56.83%   60.19%   +3.35%     
==========================================
  Files          63       63              
  Lines        4622     4625       +3     
==========================================
+ Hits         2627     2784     +157     
+ Misses       1995     1841     -154     

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

@hudson-ai
Copy link
Collaborator

@wjn0 thank you for the additional tests!

@riedgar-ms I agree that checking that type matches the const is unnecessary, as well as extensive negative tests. That being said, I think that there is at least one negative test that would be useful here:

If the user passes both a type and a const (presumed to be compatible with each other), we should assert that the const value has "priority". So I think I'd take that last test that has {"type": "integer", "const": 1} and turn it into a negative test that asserts that 2 doesn't match, for example. What do you two think?

@riedgar-ms
Copy link
Collaborator

@hudson-ai @wjn0 Agree on that extra negative test, to make sure that the const takes precedence over any type.

@wjn0
Copy link
Contributor Author

wjn0 commented Jun 6, 2024

@hudson-ai @riedgar-ms done! Added a (negative) precedence test, hopefully in keeping with the rest of the suite.

@hudson-ai
Copy link
Collaborator

Awesome!! Looks good to me.

@hudson-ai hudson-ai merged commit 14862a1 into guidance-ai:main Jun 6, 2024
103 checks passed
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.

4 participants