I have been around in Spring Boot eco-system since 2012, I have worked on many little projects. The most important thing in development I experienced id Validation
especially JPA Entities
. We simply do validation like:
@Entity
public class Input {
@Id
@GeneratedValue
private Long id;
@Min(1)
@Max(10)
private int numberBetweenOneAndTen;
@Pattern(regexp = "^[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}$")
private String ipAddress;
// ...
}
as mentioned here, but there is a note highlighted which says :
We usually don't want to do validation as late as in the persistence layer because it means that the business code above has worked with potentially invalid objects which may lead to unforeseen errors...
What is the safe and suitable way of Validating JPA Entities
?
I have tried in one of my projects as:
1) Entity class
@Data
@Entity(name = "users")
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = false)
public class AppUser extends AuditModel{
private String userFirstName;
private String userLastName;
private String userDateOfBirth;
}
2) Dto class
@Data
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode
public class AppUserDto {
private Long id;
@NotNull(message = "First name must not be null")
@NotBlank(message = "First name must not be blank")
private String userFirstName;
@NotNull(message = "Last name must not be null")
@NotBlank(message = "Last name must not be blank")
private String userLastName;
@NotNull(message = "Date of Birth must not be null")
@NotBlank(message = "Date of Birth must not be blank")
@Past()
private String userDateOfBirth;
}
3) Finally Validating Bean to a Spring Service Method
@Service
@Validated
public class AppUserService {
private final AppUserRepository appUserRepository;
@Autowired
public AppUserService(AppUserRepository appUserRepository) {
this.appUserRepository = appUserRepository;
}
public void update(@Valid AppUserDto appUserDto){
appUserRepository.update(
appUserDto.getId(),
appUserDto.getUserFirstName(),
appUserDto.getUserLastName(),
appUserDto.getUserDateOfBirth()
);
}
}
Is this approach suitable or there is an other better way than that?
Please give your feed back your reviews are precious to me!