Skip to content
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

Closed
azamsharp opened this issue Mar 2, 2020 · 5 comments
Closed

How to provide the custom message for validation #7

azamsharp opened this issue Mar 2, 2020 · 5 comments
Labels
enhancement New feature or request

Comments

@azamsharp
Copy link

azamsharp commented Mar 2, 2020

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?

@SvenTiigi
Copy link
Owner

Hey @azamsharp,

Sorry for the delayed response.

Currently the default .nonEmpty validation is defined as an extension on the Validation where the Value conforms to the Collection protocol and doesn't accept any custom messages instead it defines its own:

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 ValidatedPropertyKit hasn't got an offical 1.0 release version there is definitely a lot that should be improved especially the error handling.

Please feel free to open up a PR to get things started ✌️

@SvenTiigi SvenTiigi added the enhancement New feature or request label Mar 3, 2020
@Thongvor
Copy link

How can I override/extend "isEmail" or any other regEx Validation to show a custom error message?

@SvenTiigi
Copy link
Owner

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 ValidatedPropertyKit has no official 1.0 version.

As of for now you can switch on the validatedValue from the projected value of the PropertyWrapper which is a Result type:

@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
}

@Thongvor
Copy link

Thongvor commented Aug 2, 2020

@SvenTiigi, thank you for your answer!
I found a working solution without using switch on the validatedValue,
if someone needs it:

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))}
        }
    }
}

@mikekllr
Copy link

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants