Git Product home page Git Product logo

Comments (5)

justin-tay avatar justin-tay commented on July 18, 2024

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.

from json-schema-validator.

xinw3 avatar xinw3 commented on July 18, 2024

@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?

from json-schema-validator.

justin-tay avatar justin-tay commented on July 18, 2024

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.

from json-schema-validator.

xinw3 avatar xinw3 commented on July 18, 2024

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));

from json-schema-validator.

justin-tay avatar justin-tay commented on July 18, 2024

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));

from json-schema-validator.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.