3

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!

HarrisonQi
  • 183
  • 7

1 Answers1

2

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

  • I have no problem with a simple data structure like the one you wrote. The problem is in the properties of the inner class. – HarrisonQi Jul 16 '20 at 05:42
  • It's the same. Use `@field:Valid` and `@Min` and`@Max` instead of @Size. Edited the code from the answer above. With the code above the validation does not pass for invalid data. – Margarita Bobova Jul 17 '20 at 08:13