Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin + Spring Boot + @Valid not worked child object's internal object

Here's the code:

data class Father(
        @Valid
        val sonExamResult: Son.ExamResult
)

data class Son(
        val examResult:ExamResult
){
    data class ExamResult(
            @field: Size(min = 0, max = 100)
            val math:Int,
            @field: Size(min = 0, max = 100)
            val physicalEducation:Int
    )
}

How can I verify a data structure similar to the above? I try to pass a -1 to ExamResult.math, but nothing happend.

My native language is not English, I am very sorry for the word error.

Thank you for your help!

like image 260
HarrisonQi Avatar asked Dec 18 '25 11:12

HarrisonQi


1 Answers

The @Size uses for lists and other collections, where min and max parameters restrict its size. You need to use @Max and @Min and data class

data class Father(
    @field:Valid
    val sonExamResult: Son.ExamResult

)

data class Son(
    val examResult:ExamResult) { data class ExamResult(
        @field:Min(0)
        @field:Max(100)
        val math:Int,
        @field:Min(0)
        @field:Max(100)
        val physicalEducation:Int
)}

see also: kotlin and @Valid Spring annotation

like image 75
Margarita Bobova Avatar answered Dec 21 '25 03:12

Margarita Bobova



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!