From 511bd8d5cb66c4e5a15a60eca191c2a1b46ad05f Mon Sep 17 00:00:00 2001 From: harrel56 Date: Wed, 10 Jan 2024 23:21:38 +0100 Subject: [PATCH] toggle format assertions (#91) Co-authored-by: Andy Coates <8012398+big-andy-coates@users.noreply.github.com> --- .../DevHarrelImplementation.java | 37 ++++++++++--------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/src/main/java/org/creekservice/kafka/test/perf/implementations/DevHarrelImplementation.java b/src/main/java/org/creekservice/kafka/test/perf/implementations/DevHarrelImplementation.java index 2d14410..db8a764 100644 --- a/src/main/java/org/creekservice/kafka/test/perf/implementations/DevHarrelImplementation.java +++ b/src/main/java/org/creekservice/kafka/test/perf/implementations/DevHarrelImplementation.java @@ -28,6 +28,7 @@ import dev.harrel.jsonschema.SchemaResolver; import dev.harrel.jsonschema.SpecificationVersion; import dev.harrel.jsonschema.Validator; +import dev.harrel.jsonschema.ValidatorFactory; import dev.harrel.jsonschema.providers.JacksonNode; import java.awt.Color; import java.io.IOException; @@ -68,12 +69,7 @@ public JsonValidator prepare( final AdditionalSchemas additionalSchemas, final boolean enableFormatAssertions) { - /* - Implementation does not seem to currently provide a way to programmatically turn on format assertions. - Instead, they seem to be on-by-default, which is not inline with the draft 2020-12 spec. - */ - - final Validator validator = validator(spec, additionalSchemas); + final Validator validator = validator(spec, additionalSchemas, enableFormatAssertions); final URI schemaUri = validator.registerSchema(schema); return new JsonValidator() { @@ -118,7 +114,10 @@ public TestModel deserialize(final byte[] data) { }; } - private Validator validator(final SchemaSpec spec, final AdditionalSchemas additionalSchemas) { + private Validator validator( + final SchemaSpec spec, + final AdditionalSchemas additionalSchemas, + final boolean enableFormatAssertions) { final JacksonNode.Factory nodeFactory = new JacksonNode.Factory(); final Map remotes = additionalSchemas.remotes().entrySet().stream() @@ -136,26 +135,30 @@ private Validator validator(final SchemaSpec spec, final AdditionalSchemas addit }; switch (spec) { case DRAFT_2020_12: - final Validator validator2020 = - new dev.harrel.jsonschema.ValidatorFactory() + final ValidatorFactory validatorFactory2020 = + new ValidatorFactory() .withDisabledSchemaValidation(true) .withDialect(new Dialects.Draft2020Dialect()) - .withEvaluatorFactory(new FormatEvaluatorFactory()) .withJsonNodeFactory(nodeFactory) - .withSchemaResolver(resolver) - .createValidator(); + .withSchemaResolver(resolver); + if (enableFormatAssertions) { + validatorFactory2020.withEvaluatorFactory(new FormatEvaluatorFactory()); + } + final Validator validator2020 = validatorFactory2020.createValidator(); /* Validate against meta-schema in order to parse it eagerly */ validator2020.validate(URI.create(SpecificationVersion.DRAFT2020_12.getId()), "{}"); return validator2020; case DRAFT_2019_09: - final Validator validator2019 = - new dev.harrel.jsonschema.ValidatorFactory() + final ValidatorFactory validatorFactory2019 = + new ValidatorFactory() .withDisabledSchemaValidation(true) .withDialect(new Dialects.Draft2019Dialect()) - .withEvaluatorFactory(new FormatEvaluatorFactory()) .withJsonNodeFactory(nodeFactory) - .withSchemaResolver(resolver) - .createValidator(); + .withSchemaResolver(resolver); + if (enableFormatAssertions) { + validatorFactory2019.withEvaluatorFactory(new FormatEvaluatorFactory()); + } + final Validator validator2019 = validatorFactory2019.createValidator(); /* Validate against meta-schema in order to parse it eagerly */ validator2019.validate(URI.create(SpecificationVersion.DRAFT2019_09.getId()), "{}"); return validator2019;