001package cn.sticki.validator.spel.constrain;
002
003import cn.sticki.validator.spel.SpelConstraint;
004import cn.sticki.validator.spel.SpelValid;
005import cn.sticki.validator.spel.constraintvalidator.SpelAssertValidator;
006import org.intellij.lang.annotations.Language;
007
008import java.lang.annotation.*;
009
010import static java.lang.annotation.ElementType.FIELD;
011import static java.lang.annotation.RetentionPolicy.RUNTIME;
012
013/**
014 * 基于SpEL断言校验注解,用于标记被注解的元素需要满足指定的断言条件。
015 *
016 * @author 阿杆
017 * @version 1.0
018 * @since 2024/5/1
019 */
020@Documented
021@Retention(RUNTIME)
022@Target(FIELD)
023@Repeatable(SpelAssert.List.class)
024@SpelConstraint(validatedBy = SpelAssertValidator.class)
025public @interface SpelAssert {
026
027        /**
028         * 校验失败时的错误消息
029         */
030        String message() default "不能为null";
031
032        /**
033         * 约束开启条件,必须为合法的SpEL表达式,计算结果必须为boolean类型。
034         * <p>
035         * 当 表达式为空 或 计算结果为true 时,会对带注解的元素进行校验。
036         * <p>
037         * 默认情况下,开启校验。
038         */
039        @Language("SpEL")
040        String condition() default "";
041
042        /**
043         * 分组条件,必须为合法的SpEL表达式。
044         * <p>
045         * 当分组信息不为空时,只有当 {@link SpelValid#spelGroups()} 中的分组信息与此处的分组信息有交集时,才会对带注解的元素进行校验。
046         * <p>
047         * 其计算结果可以是任何类型,但只有两个计算结果完全相等时,才被认为是相等的。
048         */
049        @Language("SpEL")
050        String[] group() default {};
051
052        /**
053         * 断言语句,必须为合法的SpEL表达式。
054         * <p>
055         * 计算结果必须为boolean类型,true为校验成功,false为校验失败
056         */
057        @Language("SpEL")
058        String assertTrue() default "";
059
060        @Retention(RetentionPolicy.RUNTIME)
061        @Target(ElementType.FIELD)
062        @Documented
063        @interface List {
064
065                SpelAssert[] value();
066
067        }
068
069}