File tree Expand file tree Collapse file tree 2 files changed +17
-7
lines changed
java-runtime/src/main/java/io/spine/validate Expand file tree Collapse file tree 2 files changed +17
-7
lines changed Original file line number Diff line number Diff line change 4141final 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 ();
Original file line number Diff line number Diff line change 5656 */
5757public 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 ();
You can’t perform that action at this time.
0 commit comments