diff --git a/forgeboot-webmvc/validation/.gitattributes b/forgeboot-webmvc/validation/.gitattributes new file mode 100644 index 0000000..8af972c --- /dev/null +++ b/forgeboot-webmvc/validation/.gitattributes @@ -0,0 +1,3 @@ +/gradlew text eol=lf +*.bat text eol=crlf +*.jar binary diff --git a/forgeboot-webmvc/validation/.gitignore b/forgeboot-webmvc/validation/.gitignore new file mode 100644 index 0000000..c2065bc --- /dev/null +++ b/forgeboot-webmvc/validation/.gitignore @@ -0,0 +1,37 @@ +HELP.md +.gradle +build/ +!gradle/wrapper/gradle-wrapper.jar +!**/src/main/**/build/ +!**/src/test/**/build/ + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache +bin/ +!**/src/main/**/bin/ +!**/src/test/**/bin/ + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr +out/ +!**/src/main/**/out/ +!**/src/test/**/out/ + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ + +### VS Code ### +.vscode/ diff --git a/forgeboot-webmvc/validation/build.gradle.kts b/forgeboot-webmvc/validation/build.gradle.kts new file mode 100644 index 0000000..313e46b --- /dev/null +++ b/forgeboot-webmvc/validation/build.gradle.kts @@ -0,0 +1,5 @@ + +dependencies { + compileOnly(libs.springBootDependencies.bom) + compileOnly(libs.springBootStarter.validation) +} diff --git a/forgeboot-webmvc/validation/src/main/kotlin/com/gewuyou/forgeboot/webmvc/validation/annotations/ValidatedForAdd.kt b/forgeboot-webmvc/validation/src/main/kotlin/com/gewuyou/forgeboot/webmvc/validation/annotations/ValidatedForAdd.kt new file mode 100644 index 0000000..905830c --- /dev/null +++ b/forgeboot-webmvc/validation/src/main/kotlin/com/gewuyou/forgeboot/webmvc/validation/annotations/ValidatedForAdd.kt @@ -0,0 +1,15 @@ +package com.gewuyou.forgeboot.webmvc.validation.annotations + +import com.gewuyou.forgeboot.webmvc.validation.group.ValidationGroups +import org.springframework.validation.annotation.Validated + +/** + *Add验证 + * + * @since 2025-05-28 22:31:13 + * @author gewuyou + */ +@Target(AnnotationTarget.VALUE_PARAMETER, AnnotationTarget.CLASS) +@Retention(AnnotationRetention.RUNTIME) +@Validated(ValidationGroups.Add::class) +annotation class ValidatedForAdd \ No newline at end of file diff --git a/forgeboot-webmvc/validation/src/main/kotlin/com/gewuyou/forgeboot/webmvc/validation/annotations/ValidatedForArchive.kt b/forgeboot-webmvc/validation/src/main/kotlin/com/gewuyou/forgeboot/webmvc/validation/annotations/ValidatedForArchive.kt new file mode 100644 index 0000000..3526694 --- /dev/null +++ b/forgeboot-webmvc/validation/src/main/kotlin/com/gewuyou/forgeboot/webmvc/validation/annotations/ValidatedForArchive.kt @@ -0,0 +1,15 @@ +package com.gewuyou.forgeboot.webmvc.validation.annotations + +import com.gewuyou.forgeboot.webmvc.validation.group.ValidationGroups +import org.springframework.validation.annotation.Validated + +/** + *Archive验证 + * + * @since 2025-05-28 22:34:20 + * @author gewuyou + */ +@Target(AnnotationTarget.VALUE_PARAMETER, AnnotationTarget.CLASS) +@Retention(AnnotationRetention.RUNTIME) +@Validated(ValidationGroups.Archive::class) +annotation class ValidatedForArchive \ No newline at end of file diff --git a/forgeboot-webmvc/validation/src/main/kotlin/com/gewuyou/forgeboot/webmvc/validation/annotations/ValidatedForDelete.kt b/forgeboot-webmvc/validation/src/main/kotlin/com/gewuyou/forgeboot/webmvc/validation/annotations/ValidatedForDelete.kt new file mode 100644 index 0000000..ba21a52 --- /dev/null +++ b/forgeboot-webmvc/validation/src/main/kotlin/com/gewuyou/forgeboot/webmvc/validation/annotations/ValidatedForDelete.kt @@ -0,0 +1,15 @@ +package com.gewuyou.forgeboot.webmvc.validation.annotations + +import com.gewuyou.forgeboot.webmvc.validation.group.ValidationGroups +import org.springframework.validation.annotation.Validated + +/** + *Delete验证 + * + * @since 2025-05-28 22:32:12 + * @author gewuyou + */ +@Target(AnnotationTarget.VALUE_PARAMETER, AnnotationTarget.CLASS) +@Retention(AnnotationRetention.RUNTIME) +@Validated(ValidationGroups.Delete::class) +annotation class ValidatedForDelete \ No newline at end of file diff --git a/forgeboot-webmvc/validation/src/main/kotlin/com/gewuyou/forgeboot/webmvc/validation/annotations/ValidatedForDisable.kt b/forgeboot-webmvc/validation/src/main/kotlin/com/gewuyou/forgeboot/webmvc/validation/annotations/ValidatedForDisable.kt new file mode 100644 index 0000000..7fbd844 --- /dev/null +++ b/forgeboot-webmvc/validation/src/main/kotlin/com/gewuyou/forgeboot/webmvc/validation/annotations/ValidatedForDisable.kt @@ -0,0 +1,15 @@ +package com.gewuyou.forgeboot.webmvc.validation.annotations + +import com.gewuyou.forgeboot.webmvc.validation.group.ValidationGroups +import org.springframework.validation.annotation.Validated + +/** + *Disable验证 + * + * @since 2025-05-28 22:33:25 + * @author gewuyou + */ +@Target(AnnotationTarget.VALUE_PARAMETER, AnnotationTarget.CLASS) +@Retention(AnnotationRetention.RUNTIME) +@Validated(ValidationGroups.Disable::class) +annotation class ValidatedForDisable \ No newline at end of file diff --git a/forgeboot-webmvc/validation/src/main/kotlin/com/gewuyou/forgeboot/webmvc/validation/annotations/ValidatedForEnable.kt b/forgeboot-webmvc/validation/src/main/kotlin/com/gewuyou/forgeboot/webmvc/validation/annotations/ValidatedForEnable.kt new file mode 100644 index 0000000..2509bb2 --- /dev/null +++ b/forgeboot-webmvc/validation/src/main/kotlin/com/gewuyou/forgeboot/webmvc/validation/annotations/ValidatedForEnable.kt @@ -0,0 +1,15 @@ +package com.gewuyou.forgeboot.webmvc.validation.annotations + +import com.gewuyou.forgeboot.webmvc.validation.group.ValidationGroups +import org.springframework.validation.annotation.Validated + +/** + *Enable验证 + * + * @since 2025-05-28 22:32:43 + * @author gewuyou + */ +@Target(AnnotationTarget.VALUE_PARAMETER, AnnotationTarget.CLASS) +@Retention(AnnotationRetention.RUNTIME) +@Validated(ValidationGroups.Enable::class) +annotation class ValidatedForEnable \ No newline at end of file diff --git a/forgeboot-webmvc/validation/src/main/kotlin/com/gewuyou/forgeboot/webmvc/validation/annotations/ValidatedForPublish.kt b/forgeboot-webmvc/validation/src/main/kotlin/com/gewuyou/forgeboot/webmvc/validation/annotations/ValidatedForPublish.kt new file mode 100644 index 0000000..09bfabe --- /dev/null +++ b/forgeboot-webmvc/validation/src/main/kotlin/com/gewuyou/forgeboot/webmvc/validation/annotations/ValidatedForPublish.kt @@ -0,0 +1,15 @@ +package com.gewuyou.forgeboot.webmvc.validation.annotations + +import com.gewuyou.forgeboot.webmvc.validation.group.ValidationGroups +import org.springframework.validation.annotation.Validated + +/** + *Publish验证 + * + * @since 2025-05-28 22:33:54 + * @author gewuyou + */ +@Target(AnnotationTarget.VALUE_PARAMETER, AnnotationTarget.CLASS) +@Retention(AnnotationRetention.RUNTIME) +@Validated(ValidationGroups.Publish::class) +annotation class ValidatedForPublish \ No newline at end of file diff --git a/forgeboot-webmvc/validation/src/main/kotlin/com/gewuyou/forgeboot/webmvc/validation/annotations/ValidatedForUpdate.kt b/forgeboot-webmvc/validation/src/main/kotlin/com/gewuyou/forgeboot/webmvc/validation/annotations/ValidatedForUpdate.kt new file mode 100644 index 0000000..48b9896 --- /dev/null +++ b/forgeboot-webmvc/validation/src/main/kotlin/com/gewuyou/forgeboot/webmvc/validation/annotations/ValidatedForUpdate.kt @@ -0,0 +1,15 @@ +package com.gewuyou.forgeboot.webmvc.validation.annotations + +import com.gewuyou.forgeboot.webmvc.validation.group.ValidationGroups +import org.springframework.validation.annotation.Validated + +/** + *Update验证 + * + * @since 2025-05-28 22:31:49 + * @author gewuyou + */ +@Target(AnnotationTarget.VALUE_PARAMETER, AnnotationTarget.CLASS) +@Retention(AnnotationRetention.RUNTIME) +@Validated(ValidationGroups.Update::class) +annotation class ValidatedForUpdate \ No newline at end of file diff --git a/forgeboot-webmvc/validation/src/main/kotlin/com/gewuyou/forgeboot/webmvc/validation/group/ValidationGroup.kt b/forgeboot-webmvc/validation/src/main/kotlin/com/gewuyou/forgeboot/webmvc/validation/group/ValidationGroup.kt new file mode 100644 index 0000000..0a57568 --- /dev/null +++ b/forgeboot-webmvc/validation/src/main/kotlin/com/gewuyou/forgeboot/webmvc/validation/group/ValidationGroup.kt @@ -0,0 +1,62 @@ +package com.gewuyou.forgeboot.webmvc.validation.group + + +/** + * 验证分组接口集合 + * + * 用于区分不同操作(添加、更新、删除)时的字段校验需求。 + * 可与 javax 或 jakarta validation 注解的 `groups` 属性配合使用。 + * + * 示例用法: + * ```kotlin + * @NotNull(groups = [ValidationGroups.Update::class]) + * val id: Long + * ``` + * - Add: 新增 + * - Update: 修改 + * - Delete: 删除 + * - Enable: 启用 + * - Disable: 禁用 + * - Publish: 发布 + * - Archive: 归档 + * @author gewuyou + * @since 2025-01-18 14:32:41 + */ +object ValidationGroups { + + /** + * 添加操作的验证分组 + */ + interface Add + + /** + * 更新操作的验证分组 + */ + interface Update + + /** + * 删除操作的验证分组 + */ + interface Delete + + /** + * 启用操作的验证分组 + */ + interface Enable + + /** + * 启用操作的验证分组 + */ + interface Disable + + + /** + * 启用操作的验证分组 + */ + interface Publish + + /** + * 归档操作的验证分组 + */ + interface Archive +} diff --git a/forgeboot-webmvc/validation/src/main/kotlin/com/gewuyou/forgeboot/webmvc/validation/util/ValidationUtils.kt b/forgeboot-webmvc/validation/src/main/kotlin/com/gewuyou/forgeboot/webmvc/validation/util/ValidationUtils.kt new file mode 100644 index 0000000..8f50318 --- /dev/null +++ b/forgeboot-webmvc/validation/src/main/kotlin/com/gewuyou/forgeboot/webmvc/validation/util/ValidationUtils.kt @@ -0,0 +1,45 @@ +package com.gewuyou.forgeboot.webmvc.validation.util + +import jakarta.validation.ConstraintViolation +import jakarta.validation.ConstraintViolationException +import jakarta.validation.Validation +import jakarta.validation.Validator +import kotlin.collections.isNotEmpty + +/** + * 验证工具类 + * + * 提供了数据对象的验证功能,使用Bean Validation规范 + * @since 2025-05-28 22:26:12 + * @author gewuyou + */ +class ValidationUtils { + // 初始化默认的Validator实例 + private val validator: Validator = Validation.buildDefaultValidatorFactory().validator + + /** + * 验证对象是否符合规范 + * + * @param obj 待验证的对象 + * @param groups 验证分组 + * @return 返回验证失败的约束违规集合 + */ + fun validate(obj: T, vararg groups: Class<*>): Set> { + return validator.validate(obj, *groups) + } + + /** + * 验证对象并抛出异常 + * + * 如果对象验证不通过,则抛出ConstraintViolationException异常 + * @param obj 待验证的对象 + * @param groups 验证分组 + * @throws ConstraintViolationException 如果存在验证错误 + */ + fun validateAndThrow(obj: T, vararg groups: Class<*>) { + val violations = validate(obj, *groups) + if (violations.isNotEmpty()) { + throw ConstraintViolationException(violations) + } + } +}