Validation Library Document - dev-team-projects/DeliTalk GitHub Wiki
์์ฑ์ : ์์ฑ์ค
-
๋ฐ์ดํฐ ์ ํจ์ฑ ๊ฒ์ฆ(Validation) ์ ํด๋ผ์ด์ธํธ๊ฐ ์ ๋ฌํ ์์ฒญ ๊ฐ์ด ์๋ฒ์์ ๊ธฐ๋ํ ์กฐ๊ฑด์ ๋ง์กฑํ๋์ง ํ์ธํ๋ ์ ์ฐจ์ ๋๋ค.
-
Spring Boot์์๋ javax.validation(JSR-380), Hibernate Validator๋ฅผ ์ด์ฉํด ๊ฐํธํ๊ฒ ๊ฒ์ฆ ๊ฐ๋ฅํฉ๋๋ค.
- ์ฃผ๋ก javax.validation.constraints ํจํค์ง์ ์ด๋ ธํ ์ด์ ์ ์ฌ์ฉํฉ๋๋ค.
์ด๋ ธํ ์ด์ | ์ค๋ช |
---|---|
@NotNull | ๊ฐ์ด null์ด ์๋์ด์ผ ํจ |
@NotBlank | ๋ฌธ์์ด์ด null, "", " "๊ฐ ์๋์ด์ผ ํจ |
@Size(min=, max=) | ๋ฌธ์์ด/์ปฌ๋ ์ ํฌ๊ธฐ ์ ํ |
์ด๋ฉ์ผ ํ์ ๊ฒ์ฆ | |
@Pattern(regexp="") | ์ ๊ทํํ์ ๊ฒ์ฆ |
@Min, @Max | ์ต์/์ต๋๊ฐ ๊ฒ์ฆ |
public class UserRequestDto {
@NotBlank(message = "์ด๋ฆ์ ํ์ ์
๋ ฅ์
๋๋ค.")
private String name;
@Email(message = "์ฌ๋ฐ๋ฅธ ์ด๋ฉ์ผ ํ์์ด ์๋๋๋ค.")
private String email;
}
@PostMapping("/user")
public ResponseEntity<String> createUser(@Valid @RequestBody UserRequestDto requestDto) {
// ์ ํจ์ฑ ๊ฒ์ฆ ํต๊ณผ ์ ์ฒ๋ฆฌ
return ResponseEntity.ok("์ ์ ์์ฑ ์๋ฃ");
}
-
@Valid
๋๋@Validated
์ด๋ ธํ ์ด์ ์ ๋ฐ๋์ ๋ถ์ฌ์ผ ๊ฒ์ฆ์ด ์ํ๋ฉ๋๋ค.