🚧 自定义验证表单
This commit is contained in:
parent
54c2581e08
commit
86104b6bdf
|
@ -1,15 +0,0 @@
|
|||
package com.mall.common.domain.dto.valid;
|
||||
|
||||
public interface ValidationGroups {
|
||||
interface QUERY {
|
||||
}
|
||||
|
||||
interface Add {
|
||||
}
|
||||
|
||||
interface Update {
|
||||
}
|
||||
|
||||
interface DELETE {
|
||||
}
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
package com.mall.common.domain.dto.valid.custom;
|
||||
|
||||
import jakarta.validation.Constraint;
|
||||
import jakarta.validation.Payload;
|
||||
|
||||
import java.lang.annotation.Documented;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
import static java.lang.annotation.ElementType.*;
|
||||
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
||||
|
||||
@Documented
|
||||
@Constraint(validatedBy = {ListValueConstraintValidator.class})
|
||||
@Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE})
|
||||
@Retention(RUNTIME)
|
||||
public @interface ListValue {
|
||||
|
||||
String message() default "{com.mall.common.domain.dto.valid.custom.ListValue.message}";
|
||||
|
||||
Class<?>[] groups() default {};
|
||||
|
||||
Class<? extends Payload>[] payload() default {};
|
||||
|
||||
int[] vals() default {};
|
||||
|
||||
}
|
|
@ -0,0 +1,48 @@
|
|||
package com.mall.common.domain.dto.valid.custom;
|
||||
|
||||
import jakarta.validation.ConstraintValidator;
|
||||
import jakarta.validation.ConstraintValidatorContext;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
public class ListValueConstraintValidator implements ConstraintValidator<ListValue, Integer> {
|
||||
private final Set<Integer> set = new HashSet<>();
|
||||
|
||||
/**
|
||||
* Initializes the validator in preparation for
|
||||
* calls.
|
||||
* The constraint annotation for a given constraint declaration
|
||||
* is passed.
|
||||
* <p>
|
||||
* This method is guaranteed to be called before any use of this instance for
|
||||
* validation.
|
||||
* <p>
|
||||
* The default implementation is a no-op.
|
||||
*
|
||||
* @param constraintAnnotation annotation instance for a given constraint declaration
|
||||
*/
|
||||
@Override
|
||||
public void initialize(ListValue constraintAnnotation) {
|
||||
int[] vals = constraintAnnotation.vals();
|
||||
for (int val : vals) {
|
||||
set.add(val);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements the validation logic.
|
||||
* The state of {@code value} must not be altered.
|
||||
* <p>
|
||||
* This method can be accessed concurrently, thread-safety must be ensured
|
||||
* by the implementation.
|
||||
*
|
||||
* @param value object to validate
|
||||
* @param context context in which the constraint is evaluated
|
||||
* @return {@code false} if {@code value} does not pass the constraint
|
||||
*/
|
||||
@Override
|
||||
public boolean isValid(Integer value, ConstraintValidatorContext context) {
|
||||
return set.contains(value);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,4 @@
|
|||
package com.mall.common.domain.dto.valid.group;
|
||||
|
||||
public interface AddGroup {
|
||||
}
|
|
@ -0,0 +1,4 @@
|
|||
package com.mall.common.domain.dto.valid.group;
|
||||
|
||||
public interface DeletedGroup {
|
||||
}
|
|
@ -0,0 +1,4 @@
|
|||
package com.mall.common.domain.dto.valid.group;
|
||||
|
||||
public interface QueryGroup {
|
||||
}
|
|
@ -0,0 +1,4 @@
|
|||
package com.mall.common.domain.dto.valid.group;
|
||||
|
||||
public interface UpdateGroup {
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
com.mall.common.domain.dto.valid.custom.ListValue.message=\u503C\u4E0D\u5728\u6307\u5B9A\u8303\u56F4\u4E2D
|
|
@ -1,7 +1,8 @@
|
|||
package com.mall.product.controller;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.mall.common.domain.dto.valid.ValidationGroups;
|
||||
import com.mall.common.domain.dto.valid.group.AddGroup;
|
||||
import com.mall.common.domain.dto.valid.group.UpdateGroup;
|
||||
import com.mall.common.domain.vo.result.PageResult;
|
||||
import com.mall.common.domain.vo.result.Result;
|
||||
import com.mall.common.domain.vo.result.ResultCodeEnum;
|
||||
|
@ -12,7 +13,6 @@ import com.mall.product.service.BrandService;
|
|||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.Parameter;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import jakarta.validation.Valid;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
@ -50,14 +50,14 @@ public class BrandController {
|
|||
|
||||
@Operation(summary = "添加品牌", description = "添加品牌")
|
||||
@PostMapping()
|
||||
public Result<String> addBrand(@Valid @RequestBody BrandDto dto) {
|
||||
public Result<String> addBrand(@Validated(AddGroup.class) @RequestBody BrandDto dto) {
|
||||
brandService.addBrand(dto);
|
||||
return Result.success(ResultCodeEnum.ADD_SUCCESS);
|
||||
}
|
||||
|
||||
@Operation(summary = "更新品牌", description = "更新品牌")
|
||||
@PutMapping()
|
||||
public Result<String> updateBrand(@Validated(ValidationGroups.Update.class) @RequestBody BrandDto dto) {
|
||||
public Result<String> updateBrand(@Validated(UpdateGroup.class) @RequestBody BrandDto dto) {
|
||||
brandService.updateBrand(dto);
|
||||
return Result.success(ResultCodeEnum.UPDATE_SUCCESS);
|
||||
}
|
||||
|
|
|
@ -1,6 +1,9 @@
|
|||
package com.mall.product.domain.dto;
|
||||
|
||||
import com.mall.common.domain.dto.valid.ValidationGroups;
|
||||
import com.mall.common.domain.dto.valid.custom.ListValue;
|
||||
import com.mall.common.domain.dto.valid.group.AddGroup;
|
||||
import com.mall.common.domain.dto.valid.group.DeletedGroup;
|
||||
import com.mall.common.domain.dto.valid.group.UpdateGroup;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
|
@ -18,21 +21,22 @@ import org.hibernate.validator.constraints.URL;
|
|||
public class BrandDto {
|
||||
|
||||
@Schema(name = "brandId", title = "品牌id")
|
||||
@NotNull(message = "品牌id未传", groups = {ValidationGroups.Update.class, ValidationGroups.DELETE.class})
|
||||
@NotNull(message = "品牌id未传", groups = {UpdateGroup.class, DeletedGroup.class})
|
||||
private Long brandId;
|
||||
|
||||
@Schema(name = "name", title = "品牌名")
|
||||
@NotBlank(message = "品牌名不能为空", groups = ValidationGroups.Update.class)
|
||||
@NotBlank(message = "品牌名不能为空", groups = {AddGroup.class, UpdateGroup.class})
|
||||
private String name;
|
||||
|
||||
@Schema(name = "logo", title = "品牌logo地址")
|
||||
@URL(message = "logo地址不合法")
|
||||
@URL(message = "logo地址不合法", groups = {AddGroup.class, UpdateGroup.class})
|
||||
private String logo;
|
||||
|
||||
@Schema(name = "descript", title = "介绍")
|
||||
private String descript;
|
||||
|
||||
@Schema(name = "showStatus", title = "显示状态[0-不显示;1-显示]")
|
||||
@ListValue(vals = {0, 1}, groups = {AddGroup.class, UpdateGroup.class})
|
||||
private Integer showStatus;
|
||||
|
||||
@Schema(name = "firstLetter", title = "检索首字母")
|
||||
|
|
Loading…
Reference in New Issue