konform-kt/konform

Introspection to create input field validation in HTML DSL

Closed this issue · 1 comments

I suspect it is not possible, but I have to check: I want to use Konform with HTML DSL and generate HTML form validation information (required, minlength etc). But to do that I would need some kind of introspection of which validations is applied to an Validation/Object.

Is it possible? It would make it really useful in combination with other things. 😃

Found this so suspect it's not possible, but honestly not that intimate with deep Kotlin details: #27

Hey @anderssv ,

this isn't really possible for the reasons outlined in the issue you linked. Konform has the validation logic as code, not in a way that could be used the generate minlenght="..." etc. Changing this to something that is declarative and could be used to generate HTML forms, JSON schema et.c is way out of scope.

You could do something like this yourself and convert it into both konform and html form validation:

data class HtmlFormAndKonformValidation(
   val minlength: Int? = null
   // ..
) {
  private val me get() = this

  fun toHtmlFormValidation() = // ...

  val konform = Validation<String> {
    if(me.minlength != null) minlength(me.minlength)
  }
}

data class MyThing(val foo: String)
val myThingFooValidation = HtmlFormAndKonformValidation(minlenght = 5)

val myThingKonformValidation = Validation<MyThing> {
  MyThing::foo { run(myThingFooValidation.konform) }
}