• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

TAKETODAY / today-infrastructure / 18154768944

01 Oct 2025 07:26AM UTC coverage: 81.882% (-0.005%) from 81.887%
18154768944

push

github

web-flow
Merge pull request #290 from TAKETODAY/dev/jspecify

jspecify

59788 of 78013 branches covered (76.64%)

Branch coverage included in aggregate %.

141239 of 167496 relevant lines covered (84.32%)

3.6 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

89.36
today-context/src/main/java/infra/context/properties/bind/validation/ValidationErrors.java
1
/*
2
 * Copyright 2017 - 2025 the original author or authors.
3
 *
4
 * This program is free software: you can redistribute it and/or modify
5
 * it under the terms of the GNU General Public License as published by
6
 * the Free Software Foundation, either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program. If not, see [https://www.gnu.org/licenses/]
16
 */
17

18
package infra.context.properties.bind.validation;
19

20
import org.jspecify.annotations.Nullable;
21

22
import java.util.ArrayList;
23
import java.util.Collections;
24
import java.util.Iterator;
25
import java.util.List;
26
import java.util.Set;
27

28
import infra.context.properties.source.ConfigurationProperty;
29
import infra.context.properties.source.ConfigurationPropertyName;
30
import infra.lang.Assert;
31
import infra.origin.Origin;
32
import infra.origin.OriginProvider;
33
import infra.validation.FieldError;
34
import infra.validation.ObjectError;
35

36
/**
37
 * A collection of {@link ObjectError ObjectErrors} caused by bind validation failures.
38
 * Where possible, included {@link FieldError FieldErrors} will be OriginProvider.
39
 *
40
 * @author Phillip Webb
41
 * @author Madhura Bhave
42
 * @author <a href="https://github.com/TAKETODAY">Harry Yang</a>
43
 * @since 4.0
44
 */
45
@SuppressWarnings("NullAway")
46
public class ValidationErrors implements Iterable<ObjectError> {
47

48
  private final ConfigurationPropertyName name;
49

50
  private final Set<ConfigurationProperty> boundProperties;
51

52
  private final List<ObjectError> errors;
53

54
  ValidationErrors(ConfigurationPropertyName name, Set<ConfigurationProperty> boundProperties, List<ObjectError> errors) {
2✔
55
    Assert.notNull(name, "Name is required");
3✔
56
    Assert.notNull(errors, "Errors is required");
3✔
57
    Assert.notNull(boundProperties, "BoundProperties is required");
3✔
58
    this.name = name;
3✔
59
    this.errors = convertErrors(name, boundProperties, errors);
7✔
60
    this.boundProperties = Collections.unmodifiableSet(boundProperties);
4✔
61
  }
1✔
62

63
  private List<ObjectError> convertErrors(ConfigurationPropertyName name,
64
          Set<ConfigurationProperty> boundProperties, List<ObjectError> errors) {
65
    ArrayList<ObjectError> converted = new ArrayList<>(errors.size());
6✔
66
    for (ObjectError error : errors) {
10✔
67
      converted.add(convertError(name, boundProperties, error));
8✔
68
    }
1✔
69
    return Collections.unmodifiableList(converted);
3✔
70
  }
71

72
  private ObjectError convertError(ConfigurationPropertyName name,
73
          Set<ConfigurationProperty> boundProperties, ObjectError error) {
74
    if (error instanceof FieldError) {
3✔
75
      return convertFieldError(name, boundProperties, (FieldError) error);
7✔
76
    }
77
    return error;
2✔
78
  }
79

80
  private FieldError convertFieldError(ConfigurationPropertyName name,
81
          Set<ConfigurationProperty> boundProperties, FieldError error) {
82
    if (error instanceof OriginProvider) {
3!
83
      return error;
×
84
    }
85
    return OriginTrackedFieldError.of(error, findFieldErrorOrigin(name, boundProperties, error));
8✔
86
  }
87

88
  @Nullable
89
  private Origin findFieldErrorOrigin(ConfigurationPropertyName name,
90
          Set<ConfigurationProperty> boundProperties, FieldError error) {
91
    for (ConfigurationProperty boundProperty : boundProperties) {
10✔
92
      if (isForError(name, boundProperty.getName(), error)) {
7✔
93
        return Origin.from(boundProperty);
3✔
94
      }
95
    }
1✔
96
    return null;
2✔
97
  }
98

99
  private boolean isForError(ConfigurationPropertyName name,
100
          ConfigurationPropertyName boundPropertyName, FieldError error) {
101
    return name.isParentOf(boundPropertyName)
7✔
102
            && boundPropertyName.getLastElement(ConfigurationPropertyName.Form.UNIFORM).equalsIgnoreCase(error.getField());
8✔
103
  }
104

105
  /**
106
   * Return the name of the item that was being validated.
107
   *
108
   * @return the name of the item
109
   */
110
  public ConfigurationPropertyName getName() {
111
    return this.name;
3✔
112
  }
113

114
  /**
115
   * Return the properties that were bound before validation failed.
116
   *
117
   * @return the boundProperties
118
   */
119
  public Set<ConfigurationProperty> getBoundProperties() {
120
    return this.boundProperties;
3✔
121
  }
122

123
  public boolean hasErrors() {
124
    return !this.errors.isEmpty();
×
125
  }
126

127
  /**
128
   * Return the list of all validation errors.
129
   *
130
   * @return the errors
131
   */
132
  public List<ObjectError> getAllErrors() {
133
    return this.errors;
3✔
134
  }
135

136
  @Override
137
  public Iterator<ObjectError> iterator() {
138
    return this.errors.iterator();
4✔
139
  }
140

141
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc