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

<h1 align="center">SpEL Validator</h1><div align="center">「SpEL Validator」是基于 SpEL 的参数校验包,也是 javax.validation 的扩展增强包,用于简化参数校验。</div>## 项目地址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

make.ink favicon
Weekly Pick

Your AI tattoo generator for pro-grade concepts

Leo Wade
sellfy.com favicon
Weekly Pick

A code-free online store builder to turn views into revenue. Sell digital products, subscriptions, and merch, without fees or hassle.

Maris
www.videotoblog.ai favicon
Weekly Pick

Convert videos into awesome blog posts.

Video To Blog
www.sellerpic.ai favicon
Weekly Pick

SellerPic is the all-in-one AI design platform for e-commerce

SellerPic
fastimage.ai favicon
Weekly Pick

Fast Image AI instantly transforms your photos into stunning styles like Ghibli, Sketch, and Pixar. Effortlessly control image elements and create amazing effects with just one click.

Fast AI Team
linkedinpro.daisy.so favicon
Weekly Pick

AI-powered tool that transforms casual photos into professional LinkedIn headshots instantly. No photographer needed—just upload and download.

Gabriel
crevas.ai favicon
Weekly Pick

Crevas unifies Veo 3, Sora 2, Nano Banana and more into one intuitive canvas — so filmmakers can script, prompt, and generate cinematic stories without switching tools or losing consistency.

Spark Alpha
fotominiatur.com favicon
Weekly Pick

AI Foto Edit - Text to Image & Image Edit

foto miniatur