Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting all errors for a result #3

Closed
oripwk opened this issue May 5, 2018 · 6 comments
Closed

Getting all errors for a result #3

oripwk opened this issue May 5, 2018 · 6 comments
Labels
enhancement New feature or request

Comments

@oripwk
Copy link

oripwk commented May 5, 2018

Currently a ValidationResult<T> can be used to get errors for a specific field:

result[UserProfile::fullName]

Is there any way to get a map of all errors for a given validation (not a separate list for every field)?

@nlochschmidt
Copy link
Member

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.

@nlochschmidt nlochschmidt added the enhancement New feature or request label May 12, 2018
@wiltonlazary
Copy link

wiltonlazary commented Sep 11, 2018

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()
}

@ghost
Copy link

ghost commented Apr 14, 2019

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>>).

@jeminglin
Copy link

Seems I am not alone, ;)

@silversoul93
Copy link

#18

nlochschmidt added a commit that referenced this issue Mar 14, 2020
@nlochschmidt
Copy link
Member

Version 0.2.0 now exposes the errors in the ValidationResult.

The readme has been updated as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants