You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Validator.java 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * Copyright 2000-2016 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.data;
  17. import java.io.Serializable;
  18. import java.util.Objects;
  19. import java.util.function.BiFunction;
  20. import com.vaadin.server.SerializablePredicate;
  21. import com.vaadin.shared.ui.ErrorLevel;
  22. /**
  23. * A functional interface for validating user input or other potentially invalid
  24. * data. When a validator instance is applied to a value of the corresponding
  25. * type, it returns a <i>result</i> signifying that the value either passed or
  26. * failed the validation.
  27. * <p>
  28. * For instance, the following validator checks if a number is positive:
  29. *
  30. * <pre>
  31. * Validator&lt;Integer&gt; v = num -&gt; {
  32. * if (num &gt;= 0)
  33. * return ValidationResult.ok();
  34. * else
  35. * return ValidationResult.error("number must be positive");
  36. * };
  37. * </pre>
  38. *
  39. * @author Vaadin Ltd.
  40. *
  41. * @since 8.0
  42. *
  43. * @param <T>
  44. * the type of the value to validate
  45. *
  46. * @see ValidationResult
  47. */
  48. @FunctionalInterface
  49. public interface Validator<T>
  50. extends BiFunction<T, ValueContext, ValidationResult>, Serializable {
  51. /**
  52. * Validates the given value. Returns a {@code ValidationResult} instance
  53. * representing the outcome of the validation.
  54. *
  55. * @param value
  56. * the input value to validate
  57. * @param context
  58. * the value context for validation
  59. * @return the validation result
  60. */
  61. @Override
  62. public ValidationResult apply(T value, ValueContext context);
  63. /**
  64. * Returns a validator that passes any value.
  65. *
  66. * @param <T>
  67. * the value type
  68. * @return an always-passing validator
  69. */
  70. public static <T> Validator<T> alwaysPass() {
  71. return (value, context) -> ValidationResult.ok();
  72. }
  73. /**
  74. * Builds a validator out of a conditional function and an error message. If
  75. * the function returns true, the validator returns {@code Result.ok()}; if
  76. * it returns false or throws an exception,
  77. * {@link ValidationResult#error(String)} is returned with the given message
  78. * and error level {@link ErrorLevel#ERROR}.
  79. * <p>
  80. * For instance, the following validator checks if a number is between 0 and
  81. * 10, inclusive:
  82. *
  83. * <pre>
  84. * Validator&lt;Integer&gt; v = Validator.from(num -&gt; num &gt;= 0 && num &lt;= 10,
  85. * "number must be between 0 and 10");
  86. * </pre>
  87. *
  88. * @param <T>
  89. * the value type
  90. * @param guard
  91. * the function used to validate, not null
  92. * @param errorMessage
  93. * the message returned if validation fails, not null
  94. * @return the new validator using the function
  95. */
  96. public static <T> Validator<T> from(SerializablePredicate<T> guard,
  97. String errorMessage) {
  98. Objects.requireNonNull(errorMessage, "errorMessage cannot be null");
  99. return from(guard, ctx -> errorMessage);
  100. }
  101. /**
  102. * Builds a validator out of a conditional function and an error message. If
  103. * the function returns true, the validator returns {@code Result.ok()}; if
  104. * it returns false or throws an exception,
  105. * {@link ValidationResult#error(String)} is returned with the given message
  106. * and error level.
  107. * <p>
  108. * For instance, the following validator checks if a number is between 0 and
  109. * 10, inclusive:
  110. *
  111. * <pre>
  112. * Validator&lt;Integer&gt; v = Validator.from(num -&gt; num &gt;= 0 && num &lt;= 10,
  113. * "number must be between 0 and 10", ErrorLevel.ERROR);
  114. * </pre>
  115. *
  116. * @param <T>
  117. * the value type
  118. * @param guard
  119. * the function used to validate, not null
  120. * @param errorMessage
  121. * the message returned if validation fails, not null
  122. * @param errorLevel
  123. * the error level for failures from this validator, not null
  124. * @return the new validator using the function
  125. *
  126. * @since 8.2
  127. */
  128. public static <T> Validator<T> from(SerializablePredicate<T> guard,
  129. String errorMessage, ErrorLevel errorLevel) {
  130. Objects.requireNonNull(errorMessage, "errorMessage cannot be null");
  131. return from(guard, ctx -> errorMessage, errorLevel);
  132. }
  133. /**
  134. * Builds a validator out of a conditional function and an error message
  135. * provider. If the function returns true, the validator returns
  136. * {@code Result.ok()}; if it returns false or throws an exception,
  137. * {@code Result.error()} is returned with the message from the provider.
  138. *
  139. * @param <T>
  140. * the value type
  141. * @param guard
  142. * the function used to validate, not null
  143. * @param errorMessageProvider
  144. * the provider to generate error messages, not null
  145. * @return the new validator using the function
  146. */
  147. public static <T> Validator<T> from(SerializablePredicate<T> guard,
  148. ErrorMessageProvider errorMessageProvider) {
  149. return from(guard, errorMessageProvider, ErrorLevel.ERROR);
  150. }
  151. /**
  152. * Builds a validator out of a conditional function and an error message
  153. * provider. If the function returns true, the validator returns
  154. * {@code Result.ok()}; if it returns false or throws an exception,
  155. * {@code Result.error()} is returned with the message from the provider.
  156. *
  157. * @param <T>
  158. * the value type
  159. * @param guard
  160. * the function used to validate, not null
  161. * @param errorMessageProvider
  162. * the provider to generate error messages, not null
  163. * @param errorLevel
  164. * the error level for failures from this validator, not null
  165. * @return the new validator using the function
  166. *
  167. * @since 8.2
  168. */
  169. public static <T> Validator<T> from(SerializablePredicate<T> guard,
  170. ErrorMessageProvider errorMessageProvider, ErrorLevel errorLevel) {
  171. Objects.requireNonNull(guard, "guard cannot be null");
  172. Objects.requireNonNull(errorMessageProvider,
  173. "errorMessageProvider cannot be null");
  174. Objects.requireNonNull(errorLevel, "errorLevel cannot be null");
  175. return (value, context) -> {
  176. try {
  177. if (guard.test(value)) {
  178. return ValidationResult.ok();
  179. } else {
  180. return ValidationResult.create(
  181. errorMessageProvider.apply(context), errorLevel);
  182. }
  183. } catch (Exception e) {
  184. return ValidationResult.create(
  185. errorMessageProvider.apply(context), errorLevel);
  186. }
  187. };
  188. }
  189. }