HarrisonQi
HarrisonQi

Reputation: 183

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!

Upvotes: 3

Views: 1170

Answers (1)

Margarita Bobova
Margarita Bobova

Reputation: 61

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

Upvotes: 2

Related Questions