Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javax validation on nested objects - not working

Tags:

In my Spring Boot project I have two DTO's which I'm trying to validate, LocationDto and BuildingDto. The LocationDto has a nested object of type BuildingDto.

These are my DTO's:

LocationDto

public class LocationDto {

  @NotNull(groups = { Existing.class })
  @Null(groups = { New.class })
  @Getter
  @Setter
  private Integer id;

  @NotNull(groups = { New.class, Existing.class })
  @Getter
  @Setter
  private String name;

  @NotNull(groups = { New.class, Existing.class, LocationGroup.class })
  @Getter
  @Setter
  private BuildingDto building;

  @NotNull(groups = { Existing.class })
  @Getter
  @Setter
  private Integer lockVersion;

}

BuildingDto

public class BuildingDto {

  @NotNull(groups = { Existing.class, LocationGroup.class })
  @Null(groups = { New.class })
  @Getter
  @Setter
  private Integer id;

  @NotNull(groups = { New.class, Existing.class })
  @Getter
  @Setter
  private String name;

  @NotNull(groups = { Existing.class })
  @Getter
  @Setter
  private List<LocationDto> locations;

  @NotNull(groups = { Existing.class })
  @Getter
  @Setter
  private Integer lockVersion;

}

Currently, I can validate in my LocationDto that the properties name and building are not null, but I can't validate the presence of the property id which is inside building.

If I use the @Valid annotation on the building property, it would validate all of its fields, but for this case I only want to validate its id.

How could that be done using javax validation?

This is my controller:

@PostMapping
public LocationDto createLocation(@Validated({ New.class, LocationGroup.class }) @RequestBody LocationDto location) {
  // save entity here...
}

This is a correct request body: (should not throw validation errors)

{
  "name": "Room 44",
  "building": {
    "id": 1
  }
}

This is an incorrect request body: (must throw validation errors because the building id is missing)

{
  "name": "Room 44",
  "building": { }
}
like image 813
Daniel Castillo Avatar asked Jan 01 '19 21:01

Daniel Castillo


People also ask

What@ Valid annotation does?

The @Valid annotation ensures the validation of the whole object. Importantly, it performs the validation of the whole object graph. However, this creates issues for scenarios needing only partial validation. On the other hand, we can use @Validated for group validation, including the above partial validation.

What is use of@ validated?

@Validated annotation is a class-level annotation that we can use to tell Spring to validate parameters that are passed into a method of the annotated class. and. @Valid annotation on method parameters and fields to tell Spring that we want a method parameter or field to be validated.

What is Hibernate Validator?

Hibernate Validator allows to express and validate application constraints. The default metadata source are annotations, with the ability to override and extend through the use of XML. It is not tied to a specific application tier or programming model and is available for both server and client application programming.


1 Answers

Just try adding @valid to collection. it would be working as per reference hibernate

  @Getter
  @Setter
  @Valid
  @NotNull(groups = { Existing.class })
  private List<LocationDto> locations;
like image 133
lucid Avatar answered Sep 22 '22 14:09

lucid