Skip to content

Commit

Permalink
Merge pull request #14 from sashirestela/13-validate-fields-declared-…
Browse files Browse the repository at this point in the history
…in-parent-classes

Validate fields declared in parent classes
  • Loading branch information
sashirestela authored Apr 6, 2024
2 parents 7f26dc4 + 7ede54f commit 29d62f4
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 2 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>io.github.sashirestela</groupId>
<artifactId>slimvalidator</artifactId>
<version>1.2.0</version>
<version>1.2.1</version>
<packaging>jar</packaging>

<name>slimvalidator</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public static class FieldMetadata {
public Object getValue(Object object) {
var clazz = object.getClass();
try {
var method = clazz.getDeclaredMethod(getMethodName(name), (Class<?>[]) null);
var method = clazz.getMethod(getMethodName(name), (Class<?>[]) null);
return method.invoke(object);
} catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException
| InvocationTargetException e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.github.sashirestela.slimvalidator;

import io.github.sashirestela.slimvalidator.data.AbstractClass.ChildClass;
import io.github.sashirestela.slimvalidator.data.Address;
import io.github.sashirestela.slimvalidator.data.Address.Coordinate;
import io.github.sashirestela.slimvalidator.data.Person;
Expand Down Expand Up @@ -75,4 +76,20 @@ void shouldReturnViolationsWhenObjectDoesNotAccomplishConstraints() {
assertEquals(expectedViolationMessage, actualViolationsMessage);
}

@Test
void shouldExecuteValidationWhenThereIsClassHierarchy() {

var childObject = ChildClass.builder()
.name("name")
.level(12)
.category("category")
.build();
var validator = new Validator();
var violations = validator.validate(childObject);
var exception = new ConstraintViolationException(violations);
var actualViolationsMessage = exception.getMessage();
var expectedViolationMessage = "level must be at least 1 at most 10.";
assertEquals(expectedViolationMessage, actualViolationsMessage);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package io.github.sashirestela.slimvalidator.data;

import io.github.sashirestela.slimvalidator.constraints.Range;
import io.github.sashirestela.slimvalidator.constraints.Required;
import lombok.Getter;
import lombok.experimental.SuperBuilder;

@Getter
@SuperBuilder
public abstract class AbstractClass {

@Required
protected String name;

@Range(min = 1, max = 10)
protected Integer level;

@Getter
@SuperBuilder
public static class ChildClass extends AbstractClass {

@Required
private String category;

}

}

0 comments on commit 29d62f4

Please sign in to comment.