Skip to content

Commit 6015ec7

Browse files
committed
Add oneOfStrings validator annotation for configuration validation
1 parent 7a8ae19 commit 6015ec7

3 files changed

Lines changed: 107 additions & 13 deletions

File tree

core/src/main/java/feast/core/config/FeastProperties.java

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717
package feast.core.config;
1818

1919
import feast.core.config.FeastProperties.StreamProperties.FeatureStreamOptions;
20+
import feast.core.validators.OneOfStrings;
2021
import java.util.*;
2122
import javax.annotation.PostConstruct;
2223
import javax.validation.*;
23-
import javax.validation.constraints.AssertTrue;
2424
import javax.validation.constraints.NotBlank;
2525
import javax.validation.constraints.NotNull;
2626
import javax.validation.constraints.Positive;
@@ -130,7 +130,9 @@ public feast.core.job.Runner getType() {
130130
public static class StreamProperties {
131131

132132
/* Feature stream type. Only "kafka" is supported. */
133-
@NotBlank private String type;
133+
@OneOfStrings({"kafka"})
134+
@NotBlank
135+
private String type;
134136

135137
/* Feature stream options */
136138
@NotNull private FeatureStreamOptions options;
@@ -157,16 +159,6 @@ public static class FeatureStreamOptions {
157159
}
158160
}
159161

160-
/**
161-
* Validates whether stream options are correct.
162-
*
163-
* @return Boolean used for assertion
164-
*/
165-
@AssertTrue
166-
public boolean isValidStreamTypeSelected() {
167-
return Objects.equals(getStream().getType(), "kafka");
168-
}
169-
170162
/** Feast population job metrics */
171163
@Getter
172164
@Setter
@@ -176,7 +168,9 @@ public static class MetricsProperties {
176168
private boolean enabled;
177169

178170
/* Metric type. Possible options: statsd */
179-
@NotBlank private String type;
171+
@OneOfStrings({"statsd"})
172+
@NotBlank
173+
private String type;
180174

181175
/* Host of metric sink */
182176
@URL private String host;
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
* Copyright 2018-2020 The Feast Authors
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* https://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package feast.core.validators;
18+
19+
import java.util.Arrays;
20+
import javax.validation.ConstraintValidator;
21+
import javax.validation.ConstraintValidatorContext;
22+
23+
/** Validates whether a string value is found within a collection. */
24+
public class OneOfStringValidator implements ConstraintValidator<OneOfStrings, String> {
25+
26+
/** Values that are permitted for a specific instance of this validator */
27+
String[] allowedValues;
28+
29+
/**
30+
* Initialize the OneOfStringValidator with a collection of allowed String values.
31+
*
32+
* @param constraintAnnotation
33+
*/
34+
@Override
35+
public void initialize(OneOfStrings constraintAnnotation) {
36+
allowedValues = constraintAnnotation.value();
37+
}
38+
39+
/**
40+
* Validates whether a string value is found within the collection defined in the annotation.
41+
*
42+
* @param value String value that should be validated
43+
* @param context Provides contextual data and operation when applying a given constraint
44+
* validator
45+
* @return Boolean value indicating whether the string is found within the allowed values.
46+
*/
47+
@Override
48+
public boolean isValid(String value, ConstraintValidatorContext context) {
49+
return Arrays.asList(allowedValues).contains(value);
50+
}
51+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
* Copyright 2018-2020 The Feast Authors
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* https://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package feast.core.validators;
18+
19+
import java.lang.annotation.*;
20+
import javax.validation.Constraint;
21+
import javax.validation.Payload;
22+
23+
/**
24+
* Annotation for String "one of" validation. Allows for the definition of a collection through an
25+
* annotation. The collection is used to test values defined in the object.
26+
*/
27+
@Target({
28+
ElementType.METHOD,
29+
ElementType.FIELD,
30+
ElementType.ANNOTATION_TYPE,
31+
ElementType.CONSTRUCTOR,
32+
ElementType.PARAMETER
33+
})
34+
@Retention(RetentionPolicy.RUNTIME)
35+
@Documented
36+
@Constraint(validatedBy = OneOfStringValidator.class)
37+
public @interface OneOfStrings {
38+
/** @return Default error message that is returned if the incorrect value is set */
39+
String message() default "Field value must be one of the following: {value}";
40+
41+
/** Allows for the specification of validation groups to which this constraint belongs. */
42+
Class<?>[] groups() default {};
43+
44+
/** An attribute payload that can be used to assign custom payload objects to a constraint. */
45+
Class<? extends Payload>[] payload() default {};
46+
47+
/** @return Default value that is returned if no allowed values are configured */
48+
String[] value() default {};
49+
}

0 commit comments

Comments
 (0)