|
16 | 16 |
|
17 | 17 | package org.creekservice.kafka.test.perf.implementations;
|
18 | 18 |
|
| 19 | +import static org.creekservice.kafka.test.perf.testsuite.SchemaSpec.DRAFT_07; |
19 | 20 | import static org.creekservice.kafka.test.perf.testsuite.SchemaSpec.DRAFT_2019_09;
|
20 | 21 | import static org.creekservice.kafka.test.perf.testsuite.SchemaSpec.DRAFT_2020_12;
|
21 | 22 |
|
22 | 23 | import com.fasterxml.jackson.core.JsonProcessingException;
|
23 | 24 | import com.fasterxml.jackson.databind.ObjectMapper;
|
24 | 25 | import com.fasterxml.jackson.databind.json.JsonMapper;
|
| 26 | +import dev.harrel.jsonschema.Dialect; |
25 | 27 | import dev.harrel.jsonschema.Dialects;
|
26 | 28 | import dev.harrel.jsonschema.FormatEvaluatorFactory;
|
27 | 29 | import dev.harrel.jsonschema.JsonNode;
|
28 | 30 | import dev.harrel.jsonschema.SchemaResolver;
|
29 |
| -import dev.harrel.jsonschema.SpecificationVersion; |
30 | 31 | import dev.harrel.jsonschema.Validator;
|
31 | 32 | import dev.harrel.jsonschema.ValidatorFactory;
|
32 | 33 | import dev.harrel.jsonschema.providers.JacksonNode;
|
33 | 34 | import java.awt.Color;
|
34 | 35 | import java.io.IOException;
|
35 | 36 | import java.net.URI;
|
36 | 37 | import java.util.Map;
|
37 |
| -import java.util.Set; |
38 | 38 | import java.util.stream.Collectors;
|
39 | 39 | import org.creekservice.kafka.test.perf.model.TestModel;
|
40 | 40 | import org.creekservice.kafka.test.perf.testsuite.AdditionalSchemas;
|
41 | 41 | import org.creekservice.kafka.test.perf.testsuite.SchemaSpec;
|
42 | 42 |
|
43 | 43 | @SuppressWarnings("FieldMayBeFinal") // not final to avoid folding.
|
44 | 44 | public class DevHarrelImplementation implements Implementation {
|
| 45 | + private static final Map<SchemaSpec, Dialect> SUPPORTED = |
| 46 | + Map.of( |
| 47 | + DRAFT_07, new Dialects.Draft7Dialect(), |
| 48 | + DRAFT_2019_09, new Dialects.Draft2019Dialect(), |
| 49 | + DRAFT_2020_12, new Dialects.Draft2020Dialect()); |
45 | 50 |
|
46 | 51 | private static final MetaData METADATA =
|
47 | 52 | new MetaData(
|
48 | 53 | "json-schema (dev.harrel)",
|
49 | 54 | "DevHarrel",
|
50 | 55 | Language.Java,
|
51 | 56 | Licence.MIT,
|
52 |
| - Set.of(DRAFT_2020_12, DRAFT_2019_09), |
| 57 | + SUPPORTED.keySet(), |
53 | 58 | "https://github.com/harrel56/json-schema",
|
54 | 59 | new Color(22, 99, 0),
|
55 | 60 | dev.harrel.jsonschema.ValidatorFactory.class,
|
@@ -133,37 +138,21 @@ private Validator validator(
|
133 | 138 | }
|
134 | 139 | return SchemaResolver.Result.empty();
|
135 | 140 | };
|
136 |
| - switch (spec) { |
137 |
| - case DRAFT_2020_12: |
138 |
| - final ValidatorFactory validatorFactory2020 = |
139 |
| - new ValidatorFactory() |
140 |
| - .withDisabledSchemaValidation(true) |
141 |
| - .withDialect(new Dialects.Draft2020Dialect()) |
142 |
| - .withJsonNodeFactory(nodeFactory) |
143 |
| - .withSchemaResolver(resolver); |
144 |
| - if (enableFormatAssertions) { |
145 |
| - validatorFactory2020.withEvaluatorFactory(new FormatEvaluatorFactory()); |
146 |
| - } |
147 |
| - final Validator validator2020 = validatorFactory2020.createValidator(); |
148 |
| - /* Validate against meta-schema in order to parse it eagerly */ |
149 |
| - validator2020.validate(URI.create(SpecificationVersion.DRAFT2020_12.getId()), "{}"); |
150 |
| - return validator2020; |
151 |
| - case DRAFT_2019_09: |
152 |
| - final ValidatorFactory validatorFactory2019 = |
153 |
| - new ValidatorFactory() |
154 |
| - .withDisabledSchemaValidation(true) |
155 |
| - .withDialect(new Dialects.Draft2019Dialect()) |
156 |
| - .withJsonNodeFactory(nodeFactory) |
157 |
| - .withSchemaResolver(resolver); |
158 |
| - if (enableFormatAssertions) { |
159 |
| - validatorFactory2019.withEvaluatorFactory(new FormatEvaluatorFactory()); |
160 |
| - } |
161 |
| - final Validator validator2019 = validatorFactory2019.createValidator(); |
162 |
| - /* Validate against meta-schema in order to parse it eagerly */ |
163 |
| - validator2019.validate(URI.create(SpecificationVersion.DRAFT2019_09.getId()), "{}"); |
164 |
| - return validator2019; |
165 |
| - default: |
166 |
| - throw new RuntimeException("Unsupported Spec:" + spec); |
| 141 | + final Dialect dialect = SUPPORTED.get(spec); |
| 142 | + if (dialect == null) { |
| 143 | + throw new RuntimeException("Unsupported Spec:" + spec); |
| 144 | + } |
| 145 | + final ValidatorFactory validatorFactory = |
| 146 | + new ValidatorFactory() |
| 147 | + .withDefaultDialect(dialect) |
| 148 | + .withJsonNodeFactory(nodeFactory) |
| 149 | + .withSchemaResolver(resolver); |
| 150 | + if (enableFormatAssertions) { |
| 151 | + validatorFactory.withEvaluatorFactory(new FormatEvaluatorFactory()); |
167 | 152 | }
|
| 153 | + final Validator validator = validatorFactory.createValidator(); |
| 154 | + /* Validate against meta-schema in order to parse it eagerly */ |
| 155 | + validator.validate(spec.uri(), "{}"); |
| 156 | + return validator; |
168 | 157 | }
|
169 | 158 | }
|
0 commit comments