Skip to content

Commit c0f6b76

Browse files
author
yevhenii-nadtochii
committedFeb 3, 2025·
Simplify determining of the error message
1 parent 9e677a7 commit c0f6b76

File tree

1 file changed

+9
-16
lines changed

1 file changed

+9
-16
lines changed
 

‎model/src/main/kotlin/io/spine/validation/required/RequiredPolicy.kt

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ internal class RequiredPolicy : Policy<FieldOptionDiscovered>() {
8080
val field = event.subject
8181
val file = event.file
8282
checkFieldType(field, file)
83-
val message = determineErrorMessage(field, file)
83+
val message = determineErrorMessage(field)
8484
return if (event.option.boolValue) {
8585
accepted(field, message).asA()
8686
} else {
@@ -111,21 +111,14 @@ private fun checkFieldType(field: Field, file: File) {
111111

112112
// TODO:2025-01-31:yevhenii.nadtochii: Locally changed ProtoData.
113113
// `Field.optionList` is empty when it is part of `FieldOptionDiscovered` event.
114-
private fun determineErrorMessage(field: Field, file: File): String {
115-
val ifMissingOptions = field.optionList.filter { it.name == IF_MISSING }
116-
return when (ifMissingOptions.size) {
117-
0 -> DefaultErrorMessage.from(IfMissingOption.getDescriptor())
118-
1 -> {
119-
val companion = ifMissingOptions.first().value.unpack<IfMissingOption>()
120-
companion.errorMsg
121-
}
122-
123-
else -> {
124-
file.compilationError(field.span) {
125-
"The field `${field.qualifiedName}` is allowed to have zero or one " +
126-
"`($IF_MISSING)` companion option."
127-
}
128-
}
114+
// TODO:2025-02-03:yevhenii.nadtochii: No need to check the number of declarations.
115+
private fun determineErrorMessage(field: Field): String {
116+
val companion = field.optionList.find { it.name == IF_MISSING }
117+
return if (companion == null) {
118+
DefaultErrorMessage.from(IfMissingOption.getDescriptor())
119+
} else {
120+
companion.value.unpack<IfMissingOption>()
121+
.errorMsg
129122
}
130123
}
131124

0 commit comments

Comments
 (0)
Please sign in to comment.