GithubHelp home page GithubHelp logo

Comments (6)

 avatar commented on August 28, 2024 6

This to me is the single biggest blocker to making this useable in a nice way.

I use various methods of validation, and I encapsulate them in my own interfaces. I think validation is a core part of your logic and should not be embedded in your peripheral http/server frameworks.

For the same reason, I would never expose the data classes or exceptions of an internally used framework in my own API. I will always translate them to my own data types.

Right now, if I wanted to translate the error messages raised by this framework into my own errors, I would have to do some nasty iteration involving reflection on my own classes and their fields.

Please expose the collection of all errors (Map<List<String>, List<String>>).

from konform.

nlochschmidt avatar nlochschmidt commented on August 28, 2024 3

Not right now.

The main issue is, that I am unsure what the most useful representation would be. One Possibility would be the current internal representation which is Map<List<String>, List<String>>. E.g.

mapOf(
  listOf("user", "fullName") to listOf("is required"),
  listOf("user", "age") to listOf("must be at least 18 years old") 

Another would be a Map<String, Any> where Any might be another Map<String, Any> or a List<String> e.g.

mapOf(
  "user" to mapOf(
    "fullName" to listOf("is required"),
    "age" to listOf("must be at least 18 years old")))

Yet another idea is to have KProperty instead of String as key and also an ValidationError class.

from konform.

wiltonlazary avatar wiltonlazary commented on August 28, 2024 2

Let folks have access to error map as it is, it is easy to transform to json actually.

@Suppress("UNCHECKED_CAST")
fun <T> Any?.cast(): T = this as T

class ModelValidationException(
    val source: Any,
    val result: Invalid<Any>,
    val tag: String = source::class.simpleName!!
) : RuntimeException() {
    companion object {
        val errorsField = Invalid::class.java.getDeclaredField("errors")

        init {
            errorsField.isAccessible = true
        }
    }

    fun toJson(): JsonElement {
        val errors = errorsField.get(result).cast<Map<List<String>, List<String>>>()

        return gson.toJsonTree(mapOf(
            "tag" to tag,
            "errors" to errors
        ))
    }

    override fun toString(): String {
        return toJson().toString()
    }

    override val message: String?
        get() = toString()
}

from konform.

jeminglin avatar jeminglin commented on August 28, 2024

Seems I am not alone, ;)

from konform.

silversoul93 avatar silversoul93 commented on August 28, 2024

#18

from konform.

nlochschmidt avatar nlochschmidt commented on August 28, 2024

Version 0.2.0 now exposes the errors in the ValidationResult.

The readme has been updated as well.

from konform.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.