-
Notifications
You must be signed in to change notification settings - Fork 41
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow putting hints on empty required block #36
Comments
We now use this custom validation this in order to specify the fun <T : Any?> ValidationBuilder<T>.required() = addConstraint("is required") {
if (it == null) {
false
} else {
when (it) {
is String -> it.isNotBlank()
is Collection<*> -> it.isNotEmpty()
else -> true
}
}
} Which we use like this: FormDto::estimatedAnnualSales { required() hint "Estimated sales is required" } // Note we control the error message
FormDto::estimatedAnnualSales ifPresent { minimum(0) hint "Estimated sales cannot be negative" } Where before we'd do something like this: FormDto::estimatedAnnualSales required { minimum(0) hint "Estimated sales cannot be negative" }
// Note we have no control over the error given when `estimatedAnnualSales` is omitted, it will always be "is required" |
And we now use this one as well: /** Convenience method, same as `required()` but takes a name. */
fun <T : Any?> ValidationBuilder<T>.requiredAs(name: String) = addConstraint("{0} is required", name) {
if (it == null) {
false
} else {
when (it) {
is String -> it.isNotBlank()
is Collection<*> -> it.isNotEmpty()
else -> true
}
}
} |
Awesome that you found a solution that works using the existing extension points 👏
I agree, that doesn't look very good to me So additionally to the one design option you proposed there are at least two other design options that I think are interesting: Option 1: Your proposed option DealFormDto::closedBy required { hint "Closed by is missing" } Option 2: Using optional DealFormDto::closedBy required(hint = "Closed by is missing") { } Option 3: Using infix after the required block DealFormDto::closedBy required { } hint "Closed by is missing" I think Option 3 is most in line with the existing idioms and, might possibly be easier to implement as well. |
Option 3 makes sense. Currently many things return |
API for this introduced in #172, option 1 turned out to be the only viable one |
From the README it is not clear how I put a hint on an empty required block.
I get the "is required" default message when
closedBy
is not present. But how to set my own message?This does not work:
The text was updated successfully, but these errors were encountered: