konform-kt / konform

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

Function to retrieve the errorsMap of an Invalid ValidationResult: #18

Closed silversoul93 closed 4 years ago

silversoul93 commented 4 years ago
nlochschmidt commented 4 years ago

@silversoul93 Thanks for your contribution.

I've just released version 0.2.0 with an implementation I had done a while back but haven't been able to publish due to the recent changes around kotlin MPP setup.

There is now a public errors property on the ValidationResult which gives you access to the ValidationErrors. You can convert that to the map that you have in mind by declaring your own extension on ValidationResult:

fun ValidationResult<*>.errorMap(): Map<String, List<String>> {
    return errors.groupBy({ it.dataPath.removePrefix(".") }) { it.message }
}

note the key transform using it.dataPath.removePrefix(".") because the dataPath typically starts with a ..