Let's imagine we have two classes
@JvmInline
value class ValueClass(val integer: Int)
data class WrapperClass(val valueClass: ValueClass)
And then will try this test
@Test
fun `exclude path from value class`() {
val valueClass = ValueClass(0)
val wrapperClass = WrapperClass(valueClass)
val validation = Validation {
WrapperClass::valueClass {
ValueClass::integer {
minimum(1)
}
}
}
val result = validation.validate(wrapperClass)
println(result.errors[0].dataPath)
}
As a result, we will have dataPath equal to this - .valueClass.integer
In general, it is completely correct, but I need this dataPath as a path to the error field in my json. This json is serialized with kotlinx.serialization that supports value(inline) classes, so the real path in the json is - .valueClass, without integer.
I have found multiple solutions for my problem:
//1
Validation<ValueClass> { constrain("minimum 1", test = { it.integer >= 1 }) }
//2
Validation<ValueClass> { validate(ValidationPath.EMPTY, { it.integer }, { minimum(1) }) }
//3
fun <T, R> ValidationBuilder<T>.noPath(
property: KProperty1<T, R>,
init: ValidationBuilder<R>.() -> Unit
): Unit = validate(ValidationPath.EMPTY, property, init)
Validation<ValueClass> { noPath(ValueClass::integer) { minimum(1) } }
The third one is the most convenient in my opinion. So I believe something like this can be added to the library for other people.
Let's imagine we have two classes
And then will try this test
As a result, we will have
dataPathequal to this -.valueClass.integerIn general, it is completely correct, but I need this
dataPathas a path to the error field in myjson. This json is serialized withkotlinx.serializationthat supports value(inline) classes, so the real path in the json is -.valueClass, withoutinteger.I have found multiple solutions for my problem:
The third one is the most convenient in my opinion. So I believe something like this can be added to the library for other people.