mirror of
https://github.moeyy.xyz/https://github.com/GeWuYou/forgeboot
synced 2025-10-28 05:38:55 +08:00
feat(validation): Add data validation annotations and utility classes
- Added annotations such as ValidatedForAdd and ValidatedForUpdate for verification of different operations - Add a ValidationGroups object to define the validation grouping interface - Implement the ValidationUtils utility class to provide validation functionality - Create .gitattributes and .gitignore files to standardize the code repository - Add dependencies related to build.gradle.kts configuration validation
This commit is contained in:
parent
d31e47d1f8
commit
d8fe54db38
3
forgeboot-webmvc/validation/.gitattributes
vendored
Normal file
3
forgeboot-webmvc/validation/.gitattributes
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
/gradlew text eol=lf
|
||||||
|
*.bat text eol=crlf
|
||||||
|
*.jar binary
|
||||||
37
forgeboot-webmvc/validation/.gitignore
vendored
Normal file
37
forgeboot-webmvc/validation/.gitignore
vendored
Normal file
@ -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/
|
||||||
5
forgeboot-webmvc/validation/build.gradle.kts
Normal file
5
forgeboot-webmvc/validation/build.gradle.kts
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
|
||||||
|
dependencies {
|
||||||
|
compileOnly(libs.springBootDependencies.bom)
|
||||||
|
compileOnly(libs.springBootStarter.validation)
|
||||||
|
}
|
||||||
@ -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
|
||||||
@ -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
|
||||||
@ -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
|
||||||
@ -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
|
||||||
@ -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
|
||||||
@ -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
|
||||||
@ -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
|
||||||
@ -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
|
||||||
|
}
|
||||||
@ -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 <T> validate(obj: T, vararg groups: Class<*>): Set<ConstraintViolation<T>> {
|
||||||
|
return validator.validate(obj, *groups)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 验证对象并抛出异常
|
||||||
|
*
|
||||||
|
* 如果对象验证不通过,则抛出ConstraintViolationException异常
|
||||||
|
* @param obj 待验证的对象
|
||||||
|
* @param groups 验证分组
|
||||||
|
* @throws ConstraintViolationException 如果存在验证错误
|
||||||
|
*/
|
||||||
|
fun <T> validateAndThrow(obj: T, vararg groups: Class<*>) {
|
||||||
|
val violations = validate(obj, *groups)
|
||||||
|
if (violations.isNotEmpty()) {
|
||||||
|
throw ConstraintViolationException(violations)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user