Swiftpack.co -  Q42/ValidationKit as Swift Package
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
Q42/ValidationKit
A Swift library for validating user input, for example in forms.
.package(url: "https://github.com/Q42/ValidationKit.git", from: "0.0.2")

ValidationKit

A lightweight Swift library for validating user input, for example in forms.

Usage

Using the built in validators

import ValidationKit

// Validators can be combined using the operators `&&` and `||`
let tweetValidator: Validator = .notEmpty && .maxLength(280)

let tweet = "Hello, world!"
let result = tweetValidator.validate(input: tweet)

switch result {
case .valid(let value):
  print("Tweet '\(value)' is valid")
case .invalid(let error):
  print(String(format: "Tweet did not validate: %@", error.localizedDescription))
}

The built-in validators are:

  • isEmpty: String must be empty
  • notEmpty: String must not be empty
  • minLength(_ length: Int): String length must be greater than or equal...
  • maxLength(_ length: Int): String length must be less than or equal...
  • exactLength(_ length: Int): String length must be exactly...
  • isDate(formatter: DateFormatter): String must be parsable as a date using the given formatter
  • isInThePast: Date must be in the past
  • anyOf: Value must be one of certain given options
  • accepted: Value must be boolean true
  • hasPrefix: String must have a certain prefix

Implementing a custom validator

public extension Validator {
  static var isDutchPostalCode: Validator<String, String> {
    Validator<String, String> { input in
      if let range = input.range(of: #"^\d{4}\s?[a-zA-Z]{2}$"#, options: .regularExpression) {
        let output = String(input[range])
        return .valid(output)
      } else {
        return .invalid(.invalidDutchPostalCode)
      }
    }
  }
}

public extension ValidationError {
  static let invalidDutchPostalCode = ValidationError(localizedDescription: NSLocalizedString("Invalid Dutch postal code", comment: "Validation error text"))
}

let postalCodeValidator: Validator = .notEmpty && .isDutchPostalCode
let result = postalCodeValidator.validate(input: "2516 AH") // Equals ValidationResult.valid("2516 AH")

Releases

  • 0.0.1 - 2021-03-28 - Initial public release
  • 0.0.0 - 2019-12-02 - Initial private version for a project at Q42

Licence & Credits

ValidationKit is written by Mathijs Bernson and Tim van Steenis.

It is available under the MIT license, so feel free to use it in commercial and non-commercial projects.

GitHub

link
Stars: 1
Last commit: 1 week ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Release Notes

1 week ago

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API