json-schema-validator icon indicating copy to clipboard operation
json-schema-validator copied to clipboard

UnsupportedOperationException: No suitable validator for id

Open iMacTia opened this issue 3 years ago • 6 comments

I'm currently seeing the following error:

java.lang.UnsupportedOperationException: No suitable validator for id
	at com.networknt.schema.ValidatorTypeCode.newValidator(ValidatorTypeCode.java:124) ~[json-schema-validator-1.0.42.jar:?]
	at com.networknt.schema.JsonMetaSchema.newValidator(JsonMetaSchema.java:342) ~[json-schema-validator-1.0.42.jar:?]
	at com.networknt.schema.ValidationContext.newValidator(ValidationContext.java:53) ~[json-schema-validator-1.0.42.jar:?]
	at com.networknt.schema.JsonSchema.read(JsonSchema.java:198) ~[json-schema-validator-1.0.42.jar:?]
	at com.networknt.schema.JsonSchema.initialize(JsonSchema.java:76) ~[json-schema-validator-1.0.42.jar:?]
	at com.networknt.schema.PropertiesValidator.<init>(PropertiesValidator.java:36) ~[json-schema-validator-1.0.42.jar:?]

While trying to JsonSchemaFactory.getSchema with the following schema:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "required": [
    "id"
  ],
  "properties": {
    "id": {
      "type": "string"
    },
    "attributes": {
      "type": "object",
      "properties": {
        "createdAt": {
          "type": "string",
          "format": "date-time"
        },
        "disabled": {
          "type": "boolean"
        },
        "email": {
          "type": "string"
        },
        "handle": {
          "type": "string"
        },
        "icon": {
          "type": "string"
        },
        "modifiedAt": {
          "type": "string",
          "format": "date-time"
        },
        "name": {
          "type": "string"
        },
        "serviceAccount": {
          "type": "boolean"
        },
        "status": {
          "type": "string"
        },
        "title": {
          "type": "string"
        },
        "verified": {
          "type": "boolean"
        }
      }
    },
    "relationships": {
      "type": "object",
      "properties": {
        "org": {
          "type": "object",
          "required": [
            "data"
          ],
          "properties": {
            "data": {
              "type": "object",
              "required": [
                "id"
              ],
              "properties": {
                "id": {
                  "type": "string"
                }
              }
            }
          }
        }
      }
    }
  }
}

I'm trying to understand what I might be doing wrong, but I'm failing to understand the logic behind these lines of code. I'm not sure what's the point of defining these ValidatorTypeCodes with a null validator? Is it actually possible to have id anywhere in my JSON schema if there's no available validator for it?

iMacTia avatar Jul 15 '22 14:07 iMacTia

id is a keyword in JSON schema.

stevehu avatar Jul 16 '22 17:07 stevehu

As far as I know $id is a keyword, not "id". Is there a document you saw that specifies "id" is also a keyword? I checked the official spec and couldn't find it

iMacTia avatar Jul 16 '22 18:07 iMacTia

Hi, id was changed to $id in draft 6 (look at the end of this part of the documentation).

kmalski avatar Jul 16 '22 20:07 kmalski

Thank you @kmalski, that explains the confusion! So it looks to me like this should probably be fixed, but I'm unsure what the correct fix would be in this case. Should we just remove id from the list or rename it into $id? I'm scared the latter might potentially cause some surprises for people upgrading the package. @stevehu Let me know what you think and I'd be happy to open a PR 👍

iMacTia avatar Jul 17 '22 06:07 iMacTia

@iMacTia The library supports all specification versions in one codebase and that is the reason we have the issue. Do you have any idea to fix it in mind?

stevehu avatar Jul 17 '22 14:07 stevehu

I guess the issue is that ValidatorTypeCode applies to all versions then. A sensible solution might be to separate this out into version-specific enums. Since the JsonSchemaFactory knows which spec version we're working with, it shouldn't be hard to pass that information downstream so that JsonMetaSchema can load the correct keywords.

I had a closer look while trying to figure out the best approach, and I found we already have a similar mechanism: https://github.com/networknt/json-schema-validator/blob/4e7df5fac48050b26eee905a18aec4d26738385d/src/main/java/com/networknt/schema/Version6.java#L5

I'm trying to figure out why this is not working in my case 🤔

iMacTia avatar Jul 18 '22 09:07 iMacTia

I'm facing this issue. Is there any way that I can configure the validation to skip/ignore the id field if any?

sgomezvillamor avatar Nov 04 '22 12:11 sgomezvillamor

It appears that this was fixed somewhere along the way as I cannot reproduce the issue with the provided example. I'm adding the unit-test to ensure we don't regress.

fdutton avatar Jun 11 '23 19:06 fdutton