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

Add a registry that can be used with JSON Schema validation #549

Open
michaelmior opened this issue Nov 1, 2024 · 5 comments
Open

Add a registry that can be used with JSON Schema validation #549

michaelmior opened this issue Nov 1, 2024 · 5 comments

Comments

@michaelmior
Copy link

Describe the proposed feature

Many JSON Schema validators support the ability to register external schemas based on their ID so they can be resolved at validation time.
The idea is that you pass in the schema and the ID and it would be saved into a local registry so that any future schemas which reference that schema by ID will discover it without needing resolution.

What other libraries (C++ or other) have this feature?

[Blaze]https://github.com/sourcemeta/blaze) is the only C++ library I am aware of.

Include a code fragment with sample data that illustrates the use of this feature

This is something that should be possible by implementing a resolver which has a method to add a schema instance.

@danielaparker
Copy link
Owner

I suppose we could add another parameter to make_json_schema that would allow you to provide a registry argument, say a
std::map<jsoncons::uri, jsoncons::jsonschema::json_schema>, to be used for lookup while building a schema. But it would need to supply a mapped schema for, not just a schema document, but referenceable subschemas in that document as well.

Compare that with our resolver example. Here, the resolver looks like

json resolver(const jsoncons::uri& uri)
{
    std::string pathname = "./input/jsonschema/";
    pathname += std::string(uri.path());

    std::fstream is(pathname.c_str());
    if (!is)
    {
        return json::null();
    }

    return json::parse(is);        
}

To build the schema, the schema builder needs to resolve the URI http://localhost:1234/draft2020-12/name-defs.json#/$defs/orNull. But we don't make the user supply a schema specifically for that, instead, we allow the user to supply the schema document '/name-defs.json'. The schema builder than processes that schema document and makes multiple entries into an internal validator registry, including uri/validator entries for

'http://localhost:1234/draft2020-12/name-defs.json'
'http://localhost:1234/draft2020-12/name-defs.json#/$defs/orNull'
'http://localhost:1234/draft2020-12/name-defs.json#/$defs/orNull/anyOf/0'

Any subsequent need to resolve those URI's is done using the internal registry rather than the resolver.

We couldn't do something analogous with a schema registry of uri's and json_schema objects. The uri's would have to match exactly. I don't think that would be practical.

@michaelmior
Copy link
Author

@danielaparker Could you clarify how the existing resolver could be used to supply just name-defs.json as in your example above? It sounds like it would then be possible to have all the subsequent URIs you listed be resolved according to that single document.

That is, can I provide the schema for http://localhost:1234/draft2020-12/name-defs.json in the resolver and then have the following all be automatically resolved?

'http://localhost:1234/draft2020-12/name-defs.json'
'http://localhost:1234/draft2020-12/name-defs.json#/$defs/orNull'
'http://localhost:1234/draft2020-12/name-defs.json#/$defs/orNull/anyOf/0'

@danielaparker
Copy link
Owner

That is, can I provide the schema for http://localhost:1234/draft2020-12/name-defs.json in the resolver and then have the following all be automatically resolved?

'http://localhost:1234/draft2020-12/name-defs.json'
'http://localhost:1234/draft2020-12/name-defs.json#/$defs/orNull'
'http://localhost:1234/draft2020-12/name-defs.json#/$defs/orNull/anyOf/0'

Yes.

@michaelmior
Copy link
Author

Could you give an example of how that would work? It seems like this would already resolve the issue here. (Although perhaps there could end up being a somewhat nicer API for what I'm trying to achieve.)

@danielaparker
Copy link
Owner

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants