From 1ea0696429f66d19f8c61931b09354d46565a8dd Mon Sep 17 00:00:00 2001 From: yevhenii-nadtochii Date: Fri, 20 Dec 2024 17:45:55 +0100 Subject: [PATCH] Fix compilation of the old validation --- .../io/spine/validate/MessageValidator.java | 23 ++++++++++--------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/java-runtime/src/main/java/io/spine/validate/MessageValidator.java b/java-runtime/src/main/java/io/spine/validate/MessageValidator.java index e94f6b5b..aef05246 100644 --- a/java-runtime/src/main/java/io/spine/validate/MessageValidator.java +++ b/java-runtime/src/main/java/io/spine/validate/MessageValidator.java @@ -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); } @@ -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)); } } @@ -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(); @@ -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) { @@ -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(); + } }