Validation์ผ๋ก ์์ฒญ ๊ฐ ๊ฒ์ฆ
ํด๋ผ์ด์ธํธ๊ฐ ์์ฒญ ๊ฐ์ ๋ณด๋ผ ๋, Validation์ ์ด์ฉํ์ฌ ์๋ฒ์์ ์ฌ๋ฐ๋ฅธ ๊ฐ์ธ์ง ๊ฒ์ฆํด๋ณด์.
์์กด์ฑ ์ถ๊ฐ
gradle์ ์ด์ฉํ๋ค๋ฉด build.gradle์ ๋ค์๊ณผ ๊ฐ์ ์์กด์ฑ์ ์ถ๊ฐํด ์ค๋ค.
compile group: 'javax.validation', name: 'validation-api'
compile group: 'org.hibernate', name: 'hibernate-validator', version: '6.1.0.Final'
Dto ์์ฑ
ํด๋ผ์ด์ธํธ์๊ฒ ์ ๊ณต๋ฐ์ ๋ฐ์ดํฐ๋ฅผ ์ฒ๋ฆฌํ SignupDto
๋ฅผ ๋ค์๊ณผ ๊ฐ์ด ์์ฑํ๋ค.
import lombok.*;
import org.hibernate.validator.constraints.Length;
import javax.validation.constraints.Email;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Pattern;
@Getter
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class SignupDto {
@NotBlank
@Length(min=3, max = 20)
@Pattern(regexp = "^[ใฑ-ใ
๊ฐ-ํฃa-zA-Z0-9_-]{3,20}$")
private String nickname;
@NotBlank
@Length(min=8, max= 50)
private String password;
@Email
@NotBlank
private String email;
}
@NotBlank : ๊ณต๋ฐฑ์ ๋ฐฉ์งํ๋ ์ฃผ์์ด๋ค.
@Length(min=a, max = b) : ์ต์ a, ์ต๋ b ๋ฒ์๋ก ๊ธธ์ ๊ธธ์ด๋ฅผ ์ ํํ๋ค.
@Pattern(regexp = "") : ์ ๊ท ํํ์์ ์ฌ์ฉํ์ฌ ๋ค์ด์ฌ ์ ์๋ ๋ฌธ์๋ฅผ ์ ํํ๋ค.
์์ ์์ ์์ ํ๊ธ, ์์ด ๋์๋ฌธ์, ์ซ์, '_','-'๋ฅผ ํ์ฉํ๋ค.
@Email : String ํ์์ ๋ฐ์ดํฐ๊ฐ Email ํ์์ธ์ง ํ์ธํ๋ค.
์ปจํธ๋กค๋ฌ์์ ๊ฒ์ฆ
ํด๋ผ์ด์ธํธ๊ฐ "/sing-up" ๊ฒฝ๋ก๋ก Post ์์ฒญ์ ํตํด form์ ์ ๋ณด๋ฅผ ๋ณด๋ด์ค๋ค๋ฉด ๋ค์๊ณผ ๊ฐ์ด ์ฒ๋ฆฌํ ์ ์๋ค.
@PostMapping("/sign-up")
public ResponseEntity<?> singUpRequest(@RequestBody @Valid SignupDto signupDto, Errors errors){
return accountService.signUp(signupDto, errors);
}
ํ๋ผ๋ฏธํฐ์ @Valid
๋ฅผ ์ ์ธํด์ ์์ฒญ ๊ฐ์ ๊ฒ์ฆํ ์ ์๊ณ , ๊ฒ์ฆ ๊ฐ์ด ์ด๊ธ๋๋ฉด Errors errors
๋ก ์ฒ๋ฆฌํ ์ ์๋ค.
์์ ์์ ์์๋ ์๋น์ค ๋ ์ด์ด์ Errors ๊ฐ์ฒด๋ฅผ ์ ๋ฌํ๋ค.
Iterator๋ก ์๋ฌ ๋ฉ์์ง ์ถ์ถ
์๋น์ค์์ ์ ๋ฌ๋ฐ์ Errors๋ฅผ ์ดํฐ๋ ์ดํฐ๋ก ๋ฐํ๋ฐ์ ํ์ธํด๋ณด์.
private List<String> validCheck(SignupDto signupDto, Errors errors){
List<String> errorList = new ArrayList<>();
Iterator<ObjectError> it = errors.getAllErrors().iterator();
while(it.hasNext()){
errorList.add(it.next().getDefaultMessage());
}
return errorList;
}
์๋ฌ ๋ฉ์์ง ํ์ธํด๋ณด๊ธฐ
๋ค์๊ณผ ๊ฐ์ด Validation์ ์ด๊ธ๋๋ ๋ฐ์ดํฐ๋ฅผ ์ ๋ฌํ๋ฉด ๊ฐ๊ฐ์ ์ค๋ฅ ๋ฉ์์ง๊ฐ ์ถ๋ ฅ๋๋ ๊ฒ์ ํ์ธํ ์ ์๋ค.
Test Code
JUnit5๋ก ํ ์คํธ ์ฝ๋๋ฅผ ์์ฑํด๋ณด์.
import com.cadi.team3.domain.Account;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
@SpringBootTest
@AutoConfigureMockMvc
class AccountControllerTest {
private Account createUser() {
Account account = Account.builder()
.nickname("testUser")
.email("testUser@naver.com")
.password("123456789")
.role(Role.ROLE_USER)
.emailVerified(false)
.build();
return account;
}
@Autowired
private MockMvc mockMvc;
@Autowired
private ObjectMapper objectMapper;
@MockBean
private JavaMailSender javaMailSender;
@Autowired
AccountRepository accountRepository;
@BeforeEach
public void tearDown() {
accountRepository.deleteAll();
}
@DisplayName("๊ฐ์
Valid ํ์ธ #1 - form ๊ฐ์ด ์ ๋ชป ๋ ๊ฒฝ์ฐ")
@Test
public void sign_up_form_test1() throws Exception {
// given
SignupDto signupDto = SignupDto.builder()
.nickname("vividswan2131232131232131242145**")
.password("1234")
.email("vividswan")
.build();
// when
final ResultActions perform = mockMvc.perform(post("/sign-up")
.content(objectMapper.writeValueAsString(signupDto))
.contentType(MediaType.APPLICATION_JSON));
//then
perform.andExpect(status().is4xxClientError());
}
@DisplayName("๊ฐ์
Valid ํ์ธ #2 - ์ฌ๋ฐ๋ฅธ form ๊ฐ์ธ ๊ฒฝ์ฐ")
@Test
public void sign_up_form_test2() throws Exception {
//given
SignupDto signupDto = SignupDto.builder()
.nickname("vividswan")
.password("1234567890")
.email("vividswan@naver.com")
.build();
//when
final ResultActions perform = mockMvc.perform(post("/sign-up")
.content(objectMapper.writeValueAsString(signupDto))
.contentType(MediaType.APPLICATION_JSON));
//then
perform.andExpect(status().is2xxSuccessful());
}
}
ํ
์คํธ ์ฝ๋๊ฐ ํจ์ค๋์๋ค.
'Spring' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Spring์์ profile๋ก ํ๊ฒฝ๋ณ ์ค์ ์ ์ฉ(yml ํ์ผ) (0) | 2022.03.15 |
---|---|
Spring ํ๋ก์ ํธ์ Swagger2 ์ ์ฉ (0) | 2022.03.15 |
์๋ฒ ์คํ ์ DB Table ์์ฑ (0) | 2022.03.15 |
maven ํ๋ก์ ํธ์์ h2 ์คํ (0) | 2022.03.14 |
ํ์๊ฐ์ DTO ๋ง๋ค๊ธฐ (0) | 2022.03.14 |