0

I have create and update operation for user and the following UserDto

public class UserDto {
  @NotBlank
  private String username;
  @NotBlank
  private String password;
  @NotBlank
  private String mobileNo;

  ... other variables, etc.

}

For update operation, I don't want to update the password, I only want to update other variables. Then the password would be received as null (hence not passing @NotBlank).

But the @NotBlank is required for create operation. What is the best approach to solve this?

Should I create 2 Dtos e.g. UserCreateDto, UserUpdateDto (without password field).

I'm not sure if this is a good practice.

Muse
  • 1
  • 2

1 Answers1

0

Assuming you will have different APIs for create and update operation. You can use same dto and for password field you can put validation at backend side based on operation rather than in DTO itself.

  • I think this can cause confusion if some fields are validated in DTO while some are not. – Muse Dec 14 '22 at 12:06