Comments (5)
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.
How can I override/extend "isEmail" or any other regEx Validation to show a custom error message?
from validatedpropertykit.
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.
@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.
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)
- What is the use of `AnyValidatable` struct? HOT 2
- Referencing initializer 'init(wrappedValue:_:nilValidation:)' on 'Validated' requires that 'Published<String>' conform to 'Optionalable' HOT 3
- Sugestivní
- Support for iOS 13.x? HOT 1
- Example that works with MVVM HOT 1
- Validation Error support HOT 4
- Validation on UITextField and UITextView HOT 1
- SwiftUI Integration HOT 2
- Please update the docs to add a real world example HOT 2
- Support validation for non NIL fields HOT 2
- Doesn't seems to work on latest iOS version ( 14.2 ) HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from validatedpropertykit.