palwe-prafulla
palwe-prafulla

Reputation: 141

networknt schema validator $id $ref not working in json schema

I am using networknt json schema validator for my project. I have defined one json schema and more definitions in other json schema file. But when I use the reference to other json file with its id,validator is throwing no json schema found (file not found ) exception. I have made my ids to look like URLs (of course not network accessible since its just id) But it seems networknt validator tries to retrieve schema considering this as URL? Standard json schema documentation says that the id is not necessarily network accessible so I am not sure why its trying to fetch it. In any case I want to understand how can i created $id and $ref for json schema files to make it work.

Sample schema 1:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "https://example.com /schema/profileparcel/cedge/system/banner/post/request_schema.json",
  "title": "Banner Parcel Schema",
  "description": "Banner profile parcel schema for POST request",
  "type": "object",
  "properties": {
    "name": {
      "type": "string"
    },
    "description": {
      "type": "string"
    },
    "data": {
      "type": "object",
      "properties": {
        "login": {
          "type": "object",
          "$ref": "#/$defs/oneOfOptionsDef"
        },
        "motd": {
          "type": "object",
          "$ref": "#/$defs/oneOfOptionsDef"
        }
      },
      "required": [
        "login",
        "motd"
      ],
      "additionalProperties": false
    }
  },
  "required": [
    "data"
  ],
  "$defs": {
    "messageDef": {
      "type": "string",
      "minLength": 1,
      "maxLength": 2048
    },
    "oneOfOptionsDef": {
      "oneOf": [
        {
          ***"$ref": "https://example.com /schema/common/common_json_schema_defs.json#/defs/variableOptionTypeObjectDef"***
        },
        {
          "properties": {
            "optionType": {
              ***"$ref": "https://example.com /schema/common/common_json_schema_defs.json#/defs/globalOptionTypeDef"***
            },
            "message": {
              "$ref": "#/$defs/messageDef"
            }
          },
          "required": [
            "optionType",
            "message"
          ],
          "additionalProperties": false
        },
        {
          "properties": {
            "optionType": {
              ***"$ref": "https://example.com /schema/common/common_json_schema_defs.json#/defs/defaultOptionTypeDef"***
            },
            "message": {
              ***"$ref": "https://example.com /schema/common/common_json_schema_defs.json#/defs/emptyStringDef"***
            }
          },
          "required": [
            "optionType",
            "message"
          ],
          "additionalProperties": false
        }
      ]
    }
  },
  "additionalProperties": false
}

Sample schema 2:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "https://example.com /schema/common/common_json_schema_defs.json",
  "title": "Common Json Schema Definitions",
  "description": "Schema defining all the reusable common defs in other schemas",
  "type": "object",
  "defs": {
    "emptyStringDef": {
      "type": "string",
      "enum": [
        ""
      ]
    },
    "variableNameDef": {
      "type": "string",
      "pattern": "^\\{\\{[a-zA-Z0-9_-]+\\}\\}$",
      "minLength": 1,
      "maxLength": 32
    },
    "globalOptionTypeDef": {
      "enum": [
        "global"
      ]
    },
    "variableOptionTypeDef": {
      "enum": [
        "variable"
      ]
    },
    "defaultOptionTypeDef": {
      "enum": [
        "default"
      ]
    },
    "variableOptionTypeObjectDef": {
      "properties": {
        "optionType": {
          "$ref": "#/defs/variableOptionTypeDef"
        },
        "variableName": {
          "$ref": "#/defs/variableNameDef"
        }
      },
      "required": [
        "optionType",
        "variableName"
      ],
      "additionalProperties": false
    }
  }
}

Exception during validation:

2021-08-12T15:53:02,413  [ERROR] [main] JsonSchemaFactory: Failed to load json schema!
java.io.FileNotFoundException: https://example.com /schema/common/common_json_schema_defs.json
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1920) ~[?:?]
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1520) ~[?:?]
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:250) ~[?:?]
    at com.networknt.schema.uri.URLFetcher.openConnectionCheckRedirects(URLFetcher.java:57) ~[json-schema-validator-1.0.57.jar:?]
    at com.networknt.schema.uri.URLFetcher.fetch(URLFetcher.java:43) ~[json-schema-validator-1.0.57.jar:?]
    at com.networknt.schema.uri.URISchemeFetcher.fetch(URISchemeFetcher.java:50) ~[json-schema-validator-1.0.57.jar:?]
    at com.networknt.schema.JsonSchemaFactory.getSchema(JsonSchemaFactory.java:345) [json-schema-validator-1.0.57.jar:?]
    at com.networknt.schema.RefValidator.getRefSchema(RefValidator.java:79) [json-schema-validator-1.0.57.jar:?]
    at com.networknt.schema.OneOfValidator$ShortcutValidator.<init>(OneOfValidator.java:37) [json-schema-validator-1.0.57.jar:?]
    at com.networknt.schema.OneOfValidator.<init>(OneOfValidator.java:122) [json-schema-validator-1.0.57.jar:?]
    at jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[?:?]
    at jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) [?:?]
    at jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) [?:?]
    at java.lang.reflect.Constructor.newInstance(Constructor.java:490) [?:?]
    at com.networknt.schema.ValidatorTypeCode.newValidator(ValidatorTypeCode.java:131) [json-schema-validator-1.0.57.jar:?]
    at com.networknt.schema.JsonMetaSchema.newValidator(JsonMetaSchema.java:388) [json-schema-validator-1.0.57.jar:?]
    at com.networknt.schema.ValidationContext.newValidator(ValidationContext.java:57) [json-schema-validator-1.0.57.jar:?]
    at com.networknt.schema.JsonSchema.read(JsonSchema.java:223) [json-schema-validator-1.0.57.jar:?]
    at com.networknt.schema.JsonSchema.getValidators(JsonSchema.java:412) [json-schema-validator-1.0.57.jar:?]
    at com.networknt.schema.JsonSchema.validate(JsonSchema.java:245) [json-schema-validator-1.0.57.jar:?]
    at com.networknt.schema.JsonSchemaRef.validate(JsonSchemaRef.java:38) [json-schema-validator-1.0.57.jar:?]
    at com.networknt.schema.RefValidator.validate(RefValidator.java:133) [json-schema-validator-1.0.57.jar:?]
    at com.networknt.schema.JsonSchema.validate(JsonSchema.java:247) [json-schema-validator-1.0.57.jar:?]
    at com.networknt.schema.PropertiesValidator.validate(PropertiesValidator.java:68) [json-schema-validator-1.0.57.jar:?]
    at com.networknt.schema.JsonSchema.validate(JsonSchema.java:247) [json-schema-validator-1.0.57.jar:?]
    at com.networknt.schema.PropertiesValidator.validate(PropertiesValidator.java:68) [json-schema-validator-1.0.57.jar:?]
    at com.networknt.schema.JsonSchema.validate(JsonSchema.java:247) [json-schema-validator-1.0.57.jar:?]
    at com.networknt.schema.BaseJsonValidator.validate(BaseJsonValidator.java:104) [json-schema-validator-1.0.57.jar:?]

Upvotes: 0

Views: 2380

Answers (1)

Ether
Ether

Reputation: 53996

If your schema files aren't network-addressable at the URIs you use in $refs, the evaluator needs to know how to find your files. You should consult the documentation for the tool you are using to find out how to pre-load these documents so it can use them to resolve those $refs.

Upvotes: 2

Related Questions