Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validation error for "id" field in referenced schema #1093

Open
gourav157537 opened this issue Jul 23, 2024 · 1 comment
Open

Validation error for "id" field in referenced schema #1093

gourav157537 opened this issue Jul 23, 2024 · 1 comment

Comments

@gourav157537
Copy link

gourav157537 commented Jul 23, 2024

Library is giving error java.lang.UnsupportedOperationException: No suitable validator for id
PFA java class to reproduce issue version 1.4.0 and 1.5.0. Issue is not produced in old version of library version 1.0.86
New folder.zip
Run attached java class issue will be reproduced, replace line 81 with main1.json, and notice data validated without id field in ref schema
StackTrace
Exception in thread "main" com.networknt.schema.JsonSchemaException: java.lang.UnsupportedOperationException: No suitable validator for id
at com.networknt.schema.JsonMetaSchema.newValidator(JsonMetaSchema.java:508)
at com.networknt.schema.ValidationContext.newValidator(ValidationContext.java:63)
at com.networknt.schema.JsonSchema.read(JsonSchema.java:545)
at com.networknt.schema.JsonSchema.getValidators(JsonSchema.java:1325)
at com.networknt.schema.JsonSchema.(JsonSchema.java:151)
at com.networknt.schema.JsonSchema.from(JsonSchema.java:66)
at com.networknt.schema.JsonSchemaFactory.doCreate(JsonSchemaFactory.java:369)
at com.networknt.schema.JsonSchemaFactory.create(JsonSchemaFactory.java:365)
at com.networknt.schema.ValidationContext.newSchema(ValidationContext.java:58)
at com.networknt.schema.NonValidationKeyword$Validator.(NonValidationKeyword.java:46)
at com.networknt.schema.NonValidationKeyword.newValidator(NonValidationKeyword.java:66)
at com.networknt.schema.JsonMetaSchema.newValidator(JsonMetaSchema.java:496)
at com.networknt.schema.ValidationContext.newValidator(ValidationContext.java:63)
at com.networknt.schema.JsonSchema.read(JsonSchema.java:545)
at com.networknt.schema.JsonSchema.getValidators(JsonSchema.java:1325)
at com.networknt.schema.JsonSchema.(JsonSchema.java:151)
at com.networknt.schema.JsonSchema.from(JsonSchema.java:66)
at com.networknt.schema.JsonSchemaFactory.doCreate(JsonSchemaFactory.java:369)
at com.networknt.schema.JsonSchemaFactory.newJsonSchema(JsonSchemaFactory.java:334)
at com.networknt.schema.JsonSchemaFactory.getSchema(JsonSchemaFactory.java:526)
at com.networknt.schema.JsonSchemaFactory.getSchema(JsonSchemaFactory.java:509)
at NetworkNtIssue.main(NetworkNtIssue.java:80)
Caused by: java.lang.UnsupportedOperationException: No suitable validator for id
at com.networknt.schema.ValidatorTypeCode.newValidator(ValidatorTypeCode.java:161)
at com.networknt.schema.JsonMetaSchema.newValidator(JsonMetaSchema.java:496)
... 21 more

@justin-tay
Copy link
Contributor

This is intentional behavior to catch bugs as previously in Draft 4 unique identifiers are declared using the id but since Draft 6 it has now changed to $id. See https://json-schema.org/understanding-json-schema/basics#declaring-a-unique-identifier. There is special handling for unique identifiers and if you are using id instead of $id you may experience issues that are hard to debug.

For users who really legitimately have a custom id keyword, which I think is the intent of moving the unique identifier to $id, then this id keyword needs to be explicitly declared instead of being an unknown keyword.

        JsonMetaSchema metaSchema = JsonMetaSchema.builder(JsonMetaSchema.getV202012())
                .keyword(new AnnotationKeyword("id")).build();
        JsonSchemaFactory schemaFactory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V202012,
                builder -> builder.metaSchema(metaSchema));

This isn't what is happening here though, this looks like you aren't using the $defs keyword properly.

If you look again at your schema below, you have definitions under $defs. This means that definitions is a subschema and anything under that are keywords which means that you are using the id keyword. I'm actually not quite sure what is the intent here as I see the use of $schema which indicates that this is supposed to be a schema resource but this really doesn't look right as then I legitimately expect a $id but you have a id keyword that is obviously not a unique identifier.

                {
                  "$schema": "http://json-schema.org/draft/2019-09/schema",
                  "$defs": {
                    "definitions": {
                      "$schema": "http://json-schema.org/draft/2020-12/schema",
                      "type": "object",
                      "id": {
                        "description": "Unique identifier",
                        "type": "string",
                        "minLength": 5,
                        "maxLength": 100
                      }
                    }
                  },
                  "properties": {
                    "id": {
                      "description": "Unique identifier of an account as provided by the FI",
                      "dataPrivacyLevel": "sensitive",
                      "label": "Account ID",
                      "$ref": "#/$defs/definitions/id"
                    }
                  }
                }

Not knowing your intent, I would just suggest to remove definitions and the $schema and type children.

                {
                  "$schema": "http://json-schema.org/draft/2019-09/schema",
                  "$defs": {
                      "id": {
                        "description": "Unique identifier",
                        "type": "string",
                        "minLength": 5,
                        "maxLength": 100
                      }
                  },
                  "properties": {
                    "id": {
                      "description": "Unique identifier of an account as provided by the FI",
                      "dataPrivacyLevel": "sensitive",
                      "label": "Account ID",
                      "$ref": "#/$defs/id"
                    }
                  }
                }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants