networknt / json-schema-validator

A fast Java JSON schema validator that supports draft V4, V6, V7, V2019-09 and V2020-12
Apache License 2.0
807 stars 320 forks source link

Java Json Schema 'Unknown keyword else - you should define your own meta schema.' #992

Closed jksevend-trimble closed 4 months ago

jksevend-trimble commented 4 months ago

Hello, I setup version 1.0.72 of your validator package (really great btw) in a spring boot application to validate incoming JSON requests. Validation works, what confuses me is a warning

com.networknt.schema.JsonMetaSchema :  :  Unknown keyword else - you should define your own Meta Schema. If the keyword is irrelevant for validation, just use a NonValidationKeyword

I am instantiating the schema factory for the 2020-12 draft:

final JsonSchemaFactory schemaFactory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V202012);

and the if/then/else object:

...

   "if":{
      "properties":{
         "key":{
            "const":"api_usage_count"
         }
      }
   },
   "then":{
      "properties":{
         "value":{
            "type":"number"
         }
      }
   },
   "else":false

Now I experimented with saying "else": {} or "else": false (since i do not really need the else block in my particular use case) but omitting the else block completely by not defining it results in a JsonParseException from Jackson.

Is there any way to either fix the warning, or not define the else block at all without the JsonParseException to be thrown?

justin-tay commented 4 months ago

The parse exception is not related to this library, you just have invalid JSON, for instance you may have an extra comma.

The else issue is a bug, but fixes will only be applied to the latest version, so you will need to replicate the issue with the latest version. In this particular case this was already fixed in 1.0.73, but you probably should be using the latest version, because a lot of issues have been fixed since then.