Git Product home page Git Product logo

Comments (5)

SvenTiigi avatar SvenTiigi commented on June 18, 2024 1

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 ✌️

from validatedpropertykit.

Thongvor avatar Thongvor commented on June 18, 2024

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

from validatedpropertykit.

SvenTiigi avatar SvenTiigi commented on June 18, 2024

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
}

from validatedpropertykit.

Thongvor avatar Thongvor commented on June 18, 2024

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

from validatedpropertykit.

mikekllr avatar mikekllr commented on June 18, 2024

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?

from validatedpropertykit.

Related Issues (12)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.