Skip to content

Commit 1b3a5d9

Browse files
author
yevhenii-nadtochii
committed
Handle field_name in runtime
1 parent 665b9d8 commit 1b3a5d9

File tree

4 files changed

+27
-19
lines changed

4 files changed

+27
-19
lines changed

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

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,18 +59,26 @@ final class RequiredFieldCheck {
5959
Optional<ConstraintViolation> perform() {
6060
var matches = alternatives.stream()
6161
.anyMatch(this::allPresent);
62+
if (matches) {
63+
return Optional.empty();
64+
}
65+
6266
var message = TemplateString.newBuilder()
6367
.setWithPlaceholders(ERROR_MESSAGE)
6468
.putPlaceholderValue(RULE_VALUE, optionValue)
6569
.build();
66-
return matches
67-
? Optional.empty()
68-
: Optional.of(ConstraintViolation.newBuilder()
69-
.setMessage(message)
70-
.setFieldPath(fieldPath())
71-
.setTypeName(typeName())
72-
.build()
73-
);
70+
var fieldPath = fieldPath();
71+
var violation = ConstraintViolation.newBuilder()
72+
.setMessage(message)
73+
.setTypeName(typeName());
74+
75+
if (fieldPath.getFieldNameCount() == 1) {
76+
violation.setFieldName(fieldPath.getFieldName(0));
77+
} else {
78+
violation.setFieldPath(fieldPath);
79+
}
80+
81+
return Optional.of(violation.build());
7482
}
7583

7684
private boolean allPresent(Alternative alternative) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ private static ConstraintViolation violatedSetOnce(FieldDeclaration declaration)
297297
.build();
298298
var violation = ConstraintViolation.newBuilder()
299299
.setMessage(message)
300-
.setFieldPath(declaration.name().asPath())
300+
.setFieldName(declaration.name().value())
301301
.setTypeName(declaration.declaringType().name().value())
302302
.build();
303303
return violation;

java-runtime/src/main/java/io/spine/validate/diags/ViolationText.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@
2828

2929
import com.google.protobuf.Message;
3030
import io.spine.annotation.Internal;
31-
import io.spine.base.Field;
3231
import io.spine.option.OptionsProto;
3332
import io.spine.validate.ConstraintViolation;
3433

3534
import java.util.Collection;
3635

3736
import static com.google.common.base.Preconditions.checkNotNull;
37+
import static io.spine.base.FieldPaths.getJoined;
3838
import static io.spine.string.Diags.backtick;
3939
import static io.spine.validate.TemplateStringExtsKt.format;
4040
import static java.lang.System.lineSeparator;
@@ -86,14 +86,12 @@ public String toString() {
8686

8787
private StringBuilder buildMessage() {
8888
var typeName = violation.getTypeName();
89-
var path = violation.getFieldPath();
90-
var fieldPath = path.getFieldNameCount() == 0
91-
? ""
92-
: Field.withPath(path).toString();
89+
var field = violation.hasFieldName() ? violation.getFieldName()
90+
: getJoined(violation.getFieldPath());
9391
var formattedMessage = format(violation.getMessage());
9492
var result = new StringBuilder();
9593
appendPrefix(result, typeName);
96-
appendPrefix(result, fieldPath);
94+
appendPrefix(result, field);
9795
result.append(formattedMessage);
9896
return result;
9997
}

java-runtime/src/main/kotlin/io/spine/validate/MessageValidator.kt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ package io.spine.validate
2929
import com.google.common.collect.Range
3030
import com.google.protobuf.Descriptors
3131
import com.google.protobuf.Message
32-
import io.spine.base.Field.named
3332
import io.spine.code.proto.FieldContext
3433
import io.spine.code.proto.FieldDeclaration
3534
import io.spine.code.proto.FieldName
@@ -185,12 +184,11 @@ internal class MessageValidator private constructor(private val validatedMessage
185184
val noneSet = fieldValue.isEmpty
186185
if (noneSet) {
187186
val oneofName = constraint.oneofName()
188-
val oneofField = named(oneofName.value())
189187
val targetType = constraint.targetType()
190188
violations.add(
191189
constraintViolation {
192190
message = constraint.errorMessage(validatedMessage.context())
193-
fieldPath = oneofField.path()
191+
fieldName = oneofName.value
194192
typeName = targetType.name().value
195193
}
196194
)
@@ -266,8 +264,12 @@ private fun violation(
266264
val typeName = constraint.targetType().name()
267265
val violation = ConstraintViolation.newBuilder()
268266
.setMessage(constraint.errorMessage(context))
269-
.setFieldPath(fieldPath)
270267
.setTypeName(typeName.value())
268+
if (fieldPath.fieldNameCount == 1) {
269+
violation.setFieldName(fieldPath.getFieldName(0))
270+
} else {
271+
violation.setFieldPath(fieldPath)
272+
}
271273
if (violatingValue != null) {
272274
violation.setFieldValue(toFieldValue(violatingValue))
273275
}

0 commit comments

Comments
 (0)