Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use placeholder-based messages in ConstraintViolation #173

Draft
wants to merge 34 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
c8bdb16
Add `TemplateString` message
yevhenii-nadtochii Dec 20, 2024
b06022e
Add `TemplateString message` field to `ConstraintViolation`
yevhenii-nadtochii Dec 20, 2024
960b21e
Deprecate `printf` fields
yevhenii-nadtochii Dec 20, 2024
b6762e0
Proofread docs to `TemplateString`
yevhenii-nadtochii Dec 20, 2024
50fc482
Implement `TemplateString.format()` extension
yevhenii-nadtochii Dec 20, 2024
6373a7c
Implement tests for `TemplateString.format()`
yevhenii-nadtochii Dec 20, 2024
5dd7b76
Use a new format when stringifying violations
yevhenii-nadtochii Dec 20, 2024
e769ddc
Partially address deprecations
yevhenii-nadtochii Dec 20, 2024
1ea0696
Fix compilation of the old validation
yevhenii-nadtochii Dec 20, 2024
edce8ac
Address deprecations in runtime tests
yevhenii-nadtochii Dec 23, 2024
bb4d1d6
Address more deprecations
yevhenii-nadtochii Dec 23, 2024
f66b628
Make `Constraint` expect `TemplateString`
yevhenii-nadtochii Dec 23, 2024
f8e4aef
Rollback `Constraint` expect `String`, and provide a default method t…
yevhenii-nadtochii Dec 23, 2024
7f6917f
Make `checkPlaceholdersHasValue` public
yevhenii-nadtochii Dec 23, 2024
280a92a
Take the latest ProtoData
yevhenii-nadtochii Dec 23, 2024
6a94357
Make `set_once` create instances of `TemplateString`
yevhenii-nadtochii Dec 23, 2024
516131d
Adapt other validation tests
yevhenii-nadtochii Dec 23, 2024
065dbae
Escape `$` for JavaPoet
yevhenii-nadtochii Dec 23, 2024
7da8ead
Fix Detekt warnings
yevhenii-nadtochii Dec 23, 2024
5ec030d
Document the method
yevhenii-nadtochii Dec 24, 2024
df8f057
Keep the passed values in `TemplateString`
yevhenii-nadtochii Dec 24, 2024
fa32c4c
Remove extra empty lines
yevhenii-nadtochii Dec 24, 2024
a207c7f
Move `TemplateString` to a separate file
yevhenii-nadtochii Dec 24, 2024
4d66f36
Cover a public method with tests
yevhenii-nadtochii Dec 24, 2024
b9a9dc6
Rollback field renaming
yevhenii-nadtochii Dec 24, 2024
b5e67c8
Prefer assertions upon the formatted template
yevhenii-nadtochii Dec 24, 2024
a0b82ca
Move the most important argument to the first position
yevhenii-nadtochii Dec 24, 2024
d4c5d59
Add more details to docs
yevhenii-nadtochii Dec 24, 2024
5f07afd
Add possibility for unsafe formatting
yevhenii-nadtochii Dec 24, 2024
69c21c8
Try out new ProtoData API
yevhenii-nadtochii Dec 24, 2024
8cbe942
Put a missing white space
yevhenii-nadtochii Dec 24, 2024
63af9cd
Use unsafe formatting (functionality not implemented yet)
yevhenii-nadtochii Dec 24, 2024
ecf199f
Take the latest ProtoData
yevhenii-nadtochii Dec 24, 2024
0307a37
Bump the version -> `2.0.0-SNAPSHOT.182`
yevhenii-nadtochii Dec 24, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ object ProtoData {
* The version of ProtoData dependencies.
*/
val version: String
private const val fallbackVersion = "0.80.3"
private const val fallbackVersion = "0.80.5"

/**
* The distinct version of ProtoData used by other build tools.
Expand All @@ -82,7 +82,7 @@ object ProtoData {
* transitional dependencies, this is the version used to build the project itself.
*/
val dogfoodingVersion: String
private const val fallbackDfVersion = "0.80.3"
private const val fallbackDfVersion = "0.80.5"

/**
* The artifact for the ProtoData Gradle plugin.
Expand Down
56 changes: 28 additions & 28 deletions dependencies.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@


# Dependencies of `io.spine.validation:spine-validation-java:2.0.0-SNAPSHOT.181`
# Dependencies of `io.spine.validation:spine-validation-java:2.0.0-SNAPSHOT.182`

## Runtime
1. **Group** : com.fasterxml.jackson. **Name** : jackson-bom. **Version** : 2.15.3.
Expand Down Expand Up @@ -848,12 +848,12 @@

The dependencies distributed under several licenses, are used according their commercial-use-friendly license.

This report was generated on **Wed Dec 18 18:28:44 WET 2024** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE).
This report was generated on **Tue Dec 24 20:20:40 CET 2024** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE).




# Dependencies of `io.spine.validation:spine-validation-java-bundle:2.0.0-SNAPSHOT.181`
# Dependencies of `io.spine.validation:spine-validation-java-bundle:2.0.0-SNAPSHOT.182`

## Runtime
1. **Group** : org.jetbrains. **Name** : annotations. **Version** : 24.0.1.
Expand Down Expand Up @@ -1448,12 +1448,12 @@ This report was generated on **Wed Dec 18 18:28:44 WET 2024** using [Gradle-Lice

The dependencies distributed under several licenses, are used according their commercial-use-friendly license.

This report was generated on **Wed Dec 18 18:28:44 WET 2024** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE).
This report was generated on **Tue Dec 24 20:20:40 CET 2024** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE).




# Dependencies of `io.spine.validation:spine-validation-java-runtime:2.0.0-SNAPSHOT.181`
# Dependencies of `io.spine.validation:spine-validation-java-runtime:2.0.0-SNAPSHOT.182`

## Runtime
1. **Group** : com.google.code.findbugs. **Name** : jsr305. **Version** : 3.0.2.
Expand Down Expand Up @@ -2110,12 +2110,12 @@ This report was generated on **Wed Dec 18 18:28:44 WET 2024** using [Gradle-Lice

The dependencies distributed under several licenses, are used according their commercial-use-friendly license.

This report was generated on **Wed Dec 18 18:28:44 WET 2024** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE).
This report was generated on **Tue Dec 24 20:20:41 CET 2024** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE).




# Dependencies of `io.spine.validation:spine-validation-java-tests:2.0.0-SNAPSHOT.181`
# Dependencies of `io.spine.validation:spine-validation-java-tests:2.0.0-SNAPSHOT.182`

## Runtime
1. **Group** : com.fasterxml.jackson. **Name** : jackson-bom. **Version** : 2.15.3.
Expand Down Expand Up @@ -3031,12 +3031,12 @@ This report was generated on **Wed Dec 18 18:28:44 WET 2024** using [Gradle-Lice

The dependencies distributed under several licenses, are used according their commercial-use-friendly license.

This report was generated on **Wed Dec 18 18:28:45 WET 2024** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE).
This report was generated on **Tue Dec 24 20:20:43 CET 2024** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE).




# Dependencies of `io.spine.validation:spine-validation-model:2.0.0-SNAPSHOT.181`
# Dependencies of `io.spine.validation:spine-validation-model:2.0.0-SNAPSHOT.182`

## Runtime
1. **Group** : com.fasterxml.jackson. **Name** : jackson-bom. **Version** : 2.15.3.
Expand Down Expand Up @@ -3895,12 +3895,12 @@ This report was generated on **Wed Dec 18 18:28:45 WET 2024** using [Gradle-Lice

The dependencies distributed under several licenses, are used according their commercial-use-friendly license.

This report was generated on **Wed Dec 18 18:28:45 WET 2024** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE).
This report was generated on **Tue Dec 24 20:20:43 CET 2024** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE).




# Dependencies of `io.spine.validation:spine-validation-proto:2.0.0-SNAPSHOT.181`
# Dependencies of `io.spine.validation:spine-validation-proto:2.0.0-SNAPSHOT.182`

## Runtime
1. **Group** : com.fasterxml.jackson. **Name** : jackson-bom. **Version** : 2.15.3.
Expand Down Expand Up @@ -4785,12 +4785,12 @@ This report was generated on **Wed Dec 18 18:28:45 WET 2024** using [Gradle-Lice

The dependencies distributed under several licenses, are used according their commercial-use-friendly license.

This report was generated on **Wed Dec 18 18:28:45 WET 2024** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE).
This report was generated on **Tue Dec 24 20:20:44 CET 2024** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE).




# Dependencies of `io.spine.validation:spine-validation-consumer:2.0.0-SNAPSHOT.181`
# Dependencies of `io.spine.validation:spine-validation-consumer:2.0.0-SNAPSHOT.182`

## Runtime
1. **Group** : com.fasterxml.jackson. **Name** : jackson-bom. **Version** : 2.15.3.
Expand Down Expand Up @@ -5657,12 +5657,12 @@ This report was generated on **Wed Dec 18 18:28:45 WET 2024** using [Gradle-Lice

The dependencies distributed under several licenses, are used according their commercial-use-friendly license.

This report was generated on **Wed Dec 18 18:28:46 WET 2024** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE).
This report was generated on **Tue Dec 24 20:20:44 CET 2024** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE).




# Dependencies of `io.spine.validation:spine-validation-consumer-dependency:2.0.0-SNAPSHOT.181`
# Dependencies of `io.spine.validation:spine-validation-consumer-dependency:2.0.0-SNAPSHOT.182`

## Runtime
1. **Group** : com.google.code.findbugs. **Name** : jsr305. **Version** : 3.0.2.
Expand Down Expand Up @@ -6410,12 +6410,12 @@ This report was generated on **Wed Dec 18 18:28:46 WET 2024** using [Gradle-Lice

The dependencies distributed under several licenses, are used according their commercial-use-friendly license.

This report was generated on **Wed Dec 18 18:28:46 WET 2024** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE).
This report was generated on **Tue Dec 24 20:20:45 CET 2024** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE).




# Dependencies of `io.spine.validation:spine-validation-extensions:2.0.0-SNAPSHOT.181`
# Dependencies of `io.spine.validation:spine-validation-extensions:2.0.0-SNAPSHOT.182`

## Runtime
1. **Group** : com.fasterxml.jackson. **Name** : jackson-bom. **Version** : 2.15.3.
Expand Down Expand Up @@ -7308,12 +7308,12 @@ This report was generated on **Wed Dec 18 18:28:46 WET 2024** using [Gradle-Lice

The dependencies distributed under several licenses, are used according their commercial-use-friendly license.

This report was generated on **Wed Dec 18 18:28:46 WET 2024** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE).
This report was generated on **Tue Dec 24 20:20:45 CET 2024** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE).




# Dependencies of `io.spine.validation:spine-validation-runtime:2.0.0-SNAPSHOT.181`
# Dependencies of `io.spine.validation:spine-validation-runtime:2.0.0-SNAPSHOT.182`

## Runtime
1. **Group** : com.google.code.findbugs. **Name** : jsr305. **Version** : 3.0.2.
Expand Down Expand Up @@ -8064,12 +8064,12 @@ This report was generated on **Wed Dec 18 18:28:46 WET 2024** using [Gradle-Lice

The dependencies distributed under several licenses, are used according their commercial-use-friendly license.

This report was generated on **Wed Dec 18 18:28:46 WET 2024** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE).
This report was generated on **Tue Dec 24 20:20:46 CET 2024** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE).




# Dependencies of `io.spine.validation:spine-validation-validating:2.0.0-SNAPSHOT.181`
# Dependencies of `io.spine.validation:spine-validation-validating:2.0.0-SNAPSHOT.182`

## Runtime
1. **Group** : com.google.code.findbugs. **Name** : jsr305. **Version** : 3.0.2.
Expand Down Expand Up @@ -8824,12 +8824,12 @@ This report was generated on **Wed Dec 18 18:28:46 WET 2024** using [Gradle-Lice

The dependencies distributed under several licenses, are used according their commercial-use-friendly license.

This report was generated on **Wed Dec 18 18:28:46 WET 2024** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE).
This report was generated on **Tue Dec 24 20:20:46 CET 2024** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE).




# Dependencies of `io.spine.validation:spine-validation-vanilla:2.0.0-SNAPSHOT.181`
# Dependencies of `io.spine.validation:spine-validation-vanilla:2.0.0-SNAPSHOT.182`

## Runtime
1. **Group** : com.google.code.findbugs. **Name** : jsr305. **Version** : 3.0.2.
Expand Down Expand Up @@ -9575,12 +9575,12 @@ This report was generated on **Wed Dec 18 18:28:46 WET 2024** using [Gradle-Lice

The dependencies distributed under several licenses, are used according their commercial-use-friendly license.

This report was generated on **Wed Dec 18 18:28:46 WET 2024** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE).
This report was generated on **Tue Dec 24 20:20:47 CET 2024** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE).




# Dependencies of `io.spine.validation:spine-validation-configuration:2.0.0-SNAPSHOT.181`
# Dependencies of `io.spine.validation:spine-validation-configuration:2.0.0-SNAPSHOT.182`

## Runtime
1. **Group** : com.fasterxml.jackson. **Name** : jackson-bom. **Version** : 2.15.3.
Expand Down Expand Up @@ -10449,12 +10449,12 @@ This report was generated on **Wed Dec 18 18:28:46 WET 2024** using [Gradle-Lice

The dependencies distributed under several licenses, are used according their commercial-use-friendly license.

This report was generated on **Wed Dec 18 18:28:47 WET 2024** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE).
This report was generated on **Tue Dec 24 20:20:47 CET 2024** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE).




# Dependencies of `io.spine.validation:spine-validation-context:2.0.0-SNAPSHOT.181`
# Dependencies of `io.spine.validation:spine-validation-context:2.0.0-SNAPSHOT.182`

## Runtime
1. **Group** : com.fasterxml.jackson. **Name** : jackson-bom. **Version** : 2.15.3.
Expand Down Expand Up @@ -11323,4 +11323,4 @@ This report was generated on **Wed Dec 18 18:28:47 WET 2024** using [Gradle-Lice

The dependencies distributed under several licenses, are used according their commercial-use-friendly license.

This report was generated on **Wed Dec 18 18:28:47 WET 2024** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE).
This report was generated on **Tue Dec 24 20:20:48 CET 2024** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE).
20 changes: 19 additions & 1 deletion java-runtime/src/main/java/io/spine/validate/Constraint.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,24 @@ public interface Constraint {
*/
MessageType targetType();

/**
* Produces an error message template for the given field validation context.
*
* <p>Please note, the full support of {@link TemplateString} is not implemented yet
* for the runtime validation. This method was created to keep compatibility with
* the updated {@link ConstraintViolation} structure, which now expects a template string
* for error messages. The inheritors are not supposed to use or override this method.
*
* @param field
* the validated field
*/
default TemplateString errorMessage(FieldContext field) {
var formatted = formattedErrorMessage(field);
return TemplateString.newBuilder()
.setWithPlaceholders(formatted)
.build();
}

/**
* Produces an error message for the given field validation context.
*
Expand All @@ -52,7 +70,7 @@ public interface Constraint {
* @param field
* the validated field
*/
String errorMessage(FieldContext field);
String formattedErrorMessage(FieldContext field);

/**
* Accepts the given {@link ConstraintTranslator}.
Expand Down
34 changes: 24 additions & 10 deletions java-runtime/src/main/java/io/spine/validate/MessageValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,21 @@ public void visitPattern(PatternConstraint constraint) {
.filter(value -> partialMatch
? noPartialMatch(compiledPattern, (String) value)
: noCompleteMatch(compiledPattern, (String) value))
.map(value -> violation(constraint, fieldValue, value)
.toBuilder()
.addParam(regex)
.build())
.map(value -> {
var violation = violation(constraint, fieldValue, value);
return withRegex(violation, regex);
})
.forEach(violations::add);
}

private static ConstraintViolation withRegex(ConstraintViolation violation, String regex) {
var template = violation.getMessage().toBuilder()
.putPlaceholderValue("regex.pattern", regex);
return violation.toBuilder()
.setMessage(template)
.build();
}

private static boolean noCompleteMatch(Pattern pattern, String value) {
var matcher = pattern.matcher(value);
return !matcher.matches();
Expand Down Expand Up @@ -176,11 +184,17 @@ public void visitGoesWith(GoesConstraint constraint) {
);
var withField = declaration.get();
if (!value.isDefault() && fieldValueNotSet(withField)) {
var withFieldNotSet = violation(constraint, value).toBuilder()
.addParam(field.name().value())
.addParam(withFieldName)
var violation = violation(constraint, value);
var template = violation.getMessage()
.toBuilder()
.putPlaceholderValue("field.name", field.name().value())
.putPlaceholderValue("goes.companion", withFieldName)
.build();
violations.add(withFieldNotSet);
violations.add(
violation.toBuilder()
.setMessage(template)
.build()
);
}
}

Expand Down Expand Up @@ -226,7 +240,7 @@ public void visitRequiredOneof(IsRequiredConstraint constraint) {
var oneofField = Field.named(oneofName.value());
var targetType = constraint.targetType();
var violation = ConstraintViolation.newBuilder()
.setMsgFormat(constraint.errorMessage(message.context()))
.setMessage(constraint.errorMessage(message.context()))
.setFieldPath(oneofField.path())
.setTypeName(targetType.name().value())
.build();
Expand Down Expand Up @@ -347,7 +361,7 @@ private static ConstraintViolation violation(Constraint constraint,
var fieldPath = context.fieldPath();
var typeName = constraint.targetType().name();
var violation = ConstraintViolation.newBuilder()
.setMsgFormat(constraint.errorMessage(context))
.setMessage(constraint.errorMessage(context))
.setFieldPath(fieldPath)
.setTypeName(typeName.value());
if (violatingValue != null) {
Expand Down
Loading
Loading