You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I was wondering if it's possible to access the validated property's value when creating custom messages files.
For example, imagine an address entity
data classAddress(
valstreet:String,
valstate:String,
valzipcode:String
)
In addition, I created a custom validation that checks that the zipcode is of a given length (user provided):
data classIsOfLength(valallowedLength:Int?) : Constraint
fun <E, T> Validator<E>.Property<T?>.isOfLength(allowedLength:Int?) =this.validate(IsOfLength(allowedLength)) { it.length == allowedLength }
And finally my messages_en.properties file:
com.myProject.constraints.IsOfLength.message=zipcode {propertyValue} must be of length {allowedLength}
Now when I validate the address:
val myAddress =Address("street", "NY", "12345")
validate(entity){
validate(Address::zipcode).isOfLength(5)
}
The message I would want to see is: zipcode 12345 must be of length 5
However, propertyValue is obviously not part of the constraint params.
My question is, did I miss something and we can easily access it in our custom message file, or the only way to do it is to pass the property value directly to the constraint and use it like that.
e.g.
IsOfLength(val propertyValue:String, val allowedLength:Int?)
This would work but we have tons of validations and making a change to all of them to add the property value is cumbersome.
An alternative approach is to just format the message text upon handling the errors in the ValidationExceptionHandler class.
Since I can access the Constraint.value in the handle function there, I could inject the value directly in the Constraint.message using a simple string format.
It's significantly better than updating all the validations we already have, but I lose the custom formatters functionality valiktor provides by doing it, and it is still something I'd like to avoid if such functionality already exists and I just missed it.
Thanks in advance!
The text was updated successfully, but these errors were encountered:
I was wondering if it's possible to access the validated property's value when creating custom messages files.
For example, imagine an address entity
In addition, I created a custom validation that checks that the zipcode is of a given length (user provided):
And finally my
messages_en.properties
file:Now when I validate the address:
The message I would want to see is:
zipcode 12345 must be of length 5
However,
propertyValue
is obviously not part of the constraint params.My question is, did I miss something and we can easily access it in our custom message file, or the only way to do it is to pass the property value directly to the constraint and use it like that.
e.g.
This would work but we have tons of validations and making a change to all of them to add the property value is cumbersome.
An alternative approach is to just format the message text upon handling the errors in the
ValidationExceptionHandler
class.Since I can access the
Constraint.value
in thehandle
function there, I could inject the value directly in theConstraint.message
using a simple string format.It's significantly better than updating all the validations we already have, but I lose the custom formatters functionality valiktor provides by doing it, and it is still something I'd like to avoid if such functionality already exists and I just missed it.
Thanks in advance!
The text was updated successfully, but these errors were encountered: