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
Validation Error support #2
Comments
Hey @myurieff, Thank you for your detailed proposal good job 👍 Your proposed solution looks great and is an perfect enhancement for the I was lately thinking the same in how to improve the error propagation for ValidatedPropertyKit. Maybe another API improvement would be to allow specifying the Error type via the @Validated(.myValidation)
var property: Result<String, MyValidationError> To enable this kind of API a little bit more work has to be done and maybe it would be better to add this API as another Property Wrapper named |
Hi, sorry for commenting on this old issue :/ It would be great to have a different kind of error for each validation rule so we could switch over the error to determine which rule (or rules) failed. switch $username.validationError {
case .nonEmpty: handleEmptyUsername()
case let .range(expectedRange): handleInvalidUsernameLength(expectedRange)
case .none: print("all good")
} Cheers, Georg |
Has this been removed in a newer version? Cannot find the errorMessage Property on the Wrapper anymore? I don't know what to do with multiple validations and how to distinguish them from another to show different messages for each of them. Any solution to this? |
Hello Sven.
Wonderful project, well done!
Motivation
Solution
There are a couple of ways to do it. What first comes to my mind would be this:
The validation closure could return a
Result<Value, ValidationError>
, instead of aBool
.The property wrapper could include a
validationError
optional property that receives values upon validation and gets set to nil whenrestore()
is called.When using, we would be able to check for a validationError and if there isn't any, we can go ahead and use the value.
Here's a rough example:
Additional Remarks
This feature would require quite some changes, but all of them would be internal to the API, meaning that the users of it won't have to change anything when using the default provided validations. However, their custom ones will have to be rewritten to return
Result
s, instead ofBool
s.All of this is a result of a small pre-lunch brainstorm. I'm sure there is a way to improve it 😄 Let me know what you think :)
The text was updated successfully, but these errors were encountered: