- /**
- * Validates the items in a map.
- *
- * @param result where to add the results
- * @param fieldName name of the field containing the list
- * @param map the field's map (may be {@code null})
- * @param checker function to validate in individual item in the list
- */
- public static <T> void validateMap(@NonNull BeanValidationResult result, @NonNull String fieldName,
- Map<String, T> map, @NonNull Function<Map.Entry<String, T>, ValidationResult> checker) {
- if (map == null) {
- return;
- }
-
- BeanValidationResult result2 = new BeanValidationResult(fieldName, map);
-
- for (Entry<String, T> entry : map.entrySet()) {
- result2.addResult(checker.apply(entry));
- }
-
- if (!result2.isClean()) {
- result.addResult(result2);
- }
- }
-
- /**
- * Validates a Map entry, ensuring that neither the key nor the value are "blank"
- * (i.e., empty or {@code null}).
- *
- * @param entry entry to be validated
- * @return a result, or {@code null}
- */
- public static BeanValidationResult validateEntryNotBlankNotBlank(Map.Entry<String, String> entry) {
- BeanValidationResult result = new BeanValidationResult("" + entry.getKey(), entry.getKey());
-
- if (StringUtils.isBlank(entry.getKey())) {
- Validated.addResult(result, KEY_TOKEN, entry.getKey(), IS_BLANK);
- }
-
- if (StringUtils.isBlank(entry.getValue())) {
- Validated.addResult(result, VALUE_TOKEN, entry.getValue(), IS_BLANK);
- }
-
- return (result.isClean() ? null : result);
- }
-
- /**
- * Validates a Map entry, ensuring that the key is not "blank" (i.e., empty or
- * {@code null}) and the value is not {@code null}.
- *
- * @param entry entry to be validated
- * @return a result, or {@code null}
- */
- public static BeanValidationResult validateEntryNotBlankNotNull(Map.Entry<String, String> entry) {
- BeanValidationResult result = new BeanValidationResult("" + entry.getKey(), entry.getKey());
-
- if (StringUtils.isBlank(entry.getKey())) {
- Validated.addResult(result, KEY_TOKEN, entry.getKey(), IS_BLANK);
- }
-
- if (entry.getValue() == null) {
- result.addResult(makeNullResult(VALUE_TOKEN, entry.getValue()));
- }
-
- return (result.isClean() ? null : result);
- }
-
- /**
- * Validates a Map entry, ensuring that neither the key nor the value are
- * {@code null}. If the value is a subclass of this class, then it's
- * {@link #validate(String)} method is invoked.
- *
- * @param entry entry to be validated
- * @return a result, or {@code null}
- */
- public static <V> BeanValidationResult validateEntryValueNotNull(Map.Entry<String, V> entry) {
- BeanValidationResult result = new BeanValidationResult("" + entry.getKey(), entry.getKey());
-
- if (entry.getKey() == null) {
- result.addResult(makeNullResult(KEY_TOKEN, entry.getKey()));
- }
-
- V value = entry.getValue();
- if (value == null) {
- result.addResult(makeNullResult(VALUE_TOKEN, value));
- } else if (value instanceof Validated) {
- result.addResult(((Validated) value).validate(VALUE_TOKEN));
- }
-
- return (result.isClean() ? null : result);
- }
-