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
How to provide the custom message for validation #7
Comments
Hey @azamsharp, Sorry for the delayed response. Currently the default public extension Validation where Value: Collection {
/// The non empty Validation
static var nonEmpty: Validation {
return .init { value in
if !value.isEmpty {
return .success
} else {
return .failure("Is empty")
}
}
}
} Of course this could be refactored into a function which should fit your needs. public extension Validation where Value: Collection {
static func nonEmpty(failureMessage: String = "Is Empty") -> Validation {
return .init { value in
if !value.isEmpty {
return .success
} else {
return .failure(.init(message: failureMessage))
}
}
}
} As Please feel free to open up a PR to get things started ✌️ |
How can I override/extend "isEmail" or any other regEx Validation to show a custom error message? |
Hey @Thongvor, unfortunately I could not find the time yet to improve the Error handling mechanism so that you can provide your own Error enum or string. Thats the reason why As of for now you can switch on the @Validated(.isEmail)
var email: String?
...
switch _email.validatedValue {
case .success(let value):
break
case .failure(let validationError):
// Use the ValidationError or use any other error message
break
} |
@SvenTiigi, thank you for your answer! public extension Validation where Value == String {
static func isEmail(errorMessage: String = "Invalid email format") -> Validation {
return .init {
return Validation.isEmail.isValid(value: $0).flatMapError {_ in
return .failure(.init(message: errorMessage))}
}
}
} |
The fix doesn't seem to work anymore. However, I found a very cumbersome alternative by setting an event in the text fields and querying the .isValid attribute. I really like the repo and it integrates seamlessly with swiftui. Is there a way to adapt the repo to be able to output error messages? |
How can I provide a custom message for validation like:
Validated(.nonEmpty("Username cannot be empty"))
var username: String?
Where can I provide "Username cannot be empty" message?
The text was updated successfully, but these errors were encountered: