Schema validation with subschema and internal reference #596
-
Hello, I'm having trouble validating a JSON against a JSON schema. My case is a bit specific, I'll try to describe it as clearly as possible. I have a function that takes two string arguments:
I create my validator with Now my case is the following, The code looks like this:
And this is what a simplified root schema looks like here (which again is not a schema in this particular case):
With So in my function, after the if block, I end up with schema being equal to:
But then I don't know how to proceed. The URI resolver does not get called for the internal reference (which I think is normal because it is only called for external references?). I get the error I tried simplify my use case the most I could to avoid making an even longer post. Let me know if some information is missing. Thanks in advance! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Okay, from jsoncons's point of view, $ref in payload is accessing an external resource (one outside a schema resource.) Therefore, I think you should substitute
for
Note that we write the reference with a leading slash to prevent URI resolution from merging '/key_A/key_A_2/bar' with the default base URI path '/this/is/my/folder/file' (which we don't want.) Then all references should be resolved in the usual way: #include <jsoncons/json.hpp>
#include <jsoncons_ext/jsonschema/jsonschema.hpp>
#include <jsoncons_ext/jsonpointer/jsonpointer.hpp>
#include <iostream>
namespace jsonschema = jsoncons::jsonschema;
namespace jsonpointer = jsoncons::jsonpointer;
std::string input_str = R"(
{
"key_A": {
"key_A_1": {
"foo": {
"payload": {
"type": "object",
"patternProperties": {
"some_regex": {
"type": "object",
"$ref": "/key_A/key_A_2/bar"
}
}
}
}
},
"key_A_2": {
"bar": {
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"name": { "type": "string" },
"containers": {
"type": "array",
"items": [
{
"type": "object",
"properties": {
"number_A": { "type": "number" },
"number_B": { "type": "number" }
}
}
]
}
}
}
}
}
}
)";
int main()
{
auto input = jsoncons::ojson::parse(input_str);
auto schema = jsonpointer::get(input, "/key_A/key_A_1/foo/payload");
auto resolver = [&](const jsoncons::uri& uri) -> jsoncons::ojson
{
std::cout << "path: " << uri.path() << "\n";
std::error_code ec;
jsoncons::ojson schemaPart = jsonpointer::get(input, uri.path(), ec);
return ec ? jsoncons::ojson::null() : schemaPart;
};
try
{
auto options = jsonschema::evaluation_options{}
.default_base_uri("/this/is/my/folder/file");
auto compiled = jsonschema::make_json_schema(schema, resolver, options);
}
catch (const std::exception& e)
{
std::cout << e.what() << "\n";
}
} Output:
|
Beta Was this translation helpful? Give feedback.
Okay, from jsoncons's point of view, $ref in payload is accessing an external resource (one outside a schema resource.) Therefore, I think you should substitute
for
Note that we write the reference with a leading slash to prevent URI resolution from merging '/key_A/key_A_2/bar' with the default base URI path '/this/is/my/folder/file' (which we don't want.)
Then all references should be resolved in the usual way: