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
|
/*
* Copyright 2000-2018 Vaadin Ltd.
*
* 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.vaadin.data;
import java.io.Serializable;
import java.util.Objects;
import java.util.function.BiFunction;
import com.vaadin.server.SerializablePredicate;
import com.vaadin.shared.ui.ErrorLevel;
/**
* A functional interface for validating user input or other potentially invalid
* data. When a validator instance is applied to a value of the corresponding
* type, it returns a <i>result</i> signifying that the value either passed or
* failed the validation.
* <p>
* For instance, the following validator checks if a number is positive:
*
* <pre>
* Validator<Integer> v = num -> {
* if (num >= 0)
* return ValidationResult.ok();
* else
* return ValidationResult.error("number must be positive");
* };
* </pre>
*
* @author Vaadin Ltd.
*
* @since 8.0
*
* @param <T>
* the type of the value to validate
*
* @see ValidationResult
*/
@FunctionalInterface
public interface Validator<T>
extends BiFunction<T, ValueContext, ValidationResult>, Serializable {
/**
* Validates the given value. Returns a {@code ValidationResult} instance
* representing the outcome of the validation.
*
* @param value
* the input value to validate
* @param context
* the value context for validation
* @return the validation result
*/
@Override
public ValidationResult apply(T value, ValueContext context);
/**
* Returns a validator that passes any value.
*
* @param <T>
* the value type
* @return an always-passing validator
*/
public static <T> Validator<T> alwaysPass() {
return (value, context) -> ValidationResult.ok();
}
/**
* Builds a validator out of a conditional function and an error message. If
* the function returns true, the validator returns {@code Result.ok()}; if
* it returns false or throws an exception,
* {@link ValidationResult#error(String)} is returned with the given message
* and error level {@link ErrorLevel#ERROR}.
* <p>
* For instance, the following validator checks if a number is between 0 and
* 10, inclusive:
*
* <pre>
* Validator<Integer> v = Validator.from(num -> num >= 0 && num <= 10,
* "number must be between 0 and 10");
* </pre>
*
* @param <T>
* the value type
* @param guard
* the function used to validate, not null
* @param errorMessage
* the message returned if validation fails, not null
* @return the new validator using the function
*/
public static <T> Validator<T> from(SerializablePredicate<T> guard,
String errorMessage) {
Objects.requireNonNull(errorMessage, "errorMessage cannot be null");
return from(guard, ctx -> errorMessage);
}
/**
* Builds a validator out of a conditional function and an error message. If
* the function returns true, the validator returns {@code Result.ok()}; if
* it returns false or throws an exception,
* {@link ValidationResult#error(String)} is returned with the given message
* and error level.
* <p>
* For instance, the following validator checks if a number is between 0 and
* 10, inclusive:
*
* <pre>
* Validator<Integer> v = Validator.from(num -> num >= 0 && num <= 10,
* "number must be between 0 and 10", ErrorLevel.ERROR);
* </pre>
*
* @param <T>
* the value type
* @param guard
* the function used to validate, not null
* @param errorMessage
* the message returned if validation fails, not null
* @param errorLevel
* the error level for failures from this validator, not null
* @return the new validator using the function
*
* @since 8.2
*/
public static <T> Validator<T> from(SerializablePredicate<T> guard,
String errorMessage, ErrorLevel errorLevel) {
Objects.requireNonNull(errorMessage, "errorMessage cannot be null");
return from(guard, ctx -> errorMessage, errorLevel);
}
/**
* Builds a validator out of a conditional function and an error message
* provider. If the function returns true, the validator returns
* {@code Result.ok()}; if it returns false or throws an exception,
* {@code Result.error()} is returned with the message from the provider.
*
* @param <T>
* the value type
* @param guard
* the function used to validate, not null
* @param errorMessageProvider
* the provider to generate error messages, not null
* @return the new validator using the function
*/
public static <T> Validator<T> from(SerializablePredicate<T> guard,
ErrorMessageProvider errorMessageProvider) {
return from(guard, errorMessageProvider, ErrorLevel.ERROR);
}
/**
* Builds a validator out of a conditional function and an error message
* provider. If the function returns true, the validator returns
* {@code Result.ok()}; if it returns false or throws an exception,
* {@code Result.error()} is returned with the message from the provider.
*
* @param <T>
* the value type
* @param guard
* the function used to validate, not null
* @param errorMessageProvider
* the provider to generate error messages, not null
* @param errorLevel
* the error level for failures from this validator, not null
* @return the new validator using the function
*
* @since 8.2
*/
public static <T> Validator<T> from(SerializablePredicate<T> guard,
ErrorMessageProvider errorMessageProvider, ErrorLevel errorLevel) {
Objects.requireNonNull(guard, "guard cannot be null");
Objects.requireNonNull(errorMessageProvider,
"errorMessageProvider cannot be null");
Objects.requireNonNull(errorLevel, "errorLevel cannot be null");
return (value, context) -> {
try {
if (guard.test(value)) {
return ValidationResult.ok();
}
return ValidationResult.create(
errorMessageProvider.apply(context), errorLevel);
} catch (Exception e) {
return ValidationResult.create(
errorMessageProvider.apply(context), errorLevel);
}
};
}
}
|