11
11
import org .opensearch .common .ValidationException ;
12
12
import org .joda .time .Instant ;
13
13
14
+ import java .util .ArrayList ;
15
+ import java .util .List ;
14
16
import java .util .Map ;
15
17
import java .util .Set ;
16
18
@@ -28,7 +30,6 @@ public class RuleValidator {
28
30
private final String featureValue ;
29
31
private final String updatedAt ;
30
32
private final FeatureType featureType ;
31
- private final ValidationException validationException = new ValidationException ();
32
33
public static final int MAX_DESCRIPTION_LENGTH = 256 ;
33
34
34
35
public RuleValidator (
@@ -46,90 +47,106 @@ public RuleValidator(
46
47
}
47
48
48
49
public void validate () {
49
- validateStringFields ();
50
- validateFeatureType ();
51
- validateUpdatedAtEpoch ();
52
- validateAttributeMap ();
53
- if (!validationException .validationErrors ().isEmpty ()) {
50
+ List <String > errorMessages = new ArrayList <>();
51
+ errorMessages .addAll (validateStringFields ());
52
+ errorMessages .addAll (validateFeatureType ());
53
+ errorMessages .addAll (validateUpdatedAtEpoch ());
54
+ errorMessages .addAll (validateAttributeMap ());
55
+ if (!errorMessages .isEmpty ()) {
56
+ ValidationException validationException = new ValidationException ();
57
+ validationException .addValidationErrors (errorMessages );
54
58
throw new IllegalArgumentException (validationException );
55
59
}
56
60
}
57
61
58
- private void validateStringFields () {
59
- requireNonNullOrEmpty (description , "Rule description can't be null or empty" );
60
- if (description != null && description .length () > MAX_DESCRIPTION_LENGTH ) {
61
- validationException .addValidationError ("Rule description cannot exceed " + MAX_DESCRIPTION_LENGTH + " characters." );
62
+ private List <String > validateStringFields () {
63
+ List <String > errors = new ArrayList <>();
64
+ if (isNullOrEmpty (description )) {
65
+ errors .add ("Rule description can't be null or empty" );
66
+ } else if (description .length () > MAX_DESCRIPTION_LENGTH ) {
67
+ errors .add ("Rule description cannot exceed " + MAX_DESCRIPTION_LENGTH + " characters." );
62
68
}
63
- requireNonNullOrEmpty (featureValue , "Rule featureValue can't be null or empty" );
64
- requireNonNullOrEmpty (updatedAt , "Rule update time can't be null or empty" );
69
+ if (isNullOrEmpty (featureValue )) {
70
+ errors .add ("Rule featureValue can't be null or empty" );
71
+ }
72
+ if (isNullOrEmpty (updatedAt )) {
73
+ errors .add ("Rule update time can't be null or empty" );
74
+ }
75
+ return errors ;
65
76
}
66
77
67
- private void validateFeatureType () {
78
+ private boolean isNullOrEmpty (String str ) {
79
+ return str == null || str .isEmpty ();
80
+ }
81
+
82
+ private List <String > validateFeatureType () {
68
83
if (featureType == null ) {
69
- validationException . addValidationError ("Couldn't identify which feature the rule belongs to. Rule feature can't be null." );
84
+ return List . of ("Couldn't identify which feature the rule belongs to. Rule feature can't be null." );
70
85
}
86
+ return new ArrayList <>();
71
87
}
72
88
73
- private void validateUpdatedAtEpoch () {
89
+ private List < String > validateUpdatedAtEpoch () {
74
90
if (updatedAt != null && !isValid (Instant .parse (updatedAt ).getMillis ())) {
75
- validationException . addValidationError ("Rule update time is not a valid epoch" );
91
+ return List . of ("Rule update time is not a valid epoch" );
76
92
}
93
+ return new ArrayList <>();
77
94
}
78
95
79
- private void validateAttributeMap () {
96
+ private List <String > validateAttributeMap () {
97
+ List <String > errors = new ArrayList <>();
80
98
if (attributeMap == null || attributeMap .isEmpty ()) {
81
- validationException . addValidationError ("Rule should have at least 1 attribute requirement" );
99
+ errors . add ("Rule should have at least 1 attribute requirement" );
82
100
}
83
101
84
102
if (attributeMap != null && featureType != null ) {
85
103
for (Map .Entry <Attribute , Set <String >> entry : attributeMap .entrySet ()) {
86
104
Attribute attribute = entry .getKey ();
87
105
Set <String > attributeValues = entry .getValue ();
88
- validateAttributeExistence (attribute );
89
- validateMaxAttributeValues (attribute , attributeValues );
90
- validateAttributeValuesLength (attributeValues );
106
+ errors . addAll ( validateAttributeExistence (attribute ) );
107
+ errors . addAll ( validateMaxAttributeValues (attribute , attributeValues ) );
108
+ errors . addAll ( validateAttributeValuesLength (attributeValues ) );
91
109
}
92
110
}
111
+ return errors ;
93
112
}
94
113
95
- private void validateAttributeExistence (Attribute attribute ) {
114
+ private List < String > validateAttributeExistence (Attribute attribute ) {
96
115
if (featureType .getAttributeFromName (attribute .getName ()) == null ) {
97
- validationException .addValidationError (
98
- attribute .getName () + " is not a valid attribute within the " + featureType .getName () + " feature."
99
- );
116
+ return List .of (attribute .getName () + " is not a valid attribute within the " + featureType .getName () + " feature." );
100
117
}
118
+ return new ArrayList <>();
101
119
}
102
120
103
- private void validateMaxAttributeValues (Attribute attribute , Set <String > attributeValues ) {
121
+ private List <String > validateMaxAttributeValues (Attribute attribute , Set <String > attributeValues ) {
122
+ List <String > errors = new ArrayList <>();
123
+ String attributeName = attribute .getName ();
124
+ if (attributeValues .isEmpty ()) {
125
+ errors .add ("Attribute values for " + attributeName + " cannot be empty." );
126
+ }
104
127
int maxSize = featureType .getMaxNumberOfValuesPerAttribute ();
105
128
int actualSize = attributeValues .size ();
106
129
if (actualSize > maxSize ) {
107
- validationException . addValidationError (
130
+ errors . add (
108
131
"Each attribute can only have a maximum of "
109
132
+ maxSize
110
133
+ " values. The input attribute "
111
- + attribute
134
+ + attributeName
112
135
+ " has length "
113
136
+ attributeValues .size ()
114
137
+ ", which exceeds this limit."
115
138
);
116
139
}
140
+ return errors ;
117
141
}
118
142
119
- private void validateAttributeValuesLength (Set <String > attributeValues ) {
143
+ private List < String > validateAttributeValuesLength (Set <String > attributeValues ) {
120
144
int maxValueLength = featureType .getMaxCharLengthPerAttributeValue ();
121
145
for (String attributeValue : attributeValues ) {
122
146
if (attributeValue .isEmpty () || attributeValue .length () > maxValueLength ) {
123
- validationException .addValidationError (
124
- "Attribute value [" + attributeValue + "] is invalid (empty or exceeds " + maxValueLength + " characters)"
125
- );
147
+ return List .of ("Attribute value [" + attributeValue + "] is invalid (empty or exceeds " + maxValueLength + " characters)" );
126
148
}
127
149
}
128
- }
129
-
130
- private void requireNonNullOrEmpty (String value , String errorMessage ) {
131
- if (value == null || value .isEmpty ()) {
132
- validationException .addValidationError (errorMessage );
133
- }
150
+ return new ArrayList <>();
134
151
}
135
152
}
0 commit comments