konform-kt / konform

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

Sets the dataPath for the parent #76

Closed shihuili1218 closed 18 hours ago

shihuili1218 commented 1 year ago
data class Baby(
    val age: Int
)

The current error message is '. age must be less than 5 '. I add Baby before .age. e.g: 'Baby. age must be less than 5'

shihuili1218 commented 1 year ago

@nlochschmidt @sksamuel @jillesvangurp @wtomi

Could you please give me some guidance?

dhoepelman commented 4 days ago

How would you expect this to interact with nested validations?

data class Baby(val age: Int)
val babyValidation = Validation<Baby> {
   path = Baby::class
   Baby::age { exclusiveMaximum(5) } 
}
val invalidBaby = Baby(5)

// "Baby.age must be less than 5"
println(babyValidation.validate(notBaby).errors)

data class Parent(val babies: List<Baby>)
val parentValidation = Validation<Parent> { babies onEach { run(babyValidation) } }
val invalidParent = Parent(listOf(invalidBaby))

println(parentValidation.validate(invalidParent).errors)
// ".babies[0].age must be less than 5"
// ".babies[0].Baby.age must be less than 5"
dhoepelman commented 3 days ago

Likely adding this to 0.9.0 with API:

val validation: Validation<Baby> { ... }
val validationWithClass = validation.prependPath(PathSegment.toPathSegment(Baby::class)))

// ".age must be less than 5"
println(validation.validate(notBaby).error)
// "Baby.age must be less than 5"
println(validationWithClass.validate(notBaby).error)
dhoepelman commented 18 hours ago

Fixed by #155, to be released in 0.9.0