Skip to main content

CurateClick

github.com favicon

SpEL Validator

SpEL Validator是基于SpEL的Java参数校验包,扩展自javax.validation,支持复杂校验逻辑、多字段联合校验、调用静态方法和Spring Bean,几乎适用于所有参数校验场景。

Check out SpEL Validator on CurateClick

• Copy the embed code to showcase this product on your website

• Share on X to spread the word about this amazing tool

SpEL Validator

「SpEL Validator」是基于 SpEL 的参数校验包,也是 javax.validation 的扩展增强包,用于简化参数校验。
## 项目地址https://github.com/stick-i/spel-validator项目内README文档有更详细的说明,也提供了示例项目。## 💡 它解决了什么问题?- 枚举值字段校验: ```java @SpelAssert(assertTrue = " T(cn.sticki.enums.UserStatusEnum).getByCode(#this.userStatus) != null ", message = "用户状态不合法") private Integer userStatus; ```- 多字段联合校验: ```java @NotNull private Integer contentType; @SpelNotNull(condition = "#this.contentType == 1", message = "语音内容不能为空") private Object audioContent; @SpelNotNull(condition = "#this.contentType == 2", message = "视频内容不能为空") private Object videoContent; ```- 复杂逻辑校验,调用静态方法: ```java // 中文算两个字符,英文算一个字符,要求总长度不超过 10 // 调用外部静态方法进行校验 @SpelAssert(assertTrue = "T(cn.sticki.util.StringUtil).getLength(#this.userName) <= 10", message = "用户名长度不能超过10") private String userName; ```- 调用 Spring Bean(需要使用 @EnableSpelValidatorBeanRegistrar 开启Spring Bean支持): ```java // 这里只是简单举例,实际开发中不建议这样判断用户是否存在 @SpelAssert(assertTrue = "@userService.getById(#this.userId) != null", message = "用户不存在") private Long userId; ```## 📝 特点- 强大的参数校验功能,几乎支持所有场景下的参数校验。- 扩展自 javax.validation 包,只新增不修改,无缝集成到项目中。- 基于 SpEL(Spring Expression Language) 表达式,支持复杂的校验逻辑。- 支持调用 Spring Bean,可在表达式中使用注入过的 Spring Bean。- 校验时基于整个对象,支持对象内字段间的校验逻辑。- 支持自定义校验注解,可根据业务需求自定义校验逻辑。- 无需额外的异常处理,校验失败时会上报到 javax.validation 的异常体系中。- 简单易用,使用方式几乎与 javax.validation 一致,学习成本低,上手快。## 目前支持的约束注解| 注解 | 说明 | 对标 javax.validation ||:---------------:|:---------------:|:-------------------:|| `@SpelAssert` | 逻辑断言校验 | 无 || `@SpelNotNull` | 非 null 校验 | `@NotNull` || `@SpelNotEmpty` | 集合、字符串、数组大小非空校验 | `@NotEmpty` || `@SpelNotBlank` | 字符串非空串校验 | `@NotBlank` || `@SpelNull` | 必须为 null 校验 | `@Null` || `@SpelSize` | 集合、字符串、数组长度校验 | `@Size` |每个约束注解都包含三个默认的属性:- `message`:校验失败时的提示信息。- `group`:分组条件,支持 SpEL 表达式,当分组条件满足时,才会对带注解的元素进行校验。- `condition`:约束开启条件,支持 SpEL 表达式,当 表达式为空 或 计算结果为true 时,才会对带注解的元素进行校验。

Latest Weekly Picks

happyhorseai.ai favicon
Weekly Pick

Happy Horse AI Creates Videos with Motion and Storytelling

Happy Horse
promptbuilder.cc favicon
Weekly Pick

Generate, optimize, test, and manage AI prompts in one place. Turn an idea into a ready-to-use prompt in seconds.

Prompt Builder
freqz.net favicon
Weekly Pick

Your Personal K-Style Vibe Photo Studio

Freqz
webleadr.com favicon
Weekly Pick

Find and contact web design leads and businesses without websites, like local dentists, effortlessly from any location in just a few clicks.

Webleadr

the art of having something to say

pretty-scale.com favicon
Weekly Pick

How Pretty Are You? Let AI Decide.

Pretty Scale
c2story.com favicon
Weekly Pick

Create Illustrated Stories with AI

C2story

Impromptu Speech Topics & Timer