Repository navigation
Expand file tree
/
Copy pathValidator.java
More file actions
394 lines (362 loc) · 18 KB
/
Copy pathValidator.java
File metadata and controls
394 lines (362 loc) · 18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
package org.javawebstack.validator;
import org.javawebstack.abstractdata.AbstractArray;
import org.javawebstack.abstractdata.AbstractElement;
import org.javawebstack.abstractdata.AbstractNull;
import org.javawebstack.abstractdata.mapper.Mapper;
import org.javawebstack.abstractdata.mapper.MapperTypeSpec;
import org.javawebstack.validator.rule.*;
import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.sql.Date;
import java.sql.Timestamp;
import java.util.*;
public class Validator {
private static final Map<String, Constructor<? extends ValidationRule>> validationRules = new HashMap<>();
private static final Map<Class<?>, Validator> validators = new HashMap<>();
private static final Map<Class<? extends ValidationRule>, Class<? extends Annotation>> ruleAnnotationClasses = new HashMap<>();
static {
registerRuleType("string", StringRule.Validator.class, StringRule.class);
registerRuleType("boolean", BooleanRule.Validator.class, BooleanRule.class);
registerRuleType("bool", BooleanRule.Validator.class, BooleanRule.class);
registerRuleType("enum", EnumRule.Validator.class, EnumRule.class);
registerRuleType("required", RequiredRule.Validator.class, RequiredRule.class);
registerRuleType("req", RequiredRule.Validator.class, RequiredRule.class);
registerRuleType("ipv4", IPv4AddressRule.Validator.class, IPv4AddressRule.class);
registerRuleType("ipv6", IPv6AddressRule.Validator.class, IPv6AddressRule.class);
registerRuleType("int", IntegerRule.Validator.class, IntegerRule.class);
registerRuleType("integer", IntegerRule.Validator.class, IntegerRule.class);
registerRuleType("double", DoubleRule.Validator.class, DoubleRule.class);
registerRuleType("numeric", NumericRule.Validator.class, NumericRule.class);
registerRuleType("num", NumericRule.Validator.class, NumericRule.class);
registerRuleType("date", DateRule.Validator.class, DateRule.class);
registerRuleType("array", ArrayRule.Validator.class, ArrayRule.class);
registerRuleType("list", ArrayRule.Validator.class, ArrayRule.class);
registerRuleType("alpha", AlphaRule.Validator.class, AlphaRule.class);
registerRuleType("alpha_num", AlphaNumRule.Validator.class, AlphaNumRule.class);
registerRuleType("alpha_dash", AlphaDashRule.Validator.class, AlphaDashRule.class);
registerRuleType("email", EmailRule.Validator.class, EmailRule.class);
registerRuleType("regex", RegexRule.Validator.class, RegexRule.class);
registerRuleType("uuid", UUIDRule.Validator.class, UUIDRule.class);
registerRuleType("charset", CharsetRule.Validator.class, CharsetRule.class);
registerRuleType("word_count", WordCountRule.Validator.class, WordCountRule.class);
}
private final Map<String[], ValidationConfig> rules = new HashMap<>();
public static void registerRuleType(String name, Class<? extends ValidationRule> type, Class<? extends Annotation> annotationClass) {
if (!ruleAnnotationClasses.containsKey(type) && annotationClass != null)
ruleAnnotationClasses.put(type, annotationClass);
try {
Constructor<? extends ValidationRule> constructor = type.getDeclaredConstructor(String[].class);
constructor.setAccessible(true);
validationRules.put(name, constructor);
return;
} catch (NoSuchMethodException ignored) {
}
try {
Constructor<? extends ValidationRule> constructor = type.getDeclaredConstructor();
constructor.setAccessible(true);
validationRules.put(name, constructor);
} catch (NoSuchMethodException ignored) {
}
}
public static ValidationRule makeRule(String name, String[] params) {
Constructor<? extends ValidationRule> constructor = validationRules.get(name);
if (constructor == null)
return null;
if (constructor.getParameterCount() == 0) {
try {
return constructor.newInstance();
} catch (InstantiationException | InvocationTargetException | IllegalAccessException ignored) {
}
} else {
try {
return constructor.newInstance((Object) params);
} catch (InstantiationException | InvocationTargetException | IllegalAccessException ignored) {
}
}
return null;
}
public static ValidationRule makeRule(String source) {
String[] params = null;
if (source.contains("(")) {
String[] spl = source.split("\\(", 2);
source = spl[0];
String s = spl[1];
s = s.substring(0, s.length() - 1);
AbstractArray array = AbstractElement.fromJson("[" + s + "]").array();
if (array.stream().filter(e -> e.isPrimitive()).count() == array.size()) {
params = new String[array.size()];
for (int i = 0; i < params.length; i++)
params[i] = array.get(i).toString();
}
}
if (params == null)
params = new String[0];
return makeRule(source, params);
}
public static ValidationRule[] makeRules(String... sources) {
ValidationRule[] rules = new ValidationRule[sources.length];
for (int i = 0; i < rules.length; i++)
rules[i] = makeRule(sources[i]);
return rules;
}
public static Validator getValidator(Class<?> type) {
Validator validator = validators.get(type);
if (validator == null) {
validator = new Validator();
getClassRules(null, type).forEach(validator::rule);
validators.put(type, validator);
}
return validator;
}
public static <T> T map(ValidationContext context, Class<T> type, AbstractElement element, Mapper mapper) {
Validator validator = getValidator(type);
ValidationResult result = validator.validate(context, element);
if (!result.isValid())
throw new ValidationException(result);
return mapper.map(element, type);
}
public static <T> T map(ValidationContext context, Class<T> type, AbstractElement element) {
return map(context, type, element, new Mapper());
}
public Validator rule(String[] key, ValidationRule... rules) {
return rule(key, Arrays.asList(rules));
}
public Validator rule(String[] key, List<ValidationRule> rules) {
this.rules.put(key, new ValidationConfig(null, rules));
return this;
}
private Validator rule(String[] key, ValidationConfig config) {
this.rules.put(key, config);
return this;
}
public Validator rule(String key, List<ValidationRule> rules) {
return rule(Arrays.stream(key.split("\\.")).map(k -> k.equals("*") ? null : k).toArray(String[]::new), rules);
}
public Validator rule(String key, ValidationRule... rules) {
return rule(key, Arrays.asList(rules));
}
public ValidationResult validate(ValidationContext context, AbstractElement rootElement) {
context.setValidator(this);
Map<String[], List<ValidationError>> errors = new HashMap<>();
for (String[] key : rules.keySet()) {
errors.putAll(check(context, rules, new String[0], new String[0], key, rootElement));
}
return new ValidationResult(context, errors);
}
private Map<String[], List<ValidationError>> check(ValidationContext context, Map<String[], ValidationConfig> rules, String[] keyPrefix, String[] resolvedKeyPrefix, String[] key, AbstractElement element) {
if (element == null)
element = AbstractNull.INSTANCE;
if (key.length == 0) {
Map<String[], List<ValidationError>> errors = new HashMap<>();
ValidationConfig config = getMapValue(rules, keyPrefix);
for (ValidationRule rule : config.rules) {
String error = rule.validate(context, config.field, element);
if (error != null) {
if (!errors.containsKey(resolvedKeyPrefix))
errors.put(resolvedKeyPrefix, new ArrayList<>());
errors.get(resolvedKeyPrefix).add(new ValidationError(rule, error));
}
}
return errors;
}
String[] innerKey = new String[key.length - 1];
System.arraycopy(key, 1, innerKey, 0, innerKey.length);
String[] innerKeyPrefix = new String[keyPrefix.length + 1];
System.arraycopy(keyPrefix, 0, innerKeyPrefix, 0, keyPrefix.length);
innerKeyPrefix[innerKeyPrefix.length - 1] = key[0];
if (key[0].equals("*")) {
Map<String[], List<ValidationError>> errors = new HashMap<>();
if (element.isArray()) {
for (int i = 0; i < element.array().size(); i++) {
String[] innerResolvedKeyPrefix = new String[keyPrefix.length + 1];
System.arraycopy(resolvedKeyPrefix, 0, innerResolvedKeyPrefix, 0, resolvedKeyPrefix.length);
innerResolvedKeyPrefix[innerResolvedKeyPrefix.length - 1] = String.valueOf(i);
errors.putAll(check(context, rules, innerKeyPrefix, innerResolvedKeyPrefix, innerKey, element.array().get(i)));
}
}
if (element.isObject()) {
for (String k : element.object().keys()) {
String[] innerResolvedKeyPrefix = new String[keyPrefix.length + 1];
System.arraycopy(resolvedKeyPrefix, 0, innerResolvedKeyPrefix, 0, resolvedKeyPrefix.length);
innerResolvedKeyPrefix[innerResolvedKeyPrefix.length - 1] = k;
errors.putAll(check(context, rules, innerKeyPrefix, innerResolvedKeyPrefix, innerKey, element.object().get(k)));
}
}
return errors;
}
AbstractElement value = AbstractNull.INSTANCE;
if (element.isArray()) {
try {
value = element.array().get(Integer.parseInt(key[0]));
} catch (Exception ignored) {
}
}
if (element.isObject()) {
value = element.object().get(key[0]);
}
String[] innerResolvedKeyPrefix = new String[keyPrefix.length + 1];
System.arraycopy(resolvedKeyPrefix, 0, innerResolvedKeyPrefix, 0, resolvedKeyPrefix.length);
innerResolvedKeyPrefix[innerResolvedKeyPrefix.length - 1] = key[0];
return check(context, rules, innerKeyPrefix, innerResolvedKeyPrefix, innerKey, value);
}
private static boolean stringArrayEqual(String[] a, String[] b) {
if (a.length != b.length)
return false;
for (int i = 0; i < a.length; i++) {
if (a[i] == null && b[i] == null)
continue;
if (a[i] == null || b[i] == null)
return false;
if (!a[i].equals(b[i]))
return false;
}
return true;
}
private static <V> V getMapValue(Map<String[], V> map, String[] key) {
for (String[] k : map.keySet()) {
if (stringArrayEqual(k, key)) {
return map.get(k);
}
}
return null;
}
private static <V> void putValidationConfigMapValue(Map<String[], ValidationConfig> map, String[] key, ValidationConfig value) {
for (String[] k : map.keySet()) {
if (stringArrayEqual(k, key)) {
map.put(k, value);
return;
}
}
map.put(key, value);
}
private static void addMapRules(Field field, Map<String[], ValidationConfig> map, String[] key, List<ValidationRule> rules) {
ValidationConfig config = getMapValue(map, key);
if (config == null)
config = new ValidationConfig(field, new ArrayList<>());
config.rules.addAll(rules);
putValidationConfigMapValue(map, key, config);
}
private static String toSnakeCase(String source) {
StringBuilder sb = new StringBuilder();
sb.append(Character.toLowerCase(source.charAt(0)));
for (int i = 1; i < source.length(); i++) {
if (Character.isUpperCase(source.charAt(i))) {
if (!Character.isUpperCase(source.charAt(i - 1)))
sb.append("_");
sb.append(Character.toLowerCase(source.charAt(i)));
} else {
sb.append(source.charAt(i));
}
}
return sb.toString();
}
private static String getFieldName(Field field) {
MapperTypeSpec typeSpec = MapperTypeSpec.get(field.getDeclaringClass());
if(typeSpec == null)
return toSnakeCase(field.getName());
MapperTypeSpec.FieldSpec fieldSpec = typeSpec.getFieldSpecs().stream().filter(f -> f.getField().equals(field)).findFirst().orElse(null);
if(fieldSpec == null || fieldSpec.getName() == null)
return toSnakeCase(field.getName());
return fieldSpec.getName();
}
private static class ValidationConfig {
private final Field field;
private final List<ValidationRule> rules;
public ValidationConfig(Field field, List<ValidationRule> rules) {
this.field = field;
this.rules = rules;
}
}
private static Map<String[], ValidationConfig> getClassRules(Field field, Class<?> type) {
Map<String[], ValidationConfig> rules = new HashMap<>();
if (type.isAnnotation())
return rules;
if (type.equals(String.class))
return rules;
if (type.equals(Long.class))
return rules;
if (type.equals(Timestamp.class) || type.equals(java.util.Date.class)) {
rules.put(new String[0], new ValidationConfig(field, Collections.singletonList(new DateRule.Validator(new String[]{}))));
return rules;
}
if (type.equals(Date.class)) {
rules.put(new String[0], new ValidationConfig(field, Collections.singletonList(new DateRule.Validator(new String[]{"date"}))));
return rules;
}
if (type.equals(Boolean.class)) {
rules.put(new String[0], new ValidationConfig(field, Collections.singletonList(new BooleanRule.Validator())));
return rules;
}
if (type.equals(Integer.class)) {
rules.put(new String[0], new ValidationConfig(field, Collections.singletonList(new IntegerRule.Validator(Integer.MIN_VALUE, Integer.MAX_VALUE))));
return rules;
}
if (type.equals(Double.class) || type.equals(Float.class)) {
rules.put(new String[0], new ValidationConfig(field, Collections.singletonList(new DoubleRule.Validator(Double.MIN_VALUE, Double.MAX_VALUE))));
return rules;
}
if (type.equals(UUID.class)) {
rules.put(new String[0], new ValidationConfig(field, Collections.singletonList(new UUIDRule.Validator())));
return rules;
}
if (type.isEnum()) {
rules.put(new String[0], new ValidationConfig(field, Collections.singletonList(new EnumRule.Validator((Class<? extends Enum<?>>) type))));
return rules;
}
if (type.isArray()) {
getClassRules(null, type.getComponentType()).forEach((key, validators) -> {
String[] actualKey = new String[key.length + 1];
actualKey[0] = "*";
System.arraycopy(key, 0, actualKey, 1, key.length);
addMapRules(null, rules, actualKey, validators.rules);
});
return rules;
}
for (Field f : getFieldsRecursive(type)) {
String name = getFieldName(f);
getClassRules(f, f.getType()).forEach((key, validators) -> {
String[] actualKey = new String[key.length + 1];
actualKey[0] = name;
System.arraycopy(key, 0, actualKey, 1, key.length);
addMapRules(f, rules, actualKey, validators.rules);
});
f.setAccessible(true);
Rule[] ruleAnnotations = f.getDeclaredAnnotationsByType(Rule.class);
if (ruleAnnotations.length > 0) {
List<ValidationRule> r = new ArrayList<>();
for (String source : ruleAnnotations[0].value()) {
ValidationRule rule = Validator.makeRule(source);
if (rule != null)
r.add(rule);
}
if (r.size() > 0)
addMapRules(f, rules, new String[]{name}, r);
}
ruleAnnotationClasses.entrySet().stream().distinct().forEach(annotation -> {
Annotation a = f.getDeclaredAnnotation(annotation.getValue());
if (a != null) {
List<ValidationRule> r = new ArrayList<>();
try {
Constructor<ValidationRule> constructor = (Constructor<ValidationRule>) annotation.getKey().getDeclaredConstructor(annotation.getValue());
constructor.setAccessible(true);
r.add(constructor.newInstance(a));
} catch (NoSuchMethodException | InvocationTargetException | InstantiationException | IllegalAccessException ignored) {}
if (r.size() > 0)
addMapRules(f, rules, new String[]{name}, r);
}
});
}
return rules;
}
private static List<Field> getFieldsRecursive(Class<?> type) {
List<Field> fields;
if (type.getSuperclass() != null && !type.getSuperclass().equals(Object.class))
fields = getFieldsRecursive(type.getSuperclass());
else
fields = new ArrayList<>();
fields.addAll(Arrays.asList(type.getDeclaredFields()));
return fields;
}
}