konform-kt / konform

Portable validations for Kotlin
https://www.konform.io
MIT License
652 stars 39 forks source link

Add support for range validation #4

Closed timrobertsdev closed 2 years ago

timrobertsdev commented 5 years ago

Inspired by this reddit comment.

Instead of writing

minimum(0)
maximum(150)

the user could instead use

inRange(0..5)
between(0, 5)
betweenExclusive(0, 5)
nlochschmidt commented 5 years ago

Thanks for the MR. I started Konform with the idea of building a kind of generator to transform a limit subset of a JSON Schema code to Konform later. Therefore I started with implementing validations that are like what can be found there. The thing is, between, betweenExclusive and inRange are not part of JSON Schema, so I need to think about where to put them.

timrobertsdev commented 5 years ago

How would something like this work for you?

With this approach, you can group all validators together in a base abstract class, then expose only the ones applicable for your scenario in an object, and the only change needed for calling code is a different import. It's kind of annoying to write out the function definitions twice, but it works.

I'm not sure about the overhead of an extra function call, but I would think the compiler/JIT would optimize most of that away.

nlochschmidt commented 5 years ago

That could work.

However, since many kinds of validations can be added from the outside without actually changing the library (see #7) I would rather not include them right now. I think it will be interesting to provide multiple styles like json-schema, swagger-schema (see #6) and maybe hibernate-validator style validations before introducing a new custom konform-style set of validations.

But I'll keep this issue open as a reminder.