Skip to content

Commit e769ddc

Browse files
author
yevhenii-nadtochii
committed
Partially address deprecations
1 parent 5dd7b76 commit e769ddc

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

java-runtime/src/main/java/io/spine/validate/RequiredFieldCheck.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
final class RequiredFieldCheck {
4242

4343
private static final String ERROR_MESSAGE =
44-
"Message must have all the required fields set according to the rule: `%s`.";
44+
"Message must have all the required fields set according to the rule: `${rule}`.";
4545

4646
private final MessageValue message;
4747
private final ImmutableSet<Alternative> alternatives;
@@ -58,17 +58,22 @@ final class RequiredFieldCheck {
5858
Optional<ConstraintViolation> perform() {
5959
var matches = alternatives.stream()
6060
.anyMatch(this::allPresent);
61+
var message = TemplateString.newBuilder()
62+
.setWithPlaceholders(ERROR_MESSAGE)
63+
.putPlaceholderValue("rule", optionValue)
64+
.build();
6165
return matches
6266
? Optional.empty()
6367
: Optional.of(ConstraintViolation.newBuilder()
64-
.setMsgFormat(ERROR_MESSAGE)
68+
.setMessage(message)
6569
.setFieldPath(fieldPath())
6670
.setTypeName(typeName())
67-
.addParam(optionValue)
6871
.build()
6972
);
7073
}
7174

75+
76+
7277
private boolean allPresent(Alternative alternative) {
7378
for (var declaration : alternative.fields()) {
7479
var fieldName = declaration.name().value();

java-runtime/src/main/java/io/spine/validate/Validate.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@
5656
*/
5757
public final class Validate {
5858

59+
private static final String SET_ONCE_ERROR_MESSAGE =
60+
"Attempted to change the value of the field `${parent.type}.${field.name}` which has " +
61+
"`(set_once) = true` and already has a non-default value.";
5962
private static final Logger<?> logger = LoggingFactory.forEnclosingClass();
6063

6164
/** Prevents instantiation of this utility class. */
@@ -284,11 +287,13 @@ private static void onSetOnceMisuse(FieldDeclaration field) {
284287
private static ConstraintViolation violatedSetOnce(FieldDeclaration declaration) {
285288
var declaringTypeName = declaration.declaringType().name().value();
286289
var fieldName = declaration.name().value();
290+
var message = TemplateString.newBuilder()
291+
.setWithPlaceholders(SET_ONCE_ERROR_MESSAGE)
292+
.putPlaceholderValue("parent.type", declaringTypeName)
293+
.putPlaceholderValue("field.name", fieldName)
294+
.build();
287295
var violation = ConstraintViolation.newBuilder()
288-
.setMsgFormat("Attempted to change the value of the field `%s.%s` which has " +
289-
"`(set_once) = true` and is already set.")
290-
.addParam(declaringTypeName)
291-
.addParam(fieldName)
296+
.setMessage(message)
292297
.setFieldPath(declaration.name().asPath())
293298
.setTypeName(declaration.declaringType().name().value())
294299
.build();

0 commit comments

Comments
 (0)