Skip to content

Commit

Permalink
toggle format assertions (#91)
Browse files Browse the repository at this point in the history
Co-authored-by: Andy Coates <[email protected]>
  • Loading branch information
harrel56 and big-andy-coates authored Jan 10, 2024
1 parent 2f8ccb6 commit 511bd8d
Showing 1 changed file with 20 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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<String, JsonNode> remotes =
additionalSchemas.remotes().entrySet().stream()
Expand All @@ -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;
Expand Down

0 comments on commit 511bd8d

Please sign in to comment.