This repository has been archived by the owner on May 14, 2022. It is now read-only.
/
CollectionValidator.java
106 lines (88 loc) · 3.94 KB
/
CollectionValidator.java
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
/*
* Copyright 2018 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.netflix.titus.common.model.sanitizer.internal;
import java.util.Collection;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.TreeSet;
import java.util.function.Function;
import java.util.stream.Collectors;
import javax.validation.ConstraintValidatorContext;
import com.netflix.titus.common.model.sanitizer.CollectionInvariants;
public class CollectionValidator extends AbstractConstraintValidator<CollectionInvariants, Object> {
private CollectionInvariants constraintAnnotation;
@Override
public void initialize(CollectionInvariants constraintAnnotation) {
this.constraintAnnotation = constraintAnnotation;
}
@Override
protected boolean isValid(Object value, Function<String, ConstraintValidatorContext.ConstraintViolationBuilder> constraintViolationBuilderFunction) {
if (value == null) {
return true;
}
if (value instanceof Collection) {
return isValid((Collection<?>) value, constraintViolationBuilderFunction);
}
if (value instanceof Map) {
return isValid((Map<?, ?>) value, constraintViolationBuilderFunction);
}
return false;
}
private boolean isValid(Collection<?> value, Function<String, ConstraintValidatorContext.ConstraintViolationBuilder> constraintViolationBuilderFunction) {
if (value.isEmpty()) {
return true;
}
if (!constraintAnnotation.allowNullValues()) {
if (value.stream().anyMatch(Objects::isNull)) {
attachMessage(constraintViolationBuilderFunction, "null values not allowed");
return false;
}
}
return true;
}
private boolean isValid(Map<?, ?> value, Function<String, ConstraintValidatorContext.ConstraintViolationBuilder> constraintViolationBuilderFunction) {
if (value.isEmpty()) {
return true;
}
if (!constraintAnnotation.allowEmptyKeys()) {
if (value.keySet().stream().anyMatch(key -> key == null || (key instanceof String && ((String) key).isEmpty()))) {
attachMessage(constraintViolationBuilderFunction, "empty key names not allowed");
return false;
}
}
if (!constraintAnnotation.allowNullKeys()) {
if (value.keySet().stream().anyMatch(Objects::isNull)) {
attachMessage(constraintViolationBuilderFunction, "null key names not allowed");
return false;
}
}
if (!constraintAnnotation.allowNullValues()) {
Set<String> badEntryKeys = value.entrySet().stream()
.filter(e -> e.getValue() == null)
.map(e -> e.getKey() instanceof String ? (String) e.getKey() : "<not_string>")
.collect(Collectors.toSet());
if (!badEntryKeys.isEmpty()) {
attachMessage(constraintViolationBuilderFunction, "null values found for keys: " + new TreeSet<>(badEntryKeys));
return false;
}
}
return true;
}
private void attachMessage(Function<String, ConstraintValidatorContext.ConstraintViolationBuilder> constraintViolationBuilderFunction, String message) {
constraintViolationBuilderFunction.apply(message).addConstraintViolation().disableDefaultConstraintViolation();
}
}