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

JSON reserved keywords #1032

Merged
merged 3 commits into from
Sep 18, 2024
Merged

Conversation

hudson-ai
Copy link
Collaborator

@hudson-ai hudson-ai commented Sep 18, 2024

Add a set containing every single keyword reserved by JSON schema Draft 2020-12.

"Invalid" keywords (rather, unsupported/not-yet-supported keys) are now defined as "any keyword we don't explicitly provide an implementation for that is ALSO a reserved keyword.

This should prevent us from having to manually whitelist non-keywords. Note that I think that the sets/enums of "supported" keys probably needs a second refactor... Not part of this PR though.

For posterity, here's the code I wrote to get the set of all reserved keywords:

from referencing import Registry
from referencing.retrieval import to_cached_resource
import httpx

@to_cached_resource()
def retrieve_with_httpx(url: str) -> str:
    response = httpx.get(url)
    response.raise_for_status()
    return response.text

registry = Registry(retrieve=retrieve_with_httpx)
resolved = registry.resolver().lookup("https://json-schema.org/draft/2020-12/schema")

def crawl(schema, resolver):
    props = {}
    todo = [schema]
    while todo:
        current = todo.pop()
        if "$ref" in current:
            resolved = resolver.lookup(current["$ref"])
            props.update(
                crawl(resolved.contents, resolved.resolver)
            )
        if "properties" in current:
            props.update(current["properties"])
        if "allOf" in current:
            todo.extend(current["allOf"])
        if "anyOf" in current:
            raise NotImplementedError("anyOf")
        if "oneOf" in current:
            raise NotImplementedError("oneOf")
        if "$dynamicRef" in current:
            raise NotImplementedError("$dynamicRef")

    return props

props = crawl(resolved.contents, resolved.resolver)

@codecov-commenter
Copy link

codecov-commenter commented Sep 18, 2024

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 61.46%. Comparing base (78575da) to head (9fa707d).
Report is 2 commits behind head on main.

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

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1032      +/-   ##
==========================================
- Coverage   70.20%   61.46%   -8.74%     
==========================================
  Files          62       62              
  Lines        4470     4471       +1     
==========================================
- Hits         3138     2748     -390     
- Misses       1332     1723     +391     

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

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.

Minor suggestion: reformat so that the keywords aren't just one huge line, and sort them. Don't feel obliged, though.

@hudson-ai hudson-ai merged commit edd8b87 into guidance-ai:main Sep 18, 2024
45 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.

3 participants