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: Validation fails on keyword 'uuid' #939

Closed rainer-wei closed 7 months ago

rainer-wei commented 7 months ago

The following code example fails with 1.3.0:

import com.networknt.schema.InputFormat;
import com.networknt.schema.JsonSchemaFactory;
import com.networknt.schema.SpecVersion;
import org.junit.jupiter.api.Assertions;

import java.util.UUID;

public class JsonSchemaValidatorTest {
  public static void main(String[] args) {
    var schemaString = """
        {
          "$schema": "http://json-schema.org/draft-07/schema#",
          "type": "object",
          "additionalProperties": false,
          "required": [
            "someUuid"
          ],
          "properties": {
            "someUuid": {
              "$ref": "#/definitions/uuid"
            }
          },
          "definitions": {
            "uuid": {
              "type": "string",
              "pattern": "^[0-9a-f]{8}(\\\\-[0-9a-f]{4}){3}\\\\-[0-9a-f]{12}$",
              "minLength": 36,
              "maxLength": 36
            }
          }
        }

              """;
    var factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V7);
    var jsonSchema = factory.getSchema(schemaString);
    var json = "{\"someUuid\": \"" + UUID.randomUUID() + "\"}";
    var validationMessages = jsonSchema.validate(json, InputFormat.JSON);
    Assertions.assertTrue(validationMessages.isEmpty());
  }
}
Caused by: java.lang.UnsupportedOperationException: No suitable validator for uuid
    at com.networknt.schema.ValidatorTypeCode.newValidator(ValidatorTypeCode.java:161)
    at com.networknt.schema.JsonMetaSchema.newValidator(JsonMetaSchema.java:323)
    ... 25 more

Renaming the definition reference to something like uuid-string can be used as workaround.

justin-tay commented 7 months ago

This will be fixed in the next version.