-
Hello, After some searches I didn't find my answer. Let's take directly an example. I have the following simple config file with a valid content: hostname: "example.com"
port: 1234
isSslEnabled: true But I want that any unexpected properties must lead to a failure like this is the case for the following content: hostname: "example.com"
port: 1234
isSslEnabled: true
unexpectedField: test To fail on missing fields, I know it exists the method What about when having a class annotated with @ConfigSerializable
// Lombok annotations...
public final class HostValidatingProperties
extends ValidatingConvertibleProperties<HostProperties> {
private String hostname;
private int port;
private boolean isSslEnabled;
// Other methods...
} Is there a way to configure the Even if we can find solutions for missing fields, this isn't the same story for unexpected ones. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
For missing fields, @ConfigSerializable
// Lombok annotations...
public final class HostValidatingProperties
extends ValidatingConvertibleProperties<HostProperties> {
@Required
private String hostname;
@Required
private int port;
@Required
private boolean isSslEnabled;
// Other methods...
} Following test pass now: class DbmsHostValidatingPropertiesTest {
/* Other tests */
@DisplayName("With missing content")
@Test
void withMissingContent_shouldThrowSerializationException() {
// Given
String yamlFileName = "whenDeserializing_withMissingPortField.yml";
Path yamlFile = /* ... */; // Retrieve the empty YAML file somewhere
// When
ThrowingCallable throwingCallable = () -> TestYamlDeserializerHelper.deserialize(yamlFile,
DbmsHostValidatingProperties.class);
// Then
assertThatThrownBy(throwingCallable).isInstanceOf(SerializationException.class);
}
} However, I didn't found any trivial way to add a constraint for unexpected fields by reading Configurate's source code. Maybe with additional searches something could be found, but finally I realized that this isn't really important in fact. So, I decided to abandon this idea. Having the possibility to add a constraint about required fields is enough for me and works. |
Beta Was this translation helpful? Give feedback.
For missing fields,
@Required
annotation add the sought constraint. By taking back the example provided in the question, we obtain this result:Following test pass now: