https://kotlinlang.org logo
j

Jaro

05/11/2020, 10:02 AM
Hi all, I have question regarding annotations. Assume a following example
Copy code
open class A(val strings: List<String>)

class B(strings: List<String>) : A(strings)

class C(strings: List<String>) : A(strings)
these classes are used in my REST API and I would like to validate their content with JSR annotations. I want to check that the collection
strings
in class
B
is not empty using
@NotEmpty
but I can apply this validation only on the property declaration (if I’m not mistaken). What is the best Kotlin way to add this validation to class
B
? In java I would probably override getter in class B and add annotation there, can I do something similar in Kotlin?
j

Jukka Siivonen

05/11/2020, 10:12 AM
Sorry not to answer your question, but looking back my non-trivial Kotlin project using JSR annotations I would not use them at all. I think Kotlin provides far better options in this area
j

Jaro

05/11/2020, 10:19 AM
No problem with not answering the question. We are using JSR quite some time and it is not feasible to change it… I’m just new to kotlin and I do not know how to handle this problem in kotlin idiomatic way.
t

thanksforallthefish

05/11/2020, 11:30 AM
you can change
class B(strings: List<String>) : A(strings)
to
class B(@get:NotEmpty override val strings: List<String>) : A(strings)
provided you also have
open class A(open val strings: List<String>)
j

Jaro

05/11/2020, 11:47 AM
I have tough that this the way how to solve the problem, but somehow I do not like to override the property to just have this annotation.
but looks like I do not have any other option. Thank you for you help.
a

ahmad

05/12/2020, 10:41 AM
Sorry for interrupting the thread, but @Jukka Siivonen can you please elaborate more why don’t like JSR annotations with kotlin?
j

Jukka Siivonen

05/12/2020, 10:43 AM
Mostly because I think validation should not be hardcoded in types and also like I said "external" validation with Kotlin should be very clean, for example https://www.konform.io/
for trivial validation JSR works fine, but we have cases when validation needs to be different depending on object state / external state etc. and using validation groups gets really messy. Maybe better domain modeling would help with this, but I would still prefer external validation
a

ahmad

05/12/2020, 12:22 PM
Thanks a lot for the explanation
t

thanksforallthefish

05/12/2020, 12:30 PM
jsr is not the best api I ever saw, but it is actually super-convenient once you really know how it works. While I would like more a dsl api rather than an annotation api, being a standard it means it integrates awesomely with a lot of tooling to help with the non-functional requirement of validation. for instance, hibernate-validator recently added a feature to automatically transform the error path, so if you are receive a json request and validating the request, you can easily return a response where you indicate the json name of the field violating the rules, rather than the kotlin/java you would get natively