ProductPromotion
Logo

Swift

made by https://0x3d.site

GitHub - SvenTiigi/ValidatedPropertyKit: Easily validate your Properties with Property Wrappers ๐Ÿ‘ฎ
Easily validate your Properties with Property Wrappers ๐Ÿ‘ฎ - SvenTiigi/ValidatedPropertyKit
Visit Site

GitHub - SvenTiigi/ValidatedPropertyKit: Easily validate your Properties with Property Wrappers ๐Ÿ‘ฎ

GitHub - SvenTiigi/ValidatedPropertyKit: Easily validate your Properties with Property Wrappers ๐Ÿ‘ฎ

import SwiftUI
import ValidatedPropertyKit

struct LoginView: View {

    @Validated(!.isEmpty && .isEmail)
    var mailAddress = String()

    @Validated(.range(8...))
    var password = String()

    var body: some View {
        List {
            TextField(
               "E-Mail",
               text: self.$mailAddress
            )
            if self._mailAddress.isInvalidAfterChanges {
                Text(verbatim: "Please enter a valid E-Mail address.")
            }
            TextField(
               "Password",
               text: self.$password
            )
            if self._password.isInvalidAfterChanges {
                Text(verbatim: "Please enter a valid password.")
            }
            Button {
               print("Login", self.mailAddress, self.password)
            } label: {
               Text(verbatim: "Submit")
            }
            .validated(
                self._mailAddress,
                self._password
            )
        }
    }

}

Features

  • Easily validate your properties ๐Ÿ‘ฎ
  • Predefined validations ๐Ÿšฆ
  • Logical Operators to combine validations ๐Ÿ”—
  • Customization and configuration to your needs ๐Ÿ’ช

Installation

Swift Package Manager

To integrate using Apple's Swift Package Manager, add the following as a dependency to your Package.swift:

dependencies: [
    .package(url: "https://github.com/SvenTiigi/ValidatedPropertyKit.git", from: "0.0.6")
]

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.

@Validated(!.isEmpty && .contains("@"))
var mailAddress = String()

@Validated(.range(8...))
var password = String()

Button(
   action: {},
   label: { Text("Submit") }
)
.validated(self._mailAddress && self._password)

By using the underscore notation you are passing the @Validated property wrapper to the validated() function

Validation ๐Ÿšฆ

Each @Validated attribute will be initialized with a Validation which can be initialized with a simple closure that must return a Bool value.

@Validated(.init { value in
   value.isEmpty
})
var username = String()

Therefore, ValidatedPropertyKit comes along with many built-in convenience functions for various types and protocols.

@Validated(!.contains("Android", options: .caseInsensitive))
var favoriteOperatingSystem = String()

@Validated(.equals(42))
var magicNumber = Int()

@Validated(.keyPath(\.isEnabled, .equals(true)))
var object = MyCustomObject()

Head over the Predefined Validations section to learn more

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.

@Validated(!.isEmpty)
var username = String()

username = "Mr.Robot"
print(_username.isValid) // true

username = ""
print(_username.isValid) // false

Validation Operators ๐Ÿ”—

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]+$"))
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 [email protected]

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.

More Resources
to explore the angular.

mail [email protected] to add your project or resources here ๐Ÿ”ฅ.

Related Articles
to learn about angular.

FAQ's
to learn more about Angular JS.

mail [email protected] to add more queries here ๐Ÿ”.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory