Skip to content

Commit

Permalink
Add support for overriding validation severity
Browse files Browse the repository at this point in the history
severityOverride cannot set an event to SUPPRESSED. Use suppressions
for that. It cannot lower the severity of an event, only elevate. This
prevents a backdoor for disabling built-in validation or ignoring
ERROR events.

Closes #856
  • Loading branch information
mtdowling committed Jul 28, 2023
1 parent 8db9d56 commit 595b5ad
Show file tree
Hide file tree
Showing 27 changed files with 665 additions and 296 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -580,15 +580,18 @@ public ValidatedResult<Model> assemble() {
}

if (disableValidation) {
List<ValidationEventDecorator> decorators = validatorFactory.loadDecorators();
return new ValidatedResult<>(transformed, ModelValidator.decorateEvents(decorators, events));
}

try {
return validate(transformed, events);
} catch (SourceException e) {
events.add(ValidationEvent.fromSourceException(e));
ValidationEventDecorator decorator = ValidationEventDecorator.compose(validatorFactory.loadDecorators());
for (int idx = 0; idx < events.size(); idx++) {
events.set(idx, decorator.decorate(events.get(idx)));
}
return new ValidatedResult<>(transformed, events);
} else {
try {
return validate(transformed, events);
} catch (SourceException e) {
events.add(ValidationEvent.fromSourceException(e));
return new ValidatedResult<>(transformed, events);
}
}
}

Expand All @@ -599,22 +602,22 @@ private void addMetadataToProcessor(Map<String, Node> metadataMap, LoadOperation
}

private ValidatedResult<Model> returnOnlyErrors(Model model, List<ValidationEvent> events) {
List<ValidationEventDecorator> decorators = validatorFactory.loadDecorators();
ValidationEventDecorator decorator = ValidationEventDecorator.compose(validatorFactory.loadDecorators());
return new ValidatedResult<>(model, events.stream()
.filter(event -> event.getSeverity() == Severity.ERROR)
.map(event -> ModelValidator.decorateEvent(decorators, event))
.map(decorator::decorate)
.collect(Collectors.toList()));
}

private ValidatedResult<Model> validate(Model model, List<ValidationEvent> events) {
// Validate the model based on the explicit validators and model metadata.
// Note the ModelValidator handles emitting events to the validationEventListener.
List<ValidationEvent> mergedEvents = new ModelValidator()
List<ValidationEvent> mergedEvents = ModelValidator.builder()
.validators(validators)
.validatorFactory(validatorFactory)
.eventListener(validationEventListener)
.includeEvents(events)
.createValidator()
.build()
.validate(model);

return new ValidatedResult<>(model, mergedEvents);
Expand Down
Loading

0 comments on commit 595b5ad

Please sign in to comment.