Skip to content

Commit

Permalink
Partially address deprecations
Browse files Browse the repository at this point in the history
  • Loading branch information
yevhenii-nadtochii committed Dec 20, 2024
1 parent 5dd7b76 commit e769ddc
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
final class RequiredFieldCheck {

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

private final MessageValue message;
private final ImmutableSet<Alternative> alternatives;
Expand All @@ -58,17 +58,22 @@ final class RequiredFieldCheck {
Optional<ConstraintViolation> perform() {
var matches = alternatives.stream()
.anyMatch(this::allPresent);
var message = TemplateString.newBuilder()
.setWithPlaceholders(ERROR_MESSAGE)
.putPlaceholderValue("rule", optionValue)
.build();
return matches
? Optional.empty()
: Optional.of(ConstraintViolation.newBuilder()
.setMsgFormat(ERROR_MESSAGE)
.setMessage(message)
.setFieldPath(fieldPath())
.setTypeName(typeName())
.addParam(optionValue)
.build()
);
}



private boolean allPresent(Alternative alternative) {
for (var declaration : alternative.fields()) {
var fieldName = declaration.name().value();
Expand Down
13 changes: 9 additions & 4 deletions java-runtime/src/main/java/io/spine/validate/Validate.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@
*/
public final class Validate {

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

/** Prevents instantiation of this utility class. */
Expand Down Expand Up @@ -284,11 +287,13 @@ private static void onSetOnceMisuse(FieldDeclaration field) {
private static ConstraintViolation violatedSetOnce(FieldDeclaration declaration) {
var declaringTypeName = declaration.declaringType().name().value();
var fieldName = declaration.name().value();
var message = TemplateString.newBuilder()
.setWithPlaceholders(SET_ONCE_ERROR_MESSAGE)
.putPlaceholderValue("parent.type", declaringTypeName)
.putPlaceholderValue("field.name", fieldName)
.build();
var violation = ConstraintViolation.newBuilder()
.setMsgFormat("Attempted to change the value of the field `%s.%s` which has " +
"`(set_once) = true` and is already set.")
.addParam(declaringTypeName)
.addParam(fieldName)
.setMessage(message)
.setFieldPath(declaration.name().asPath())
.setTypeName(declaration.declaringType().name().value())
.build();
Expand Down

0 comments on commit e769ddc

Please sign in to comment.