Comments (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.
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.
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.
Seems I am not alone, ;)
from konform.
from konform.
Version 0.2.0 now exposes the errors in the ValidationResult.
The readme has been updated as well.
from konform.
Related Issues (20)
- Kotlin 2 & multiplatform support HOT 1
- Merge or close old pull requests and issues HOT 4
- Fix release process HOT 4
- Manual approval required for workflow run 9036441614: Release version '0.5.0' HOT 2
- Provide integration with ktor
- Manual approval required for workflow run 9337925636: Release version '0.6.0' HOT 2
- Missing artifacts for the darwin targets. HOT 5
- Missing artifact for iosSimulatorArm64 target HOT 2
- Check a custom condition command HOT 1
- Add support for other regex matching functions
- Consider applying for a grant (closes May 15 2023) HOT 1
- Recursive validation support HOT 2
- Verification of multiple field combinations
- Sets the dataPath for the parent HOT 1
- Project abandoned? HOT 5
- Library best practices
- CI/CD & Gradle improvements
- Manual approval required for workflow run 8522115848: Release version '0.5.0-SNAPSHOT' HOT 3
- Question: Smart constructor pattern / composable validations HOT 3
- Introspection to create input field validation in HTML DSL HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from konform.