Closed Aravinda93 closed 3 months ago
It's hard to say since you didn't show your schema.
If /definitions/testDocument
is really a file relative to your schema then you should load your schema using a SchemaLocation
so that it knows the base IRI to resolve it to.
If /definitions/testDocument
is supposed to be in the same schema document, then it should be #/definitions/testDocument
.
@justin-tay I'm trying to upgrade from 1.0.64 to 1.4.0 and seeing the same issue. My schema is like the follows:
{
"$schema": "https://json-schema.org/draft/2019-09/schema",
"$id": "https://json-schema.org/draft/2019-09/schema",
"title": "test schema",
"type": "object",
"properties": {
"fields": {
"type": "object",
"properties": {
"ids": {
"$ref": "#/$defs/fruits"
}
}
}
},
"$defs": {
"fruits": {
"$id": "/fruits/id",
"type": "object",
"properties": {
"berries": {
"type": "array",
"items": {
"$ref": "#/$defs/Berry"
}
}
}
},
"Berry": {
"$id": "/berry-id",
"type": "object",
"properties": {
"type": {
"type": "string"
}
}
}
}
}
The json schema validator complains: com.networknt.schema.InvalidSchemaRefException: : Reference /$defs/Berry cannot be resolved
. How was 1.0.64
able to resolve it?
If I paste the schema to an online json schema validator, I receive errors as well. Was it a bug in 1.0.64
?
There are plenty of issues in 1.0.64
that were fixed particularly for ref resolution and embedded schema resources.
Putting $id
in a subschema makes it schema resource which makes any $ref
that references the document #
resolve to that schema resource.
In your example, all your $id
entries make no sense and you should just remove all of them. You shouldn't use https://json-schema.org/draft/2019-09/schema
for your $id
as this is for the metaschema. It is intended to use your own identifier. In subschemas the $id
is used for embedded schema resources and it doesn't appear to be your intent as you don't reference them.
Thanks for the answer @justin-tay. The upper level $id
was just an example. The actual id is like classpath:schemas/test
. I got past that error by not referencing the subschemas under the same $defs
document since that could cause infinite loop and is not allowed based on the documentation.
However, I encountered another error. I have some json schema documents defined under a common
folder so they can be shared by different schemas. I'm seeing this error after upgrade com.networknt.schema.JsonSchemaFactory - Failed to load json schema from classpath:schemas/common/child2.json
. The files would be something like:
main.json
{
"$schema": "https://json-schema.org/draft/2019-09/schema",
"$id": "classpath:schemas/main",
"title": "main schema",
"type": "object",
"properties": {
"fields": {
"type": "object",
"properties": {
"ids": {
"$ref": "../common/child.json"
}
}
}
}
}
child.json
{
"$id": "/common/child",
"title": "child schema",
"type": "object",
"properties": {
"value": {
"$ref": "./child2.json"
}
}
}
child2.json
{
"$id": "/common/child2",
"title": "child2 schema",
"type": "object",
"properties": {
"value": {
"type": "string",
"$comment": "child value."
}
}
}
Folder structure:
| -- main
| | -- main.json
| -- common
| | -- child.json
| | -- child2.json
Is there something wrong with the way how I load the schemas? I use the following:
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V201909);
return factory.getSchema(getClass().getResourceAsStream(name));
Your $id
entries used are very confusing. They should be absolute IRIs. Here your $id
entries look like the retrieval IRI, but aren't as they aren't placed in the correct structure, nor have the correct extension. You would be better off just removing them. The error says it can't find classpath:schemas/common/child2.json
probably because it's not there.
After removing all your $id
entries you can try the following.
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(VersionFlag.V201909);
JsonSchema schema = factory.getSchema(SchemaLocation.of("classpath:/main/main.json"));
String inputData = "{\r\n"
+ " \"fields\": {\r\n"
+ " \"ids\": {\r\n"
+ " \"value\": {\r\n"
+ " \"value\": 1\r\n"
+ " }\r\n"
+ " }\r\n"
+ " }\r\n"
+ "}";
System.out.println(schema.validate(inputData, InputFormat.JSON, OutputFormat.HIERARCHICAL));
I was previously using the following version of the JSON Schema Validator and everything was working fine when I was validating the JSON against the respective JSON Schema:
Following is the code I have for
1.0.74
version:With its test case:
The above-provided code works perfectly fine and is able to identify any issues that may be present in JSON by validating against the JSON Schema.
Now I only changed the version from
1.0.74
to latest version1.3.2
for the Schema validator:Based on this change I had to change the
objectMapper
tojsonMapper
while creating the instance of the factory:Apart from this everything remains the same including the JSON document provided as input and JSON schema but when I run this test then I get the following error:
Can anyone please let me know what needs to be modified based on the version update? I am unable to find anything on the web.