Swiftpack.co - Package - amoriarty/ValidationsKit

ValidationsKit

The purpose of this kit is to bring vapor/validation package to iOS, by removing Vapor core dependencies from it.

Example

Will try to add validations to a User model, in order to be conform for a registration process for example.
We first have to describe it as we usually do.

struct User {
    let mail: String
    let phone: String
    let password: String
    let website: String?
    let twitter: String?
}

Then we have to make this model conform to Validatable protocol by adding the validations() function. This return a Validations object for our model and allow us to add Validators to it.
Like the Vapor package, there's already some Validators you can use (such as mail, count, empty ...) or you can define you own validation rule through a closure:

extension User: Validatable {

    static func validations() throws -> Validations<User> {
        var validations = Validations(User.self)

        // 'mail' should be a valid mail address.
        validations.add(\.mail, ["mail"], .mail)

        // 'phone' should be a valid phone number.
        validations.add(\.phone, ["phone"], .phone) 

        // 'password' should have more than 8 characters.
        validations.add(\.password, ["password"], .count(8...))

        // 'website' should be nil or be a valid url.
        validations.add(\.website, ["website"], .nil || .url)

        // 'twitter' should be nil or began by '@'.
        validations.add(\.twitter, at: ["twitter"]) { twitter in
        	guard let twitter = twitter else { return }
            guard twitter.first != "@" else { return }
            throw BasicValidationError("isn't a valid Twitter username")
        }
        return validations
    }

}

We now have a model setup and we can now call the validate() method on it.

let valid = User(mail: "example@example.com", phone: "++3642424242", password: "somepassword", website: nil, twitter: nil)
do { try valid.validate() }
catch {
	// Will never be executed because our model is valid.
}

let invalidWebsite = User(mail: "example@example.com", phone: "++3642424242", password: "somepassword", website: "notValidUrl", twitter: nil)
do { try invalidWebsite.validate() }
catch {
	print("\(error)") // 'website' isn't nil or isn't a valid url.
}

Or you can validate a single field, specified at keyPath:

try valid.validate(at: \User.mail)

You also can take a look to a demo application in the ValidationsKitDemo directory, using Cocoapods.

Installation

Cocoapods

Simply add this line in you pods dependencies:

pod 'ValidationsKit', '~> 1.1'

Swift Package Manager

Add the GitHub link in you Package.swift as a dependencies:

.package(url: "https://github.com/amoriarty/ValidationsKit", from: "1.1.0"),

Add the dependency to your target:

.target(name: "your_target_name", dependencies: ["ValidationsKit"]),

Then update you project with:

$ swift package update

Difference from Vapor package

The main difference from Vapor package is that it doesn't include the Reflectable system, that allow you to make your model conform both to Decodable and Reflectable to avoid typing the key path to show in case of errors, which can lead to errors is variable are rename in the future.

Thanks

I allow myself to thank very much Vapor community for all there works and great packages, which was really a model that I wanted to bring into iOS.

Github

link
Stars: 3
Help us keep the lights on

Dependencies

Used By

Total: 1

Releases

1.1.0 - Mar 18, 2019

This release will add the possibility to validate only one field, specified at keyPath:

try model.validate(at: \Model.field)

It also include the RangeValidator, working with a Comparable type.

1.0.0 - Mar 18, 2019

It's the first public public release of the ValidationsKit, inspired by vapor/validation. This framework is develop with the Swift Package Manager and is accessible with the SPM and Cocoapods. It include validation system for a model conforming to the Validatable protocol, with a defined set of Validators or custom closure. It also include the following Validators:

  • CharacterSetValidator: Check if all characters of a String is in the defined character set.
  • CountValidator: Check if the count of a collection is in the defined range.
  • EmptyValidator: Check if the count of a collection is empty.
  • InValidator: Check if the item is in the defined array of items.
  • MailValidator: Check if a String is a valid mail address.
  • URLValidator: Check if a String is a valid url address.
  • PhoneValidator: Check if a String is a valid phone number.
  • NilValidator: Check if an optional is nil.
  • NilIgnoringValidator: Allow assembling Validators for an optional, ignoring other Validators is the optional is nil.
  • NotValidator: Inverse the result of a Validator.
  • AndValidator: Allow assembling Validators between them with a and condition.
  • OrValidator: Allow assembling Validators between them with a or condition.