5

I got a simple data class for Kotlin

data class Person(val name: String, @get: Min(18) val age: Int)

I actually build this class from a CSV file and I read the CSV using apache CSV parser. Even I have some data which is less than 18 for age field, the test still passed no error.

Looks like this annotation is not working for Kotlin?

1
  • did your validation api enabled by spring boot? Commented Jan 25, 2018 at 12:28

2 Answers 2

4

You're currently annotating the constructor parameter with the Min annotation - I believe you should annotate the field instead, with a use-site target like this:

data class Person(val name: String, @field:Min(18) val age: Int)
Sign up to request clarification or add additional context in comments.

4 Comments

Even I change to field it's still not working, never take effect.
Does it work if you're using a Java class? Maybe it's a problem in the configuration of validation overall instead of with disk specific annotation.
Do i need to do extra configuration for the validation to be working?
This issue is only with data-classes, with normal classes it works.
1

Try adding the annotation@Validated to the class (might just work for spring beans though). These annotations do not prevent the value being set they simply allow getting a BindingResult with a list of validation errors from an existing instance. If you need to prevent a value being set you can use a custom delegate.

 val age: Int by MyDelegates.min(18)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.