Skip to content

Commit 29d62f4

Browse files
authored
Merge pull request #14 from sashirestela/13-validate-fields-declared-in-parent-classes
Validate fields declared in parent classes
2 parents 7f26dc4 + 7ede54f commit 29d62f4

File tree

4 files changed

+46
-2
lines changed

4 files changed

+46
-2
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>io.github.sashirestela</groupId>
88
<artifactId>slimvalidator</artifactId>
9-
<version>1.2.0</version>
9+
<version>1.2.1</version>
1010
<packaging>jar</packaging>
1111

1212
<name>slimvalidator</name>

src/main/java/io/github/sashirestela/slimvalidator/metadata/ClassMetadata.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public static class FieldMetadata {
2727
public Object getValue(Object object) {
2828
var clazz = object.getClass();
2929
try {
30-
var method = clazz.getDeclaredMethod(getMethodName(name), (Class<?>[]) null);
30+
var method = clazz.getMethod(getMethodName(name), (Class<?>[]) null);
3131
return method.invoke(object);
3232
} catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException
3333
| InvocationTargetException e) {

src/test/java/io/github/sashirestela/slimvalidator/ValidatorTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.github.sashirestela.slimvalidator;
22

3+
import io.github.sashirestela.slimvalidator.data.AbstractClass.ChildClass;
34
import io.github.sashirestela.slimvalidator.data.Address;
45
import io.github.sashirestela.slimvalidator.data.Address.Coordinate;
56
import io.github.sashirestela.slimvalidator.data.Person;
@@ -75,4 +76,20 @@ void shouldReturnViolationsWhenObjectDoesNotAccomplishConstraints() {
7576
assertEquals(expectedViolationMessage, actualViolationsMessage);
7677
}
7778

79+
@Test
80+
void shouldExecuteValidationWhenThereIsClassHierarchy() {
81+
82+
var childObject = ChildClass.builder()
83+
.name("name")
84+
.level(12)
85+
.category("category")
86+
.build();
87+
var validator = new Validator();
88+
var violations = validator.validate(childObject);
89+
var exception = new ConstraintViolationException(violations);
90+
var actualViolationsMessage = exception.getMessage();
91+
var expectedViolationMessage = "level must be at least 1 at most 10.";
92+
assertEquals(expectedViolationMessage, actualViolationsMessage);
93+
}
94+
7895
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package io.github.sashirestela.slimvalidator.data;
2+
3+
import io.github.sashirestela.slimvalidator.constraints.Range;
4+
import io.github.sashirestela.slimvalidator.constraints.Required;
5+
import lombok.Getter;
6+
import lombok.experimental.SuperBuilder;
7+
8+
@Getter
9+
@SuperBuilder
10+
public abstract class AbstractClass {
11+
12+
@Required
13+
protected String name;
14+
15+
@Range(min = 1, max = 10)
16+
protected Integer level;
17+
18+
@Getter
19+
@SuperBuilder
20+
public static class ChildClass extends AbstractClass {
21+
22+
@Required
23+
private String category;
24+
25+
}
26+
27+
}

0 commit comments

Comments
 (0)