Skip to content

Commit f348b80

Browse files
jcabanneschristophechevalier
authored andcommitted
fix(lvp): skip validation for null and empty values
1 parent a39ef44 commit f348b80

File tree

3 files changed

+14
-13
lines changed

3 files changed

+14
-13
lines changed

lvp/README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,12 @@ This will validate that the value is one of: `ACTIVE`, `INACTIVE`, `SUSPENDED` o
6868
* The plugin retrieves the value to validate from the context (as defined by the hosting engine).
6969
* It checks whether the value is present in the configured `allowedValues` list.
7070
* The comparison is **case-sensitive**.
71-
* If the value is `null` or not found in the list, a validation error is returned.
71+
* If the value is not in the list, a validation error is returned.
72+
* If the value is null or an empty string no error is returned, this checks should be done by the `RequiredValidationPlugin` if needed.
7273

7374
## 🧷 Notes
7475

7576
* All values in the `allowedValues` array are treated as strings.
7677
* The validation is **case-sensitive** (e.g., `"ACTIVE"` ≠ `"active"`).
7778
* If the `allowedValues` option is missing or invalid, a configuration error will be raised at startup.
78-
* `null` values and empty strings are rejected by the validation.
79+
* `null` values and empty strings are accepted by the validation.

lvp/src/main/java/io/github/linagora/linid/im/lvp/ListValidationPlugin.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ public Optional<I18nMessage> validate(
6666

6767
Map<String, Object> options = configuration.getOptions();
6868

69+
if (value == null || value.toString().isEmpty()) {
70+
return Optional.empty();
71+
}
72+
6973
if (!options.containsKey(ALLOWED_VALUES)) {
7074
throw new ApiException(
7175
500,
@@ -84,7 +88,7 @@ public Optional<I18nMessage> validate(
8488
)
8589
));
8690

87-
if (value == null || !allowedValues.contains(value.toString())) {
91+
if (!allowedValues.contains(value.toString())) {
8892
return Optional.of(I18nMessage.of("error.plugin.listValidation.invalid.value"));
8993
}
9094

lvp/src/test/java/io/github/linagora/linid/im/lvp/ListValidationPluginTest.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ void testValidateMissingAllowedValuesOption() {
5656
var configuration = new ValidationConfiguration();
5757

5858
ApiException exception =
59-
assertThrows(ApiException.class, () -> plugin.validate(configuration, ""));
59+
assertThrows(ApiException.class, () -> plugin.validate(configuration, "test"));
6060

6161
assertEquals(500, exception.getStatusCode());
6262
assertEquals("error.plugin.default.missing.option", exception.getError().key());
@@ -70,39 +70,35 @@ void testValidateInvalidAllowedValuesOption() {
7070
var configuration = new ValidationConfiguration();
7171
configuration.addOption("allowedValues", "invalid");
7272
ApiException exception =
73-
assertThrows(ApiException.class, () -> plugin.validate(configuration, ""));
73+
assertThrows(ApiException.class, () -> plugin.validate(configuration, "test"));
7474

7575
assertEquals(500, exception.getStatusCode());
7676
assertEquals("error.plugin.default.invalid.option", exception.getError().key());
7777
assertEquals(Map.of("option", "allowedValues"), exception.getError().context());
7878
}
7979

8080
@Test
81-
@DisplayName("test validate: should return message on null value")
81+
@DisplayName("test validate: should accept null value")
8282
void testValidateNullValue() {
8383
var plugin = new ListValidationPlugin();
8484
var configuration = new ValidationConfiguration();
85-
configuration.addOption("allowedValues", new String[] {"TEST"});
8685

8786
var error = plugin.validate(configuration, null);
8887

8988
assertNotNull(error);
90-
assertTrue(error.isPresent());
91-
assertEquals("error.plugin.listValidation.invalid.value", error.get().key());
89+
assertTrue(error.isEmpty());
9290
}
9391

9492
@Test
95-
@DisplayName("test validate: should return message on empty string value")
93+
@DisplayName("test validate: should accept empty string value")
9694
void testValidateEmptyStringValue() {
9795
var plugin = new ListValidationPlugin();
9896
var configuration = new ValidationConfiguration();
99-
configuration.addOption("allowedValues", new String[] {"TEST"});
10097

10198
var error = plugin.validate(configuration, "");
10299

103100
assertNotNull(error);
104-
assertTrue(error.isPresent());
105-
assertEquals("error.plugin.listValidation.invalid.value", error.get().key());
101+
assertTrue(error.isEmpty());
106102
}
107103

108104
@Test

0 commit comments

Comments
 (0)