Closed as not planned
Description
Sometimes we need to attach additional meta-data to validation results (#48) and perform custom l10n for message (#2). Both tasks fits nicely into custom ValidationError types (also it is quite handy to get all possible error types for validation).
I have something like this in mind:
data class UserProfile(
val fullName: String,
val age: Int
)
sealed class UserValidationError: ValidationError
class AgeRestrictionValidationError(
override val dataPath: String,
userAge: Int,
requiredAge: Int
) : UserValidationError {
override val message = "User must be at least $requiredAge years, but found $userAge"
}
fun ValidationBuilder<User, UserValidationError>.minimumAge(ageRequirement: Int) {
// Pass currentPath from constraint?
addConstrains(AgeRestrictionValidationError(currentPath, it.age, ageRequirement)) { it.age > ageRequirement}
}
// Add returned errors superclass to generic parameter
val validator = Validation<User, UserValidationError> {
UserProfile::age {
minimumAge(18)
}
}
Maybe there is a cleaner way to achieve this in DSL (for example - avoid overloading ValidationBuilder with custom validations).
Metadata
Assignees
Labels
No labels