Skip to content

Commit

Permalink
Fix compilation of the old validation
Browse files Browse the repository at this point in the history
  • Loading branch information
yevhenii-nadtochii committed Dec 20, 2024
1 parent e769ddc commit 1ea0696
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions java-runtime/src/main/java/io/spine/validate/MessageValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,7 @@ public void visitPattern(PatternConstraint constraint) {
.filter(value -> partialMatch
? noPartialMatch(compiledPattern, (String) value)
: noCompleteMatch(compiledPattern, (String) value))
.map(value -> violation(constraint, fieldValue, value)
.toBuilder()
.addParam(regex)
.build())
.map(value -> violation(constraint, fieldValue, value))
.forEach(violations::add);
}

Expand Down Expand Up @@ -176,11 +173,7 @@ public void visitGoesWith(GoesConstraint constraint) {
);
var withField = declaration.get();
if (!value.isDefault() && fieldValueNotSet(withField)) {
var withFieldNotSet = violation(constraint, value).toBuilder()
.addParam(field.name().value())
.addParam(withFieldName)
.build();
violations.add(withFieldNotSet);
violations.add(violation(constraint, value));
}
}

Expand Down Expand Up @@ -226,7 +219,7 @@ public void visitRequiredOneof(IsRequiredConstraint constraint) {
var oneofField = Field.named(oneofName.value());
var targetType = constraint.targetType();
var violation = ConstraintViolation.newBuilder()
.setMsgFormat(constraint.errorMessage(message.context()))
.setMessage(toTemplate(constraint.errorMessage(message.context())))
.setFieldPath(oneofField.path())
.setTypeName(targetType.name().value())
.build();
Expand Down Expand Up @@ -347,7 +340,7 @@ private static ConstraintViolation violation(Constraint constraint,
var fieldPath = context.fieldPath();
var typeName = constraint.targetType().name();
var violation = ConstraintViolation.newBuilder()
.setMsgFormat(constraint.errorMessage(context))
.setMessage(toTemplate(constraint.errorMessage(context)))
.setFieldPath(fieldPath)
.setTypeName(typeName.value());
if (violatingValue != null) {
Expand All @@ -369,4 +362,12 @@ private static Any toFieldValue(Object violatingValue) {
}
return TypeConverter.toAny(violatingValue);
}

// Old error messages are incompatible with new ones.
// They will have not processed `%s` placeholders.
private static TemplateString toTemplate(String printfString) {
return TemplateString.newBuilder()
.setWithPlaceholders(printfString)
.build();
}
}

0 comments on commit 1ea0696

Please sign in to comment.