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
822 stars 323 forks source link

Since 1.3.0: UUID not correctly validated #937

Closed itineric closed 3 months ago

itineric commented 7 months ago

Consider the following schema:

{
  "type": "object",
  "properties":
  {
    "id":
    {
      "type": "string",
      "format": "uuid"
    }
  }
}

Using UUID as defined here: https://json-schema.org/draft/2019-09/json-schema-validation#rfc.section.7.3.5

Validating the following JSON does not produce an error as expected (with version 1.2.0 it worked)

{
  "id": "not an UUID"
}
justin-tay commented 7 months ago

In version 1.3.0 the format keyword by default complies with the specification that it should be an annotation since 2019-09 and configuration needs to be set to always generate an assertion or the required vocabulary needs to be in the meta schema.

https://json-schema.org/draft/2020-12/json-schema-validation#name-vocabularies-for-semantic-c

itineric commented 7 months ago

what should I modify in the given example ? Sorry I dont get it.

justin-tay commented 7 months ago

You can set setFormatAssertionsEnabled on the SchemaValidatorsConfig or on a per execution basis.

Set<ValidationMessage> assertions = schema.validate(input, InputFormat.JSON, executionContext -> {
    // By default since Draft 2019-09 the format keyword only generates annotations and not assertions
    executionContext.getConfig().setFormatAssertionsEnabled(true);
});
rmuller commented 6 months ago

Should be closed, see #938