feat(core): 新增 JSON 格式字符串校验器

This commit is contained in:
2025-04-29 22:51:42 +08:00
parent 5e9a3f3e93
commit cf5ef36af5
5 changed files with 109 additions and 5 deletions

View File

@@ -28,7 +28,7 @@ import java.util.Arrays;
import java.util.function.Function;
/**
* 枚举校验注解校验
* 枚举校验器
*
* @author Charles7c
* @author Jasmine

View File

@@ -0,0 +1,66 @@
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
* <p>
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.gnu.org/licenses/lgpl.html
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package top.continew.starter.core.validation.constraints;
import jakarta.validation.Constraint;
import jakarta.validation.Payload;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.ElementType.TYPE_USE;
/**
* JSON 格式字符串校验注解
*
* <p>
* 校验字符串是否为 JSON 格式字符串
* {@code @JsonString(message = "必须为有效的 JSON 格式")} <br />
* </p>
*
* @author Charles7c
* @since 2.12.0
*/
@Documented
@Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE})
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = JsonStringValidator.class)
public @interface JsonString {
/**
* 提示消息
*
* @return 提示消息
*/
String message() default "必须为有效的 JSON 格式";
/**
* 分组
*
* @return 分组
*/
Class<?>[] groups() default {};
/**
* 负载
*
* @return 负载
*/
Class<? extends Payload>[] payload() default {};
}

View File

@@ -0,0 +1,38 @@
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
* <p>
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.gnu.org/licenses/lgpl.html
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package top.continew.starter.core.validation.constraints;
import cn.hutool.json.JSONUtil;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
/**
* JSON 格式字符串校验器
*
* @author Charles7c
* @since 2.12.0
*/
public class JsonStringValidator implements ConstraintValidator<JsonString, String> {
@Override
public boolean isValid(String value, ConstraintValidatorContext context) {
if (value == null) {
return true;
}
return JSONUtil.isTypeJSON(value);
}
}

View File

@@ -16,13 +16,12 @@
package top.continew.starter.core.validation.constraints;
import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.util.PhoneUtil;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
/**
* 手机号校验注解校验
* 手机号校验器
*
* @author Charles7c
* @since 2.10.0
@@ -31,7 +30,7 @@ public class MobileValidator implements ConstraintValidator<Mobile, String> {
@Override
public boolean isValid(String value, ConstraintValidatorContext context) {
if (CharSequenceUtil.isBlank(value)) {
if (value == null) {
return true;
}
return PhoneUtil.isMobile(value);