swift-validations

0.3.2

Build validations with result builder syntax.
m-housh/swift-validations

What's New

0.3.2

2023-02-07T00:54:09Z

Added key path support.

swift-validations

CI

A swift package for creating validations, using ResultBuilder syntax.

Additional Resources

Overview

Validations with this library can be built by declaring Validation types, using the built-in types supplied with the library, or conforming your types to the Validatable protocol.

Using Validator's

Below is an example of using a validator that you define for a given type.

struct User { 
  let name: String
  let email: String
}

let userValidator = ValidatorOf<User> {  

  Validator.validate(\.name, using: String.notEmpty())
  Validator.validate(\.email) { 
    String.notEmpty()
    String.contains("@")
  }
}

try userValidator.validate(User(name: "Blob", email: "blob@example.com")) // success.
try userValidator.validate(User(name: "", email: "blob@example.com")) // throws error.
try userValidator.validate(User(name: "Blob", email: "blob.example.com")) // throws error.

ValidatorOf is typealias of Validator for better ergonomics, however the above could also be written as Validator<User> if you'd prefer.

Conforming to Validatable

You can conform types to the Validatable protocol or AsyncValidatable, which are types that can validate an instance of itself.

Generally you will supply the Validation/body-swift.property-2e4vc property. Which uses result builder syntax.

extension User: Validatable { 

  var body: some Validator<Self> { 
    Validator { 
      Validator.validate(\.name, using: String.notEmpty())
      Validator.validate(\.email) { 
        String.notEmpty()
        String.contains("@")
      }
    }
  }

}

try User(name: "Blob", email: "blob@example.com").validate() // success.
try User(name: "", email: "blob@example.com").validate() // throws error.
try User(name: "Blob", email: "blob.example.com").validate() // throws error.

However you can also implement the Validation/validate(_:)-lqpu.

enum UserError: Error { 
  case invalidName
}

extension User: Validatable { 
  
  func validate(_ value: User) throws { 
    guard value.name != "" else {  
      throw UserError.invalidName
    }
  }
}

Documentation

Read the api documentation here.

Description

  • Swift Tools 5.7.0
View More Packages from this Author

Dependencies

Last updated: Thu Mar 14 2024 15:49:23 GMT-0900 (Hawaii-Aleutian Daylight Time)