Skip to content

Commit

Permalink
Merge pull request #246 from Link-MIND/refactor/#245
Browse files Browse the repository at this point in the history
[#245] refactor: ํ† ์ŠคํŠธ ์ œ๋ชฉ ์ˆ˜์ • validation ์ˆ˜์ • ๋ฐ ๊ทธ๋ฃน์„ค์ •
  • Loading branch information
sss4920 authored Oct 17, 2024
2 parents 08c03b0 + 41edf26 commit c89b4b5
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@
String message() default "Invalid title";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
String pattern() default "[๊ฐ€-ํžฃ|a-z|A-Z|0-9|]";
String pattern() default "^[\\S][๊ฐ€-ํžฃใ„ฑ-ใ…Žใ…-ใ…ฃa-zA-Z0-9\\s]{0,20}$";
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
package com.app.toaster.controller.valid;


import com.app.toaster.exception.Error;
import com.app.toaster.exception.model.CustomException;

import com.app.toaster.controller.valid.marker.ToastValidationGroup;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;

public class TitleValidator implements ConstraintValidator<TitleValid, String> {

public String pattern;
private String pattern;
private Class<?>[] groups;
@Override
public void initialize(TitleValid constraintAnnotation) {
this.pattern = constraintAnnotation.pattern();
this.groups = constraintAnnotation.groups();
}

@Override
Expand All @@ -39,18 +38,27 @@ public boolean isValid(String title, ConstraintValidatorContext context) {
return false;
}

if(title.length()>15){
if(!isGroupActive(ToastValidationGroup.class) && title.length()>15){ //ํ† ์ŠคํŠธ์ชฝ์ด ์•„๋‹ˆ๋ฉด ๊ฒ€์ฆ์„ ํ•ฉ๋‹ˆ๋‹ค.
context.buildConstraintViolationWithTemplate("์ด๋ฆ„์€ ์ตœ๋Œ€ 15์ž๊นŒ์ง€ ์ž…๋ ฅ ๊ฐ€๋Šฅํ•ด์š”")
.addConstraintViolation();
return false;
}

if(!title.matches("^[\\S][๊ฐ€-ํžฃใ„ฑ-ใ…Žใ…-ใ…ฃa-zA-Z0-9\\s]{0,20}$")){
if(!title.matches(pattern)){
context.buildConstraintViolationWithTemplate("ํŠน์ˆ˜ ๋ฌธ์ž๋กœ๋Š” ๊ฒ€์ƒ‰ํ•  ์ˆ˜ ์—†์–ด์š”.")
.addConstraintViolation();
return false;
}

return true;
}

private boolean isGroupActive(Class<?> targetGroup) {
for (Class<?> group : groups) {
if (group.equals(targetGroup)) {
return true;
}
}
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.app.toaster.controller.valid.marker;

public interface ToastValidationGroup {
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
import com.app.toaster.controller.valid.Severity;
import com.app.toaster.controller.valid.TitleValid;

import com.app.toaster.controller.valid.marker.ToastValidationGroup;
import jakarta.validation.constraints.NotNull;

public record UpdateToastDto(Long toastId, @TitleValid(payload = Severity.Error.class) @NotNull String title) {
public record UpdateToastDto(Long toastId, @TitleValid(payload = Severity.Error.class, groups = {ToastValidationGroup.class}) @NotNull String title) {
public static UpdateToastDto of(Long toastId, String title){
return new UpdateToastDto(toastId,title);
}
Expand Down

0 comments on commit c89b4b5

Please sign in to comment.