Or navigate to your Xcode project then select Swift Packages, click the “+” icon and search for ValidatedPropertyKit.
Manually
If you prefer not to use any of the aforementioned dependency managers, you can integrate ValidatedPropertyKit into your project manually. Simply drag the Sources Folder into your Xcode project.
Validated 👮♂️
The @Validated attribute allows you to specify a validation alongside to the declaration of your property.
Note: @Validated supports SwiftUI View updates and will basically work the same way as @State does.
@Validated(!.isEmpty)
var username = String()
@Validated(.hasPrefix("https"))
var avatarURL: String?
If @Validated is applied on an optional type e.g. String? you can specify whether the validation should fail or succeed when the value is nil.
@Validated(
.isURL && .hasPrefix("https"),
isNilValid: true
)
var avatarURL: String?
By default the argument nilValidation is set to .constant(false)
In addition the SwiftUI.View extension validated() allows you to disable or enable a certain SwiftUI.View based on your @Validated properties. The validated() function will disable the SwiftUI.View if at least one of the passed in @Validated properties evaluates to false.
Additionally, you can extend the Validation via conditional conformance to easily declare your own Validations.
extension Validation where Value == Int {
/// Will validate if the Integer is the meaning of life
static var isMeaningOfLife: Self {
.init { value in
value == 42
}
}
}
And apply them to your validated property.
@Validated(.isMeaningOfLife)
var number = Int()
isValid ✅
You can access the isValid state at anytime by using the underscore notation to directly access the @Validated property wrapper.
Validation Operators allowing you to combine multiple Validations like you would do with Bool values.
// Logical AND
@Validated(.hasPrefix("https") && .hasSuffix("png"))
var avatarURL = String()
// Logical OR
@Validated(.hasPrefix("Mr.") || .hasPrefix("Mrs."))
var name = String()
// Logical NOT
@Validated(!.contains("Android", options: .caseInsensitive))
var favoriteOperatingSystem = String()
Predefined Validations
The ValidatedPropertyKit comes with many predefined common validations which you can make use of in order to specify a Validation for your validated property.
KeyPath
The keyPath validation will allow you to specify a validation for a given KeyPath of the attributed property.
@Validated(.keyPath(\.isEnabled, .equals(true)))
var object = MyCustomObject()
Strings
A String property can be validated in many ways like contains, hasPrefix and even RegularExpressions.
@Validated(.isEmail)
var string = String()
@Validated(.contains("Mr.Robot"))
var string = String()
@Validated(.hasPrefix("Mr."))
var string = String()
@Validated(.hasSuffix("OS"))
var string = String()
@Validated(.regularExpression("[0-9]+quot;))
var string = String()
Equatable
A Equatable type can be validated against a specified value.
@Validated(.equals(42))
var number = Int()
Sequence
A property of type Sequence can be validated via the contains or startsWith validation.
@Validated(.contains("Mr.Robot", "Elliot"))
var sequence = [String]()
@Validated(.startsWith("First Entry"))
var sequence = [String]()
Collection
Every Collection type offers the isEmpty validation and the range validation where you can easily declare the valid capacity.
@Validated(!.isEmpty)
var collection = [String]()
@Validated(.range(1...10))
var collection = [String]()
Comparable
A Comparable type can be validated with all common comparable operators.
@Validated(.less(50))
var comparable = Int()
@Validated(.lessOrEqual(50))
var comparable = Int()
@Validated(.greater(50))
var comparable = Int()
@Validated(.greaterOrEqual(50))
var comparable = Int()
License
ValidatedPropertyKit
Copyright (c) 2022 Sven Tiigi sven.tiigi@gmail.com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
ValidatedPropertyKit
A Swift Package to easily validate your properties using Property Wrappers 👮
Features
Installation
Swift Package Manager
To integrate using Apple’s Swift Package Manager, add the following as a dependency to your
Package.swift
:Or navigate to your Xcode project then select
Swift Packages
, click the “+” icon and search forValidatedPropertyKit
.Manually
If you prefer not to use any of the aforementioned dependency managers, you can integrate ValidatedPropertyKit into your project manually. Simply drag the
Sources
Folder into your Xcode project.Validated 👮♂️
The
@Validated
attribute allows you to specify a validation alongside to the declaration of your property.If
@Validated
is applied on an optional type e.g.String?
you can specify whether the validation should fail or succeed when the value isnil
.In addition the
SwiftUI.View
extensionvalidated()
allows you to disable or enable a certainSwiftUI.View
based on your@Validated
properties. Thevalidated()
function will disable theSwiftUI.View
if at least one of the passed in@Validated
properties evaluates tofalse
.Validation 🚦
Each
@Validated
attribute will be initialized with aValidation
which can be initialized with a simple closure that must return aBool
value.Therefore, ValidatedPropertyKit comes along with many built-in convenience functions for various types and protocols.
Additionally, you can extend the
Validation
via conditional conformance to easily declare your own Validations.And apply them to your validated property.
isValid ✅
You can access the
isValid
state at anytime by using the underscore notation to directly access the@Validated
property wrapper.Validation Operators 🔗
Validation Operators allowing you to combine multiple Validations like you would do with Bool values.
Predefined Validations
The
ValidatedPropertyKit
comes with many predefined common validations which you can make use of in order to specify aValidation
for your validated property.KeyPath
The
keyPath
validation will allow you to specify a validation for a givenKeyPath
of the attributed property.Strings
A String property can be validated in many ways like
contains
,hasPrefix
and evenRegularExpressions
.Equatable
A
Equatable
type can be validated against a specified value.Sequence
A property of type
Sequence
can be validated via thecontains
orstartsWith
validation.Collection
Every
Collection
type offers theisEmpty
validation and therange
validation where you can easily declare the valid capacity.Comparable
A
Comparable
type can be validated with all common comparable operators.License