mirror of https://github.com/halo-dev/halo
Refactor ValidationUtils
parent
cd9313c929
commit
ac4d50c785
|
@ -1,56 +1,85 @@
|
|||
package cc.ryanc.halo.utils;
|
||||
|
||||
import org.springframework.lang.NonNull;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
import org.springframework.validation.FieldError;
|
||||
|
||||
import javax.validation.ConstraintViolation;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import javax.validation.ConstraintViolationException;
|
||||
import javax.validation.Validation;
|
||||
import javax.validation.Validator;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* 有关字段验证的工具类
|
||||
* Object validation utilities.
|
||||
*
|
||||
* @author johnniang
|
||||
* @data 03/29/19
|
||||
*/
|
||||
public class ValidationUtils {
|
||||
|
||||
private static Validator VALIDATOR;
|
||||
|
||||
private ValidationUtils() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets validator, or create it.
|
||||
*
|
||||
* @return validator
|
||||
*/
|
||||
@NonNull
|
||||
public static Validator getValidatorOrCreate() {
|
||||
if (VALIDATOR == null) {
|
||||
synchronized (ValidationUtils.class) {
|
||||
// Init the validation
|
||||
VALIDATOR = Validation.buildDefaultValidatorFactory().getValidator();
|
||||
}
|
||||
}
|
||||
|
||||
return VALIDATOR;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates bean by hand.
|
||||
*
|
||||
* @param obj bean to be validated
|
||||
* @param groups validation group
|
||||
* @throws ConstraintViolationException throw if validation failure
|
||||
*/
|
||||
public static void validate(Object obj, Class<?>... groups) {
|
||||
|
||||
Validator validator = getValidatorOrCreate();
|
||||
|
||||
// Validate the object
|
||||
Set<ConstraintViolation<Object>> constraintViolations = validator.validate(obj, groups);
|
||||
|
||||
if (!CollectionUtils.isEmpty(constraintViolations)) {
|
||||
// If contain some errors then throw constraint violation exception
|
||||
throw new ConstraintViolationException(constraintViolations);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 将字段验证错误转换为标准的map型,key:value = field:message
|
||||
*
|
||||
* @param constraintViolations constraint violations(contain error information)
|
||||
* @return 如果返回null则未出现错误
|
||||
* @return error detail map
|
||||
*/
|
||||
public static Map<String, String> mapWithValidError(Set<ConstraintViolation<Object>> constraintViolations) {
|
||||
Map<String, String> errMap = null;
|
||||
if (!CollectionUtils.isEmpty(constraintViolations)) {
|
||||
// if not empty
|
||||
errMap = new HashMap<>(4);
|
||||
for (ConstraintViolation<Object> constraintViolation : constraintViolations) {
|
||||
errMap.put(constraintViolation.getPropertyPath().toString(), constraintViolation.getMessage());
|
||||
}
|
||||
@NonNull
|
||||
public static Map<String, String> mapWithValidError(Set<ConstraintViolation<?>> constraintViolations) {
|
||||
if (CollectionUtils.isEmpty(constraintViolations)) {
|
||||
return Collections.emptyMap();
|
||||
}
|
||||
return errMap;
|
||||
}
|
||||
|
||||
public static String stringWithValidError(Set<ConstraintViolation<?>> violations, boolean allError) {
|
||||
StringBuilder errString = new StringBuilder();
|
||||
if (!CollectionUtils.isEmpty(violations)) {
|
||||
for (ConstraintViolation<?> violation : violations) {
|
||||
if (errString.length() > 0) {
|
||||
errString.append(",");
|
||||
}
|
||||
errString.append(violation.getMessage());
|
||||
if (!allError && errString.length() > 0) {
|
||||
return errString.toString();
|
||||
}
|
||||
}
|
||||
}
|
||||
return errString.toString();
|
||||
Map<String, String> errMap = new HashMap<>(4);
|
||||
// Format the error message
|
||||
constraintViolations.forEach(
|
||||
constraintViolation ->
|
||||
errMap.put(constraintViolation.getPropertyPath().toString(), constraintViolation.getMessage()));
|
||||
return errMap;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -59,35 +88,13 @@ public class ValidationUtils {
|
|||
* @param fieldErrors 字段错误组
|
||||
* @return 如果返回null,则表示未出现错误
|
||||
*/
|
||||
public static Map<String, String> mapWithFieldError(List<FieldError> fieldErrors) {
|
||||
Map<String, String> errMap = null;
|
||||
|
||||
if (!CollectionUtils.isEmpty(fieldErrors)) {
|
||||
// 如果不为空
|
||||
errMap = new HashMap<>(4);
|
||||
for (FieldError fieldError : fieldErrors) {
|
||||
errMap.put(fieldError.getField(), fieldError.getDefaultMessage());
|
||||
}
|
||||
public static Map<String, String> mapWithFieldError(@Nullable List<FieldError> fieldErrors) {
|
||||
if (CollectionUtils.isEmpty(fieldErrors)) {
|
||||
return Collections.emptyMap();
|
||||
}
|
||||
|
||||
Map<String, String> errMap = new HashMap<>(4);
|
||||
fieldErrors.forEach(filedError -> errMap.put(filedError.getField(), filedError.getDefaultMessage()));
|
||||
return errMap;
|
||||
}
|
||||
|
||||
public static String stringWithFieldError(List<FieldError> fieldErrors, boolean allError) {
|
||||
StringBuilder errString = new StringBuilder();
|
||||
if (!CollectionUtils.isEmpty(fieldErrors)) {
|
||||
// 如果不为空
|
||||
for (FieldError fieldError : fieldErrors) {
|
||||
if (errString.length() > 0) {
|
||||
errString.append(",");
|
||||
}
|
||||
errString.append(fieldError.getField()).append(":").append(fieldError.getDefaultMessage());
|
||||
if (!allError && errString.length() > 0) {
|
||||
return errString.toString();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return errString.toString();
|
||||
}
|
||||
}
|
|
@ -18,10 +18,8 @@ import org.springframework.web.bind.annotation.ResponseStatus;
|
|||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
import org.springframework.web.servlet.NoHandlerFoundException;
|
||||
|
||||
import javax.validation.ConstraintViolation;
|
||||
import javax.validation.ConstraintViolationException;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Exception handler of controller.
|
||||
|
@ -54,10 +52,10 @@ public class ControllerExceptionHandler {
|
|||
@ExceptionHandler(ConstraintViolationException.class)
|
||||
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
||||
public BaseResponse handleConstraintViolationException(ConstraintViolationException e) {
|
||||
BaseResponse<Set<ConstraintViolation<?>>> baseResponse = handleBaseException(e);
|
||||
BaseResponse<Map<String, String>> baseResponse = handleBaseException(e);
|
||||
baseResponse.setStatus(HttpStatus.BAD_REQUEST.value());
|
||||
baseResponse.setMessage("Field validation error");
|
||||
baseResponse.setData(e.getConstraintViolations());
|
||||
baseResponse.setData(ValidationUtils.mapWithValidError(e.getConstraintViolations()));
|
||||
return baseResponse;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue