Type-based input validation.

Related tags

Miscellaneous ensure
Overview

Ensure

Type-based input validation

try Ensure<PackageIsCool>(wrappedValue: packages.ensure)

Validators

A Validator is a type that validates an input. You can create a Validator like so:

struct GreaterThanZero : Validator {
  static func validate(_ value: Int) throws {
    guard value > 0 else { throw ValidationError("must be > 0") }
  }
}

There are tons of built-in validators. You can combine them using various operations:

Not<EqualTo<Int._5>>.And<GreaterThanZero>

Dependent Types

A DependentType is a type that represents a value. Several are provided for some builtin types:

Int._0 // 0
Double._10 // 10
String.Space // " "
Bool.True // true

You can create a custom DependentType by implementing the protocol:

extension String {
  enum HelloWorld : DependentType {
    public static let value: String = "Hello, world!"
  }
}

Built-in Validators

Composition

And

// value > 0 && value < 5
GreaterThan<Int._0>.And<LessThan<Int._5>>

Or

// value < 0 || value > 5
LessThan<Int._0>.Or<GreaterThan<Int._5>>

Not

Not<EqualTo<Int._0>> // value != 0

Comparison

EqualTo

EqualTo<Int._5> // == 5
EqualTo<String.HelloWorld> // == "Hello, world!"

LessThan

LessThan<Int._5> // < 5

GreaterThan

GreaterThan<Int._5> // > 5

LessThanOrEqualTo

LessThanOrEqualTo<Int._5> // <= 5

GreaterThanOrEqualTo

GreaterThanOrEqualTo<Int._5> // >= 5

Collections

Collection.[CountIs|Count]

// [String].count > 5
Array<String>.CountIs<GreaterThan<Int._5>>
// [String].map(\.count).contains(5)
Array<String>.Mapped<Array<Int>.Count, Array<Int>.Contains<Int._5>>

Collection.IsEmpty

Not<String.IsEmpty> // !String.isEmpty

Collection.[Contains|ContainsWhere]

// [String].contains("Hello, world!")
Array<String>.Contains<String.HelloWorld>
// [String].contains(where: { $0.count > 0 })
Array<String>.ContainsWhere<String.CountIs<GreaterThan<Int._0>>>

Collection.AllSatisfy

// [String].allSatisfy { $0.count > 0 }
Array<String>.AllSatisfy<String.CountIs<GreaterThan<Int._0>>>

Collection.Mapped

// [String].map(\.count).contains(5)
Array<String>.Mapped<Array<Int>.Count, Array<Int>.Contains<Int._5>>

Collection.Reduced

// [Int].reduce(0, +) > 5
Array<Int>.Reduced<Int._0, Operator.Add, GreaterThan<Int._5>>
You might also like...
This repository hosts the PushwooshGeozones iOS SDK as an XCFramework based Swift Package.

This repository hosts the PushwooshGeozones iOS SDK as an XCFramework based Swift Package. Use this repository's Swift Package in Xcode 12+

A navigation frameword based on the Coordinator pattern and is a compact version from XCoordinator.

Coordinator A navigation frameword based on the Coordinator pattern and is a compact version from XCoordinator. Example To run the example project, cl

iPad Comic reader based on ComicFLow

Overview Finally a comic reader for iPad done right! ComicFlow was designed with one goal in mind: providing the best experience for comic fans with l

A GUI based virtualisation tool for running Linux on macOS Big Sur (x86 or arm64)
A GUI based virtualisation tool for running Linux on macOS Big Sur (x86 or arm64)

Project Mendacius GUI based virtualization tool to run Linux, based on the Virtualization framework introduced by Apple for macOS Big Sur with support

UIKit-based app project template for Swift Playgrounds 4
UIKit-based app project template for Swift Playgrounds 4

playgrounds-uikit-app This is a simplistic sample template for Swift Playgrounds 4 to begin with a UIKit-based app delegate & window scene instead of

OpenAPI specification generator for Vapor based Swift projects.

VaporToOpenAPI VaporToOpenAPI is a Swift library which can generate output compatible with OpenAPI version 3.0.1 from Vapor code. You can use generate

GraphQL based Jetpack Compose, Wear Compose and SwiftUI Kotlin Multiplatform sample
GraphQL based Jetpack Compose, Wear Compose and SwiftUI Kotlin Multiplatform sample

GraphQL based Jetpack Compose, Wear Compose and SwiftUI Kotlin Multiplatform sample

🐦 An asynchronous Twitter library based on the Twitter v2 API

Twift Twift is an asynchronous Swift library for the Twitter v2 API. No external dependencies Only one callback-based method (requestUserCredentials)

Contacts is an iOS app based on MVP (Model View Presenter) software architectural pattern.

Contacts Description Contacts is an iOS app based on MVP (Model View Presenter) software architectural pattern. Run Requirements Xcode 10.2.1 Swift 5.

Owner
Build Passed
Build Passed
A nano-sized weather station based on a Raspberry Pi with an API, iOS & Mac Catalyst app, and sensor-based automations.

Nanotool A nano-sized weather station based on a Raspberry Pi with an API, iOS & Mac Catalyst app, and sensor-based automations. Descriere Vremea este

CMD 3 Aug 18, 2022
🌳 Environment – a nicer, type-safe way of working with environment variables in Swift.

?? Environment Welcome to Environment – a nicer, type-safe way of working with environment variables in Swift. Usage Access Environment Variables The

Will Lisac 31 Dec 17, 2022
SwiftTypeReader - You can gather type definitions from Swift source code.

SwiftTypeReader - You can gather type definitions from Swift source code.

omochimetaru 23 Dec 12, 2022
React Native utility library around image and video files for getting metadata like MIME type, timestamp, duration, and dimensions. Works on iOS and Android using Java and Obj-C, instead of Node 🚀.

Qeepsake React Native File Utils Extracts information from image and video files including MIME type, duration (video), dimensions, and timestamp. The

Qeepsake 12 Oct 19, 2022
MQTagged provides a Tagged structure which helps differentiating between values of the same type.

MQTagged provides a Tagged structure which helps differentiating between values of the same type.

Miquido 2 Jun 29, 2022
The Swift code generator for your assets, storyboards, Localizable.strings, … — Get rid of all String-based APIs!

SwiftGen SwiftGen is a tool to automatically generate Swift code for resources of your projects (like images, localised strings, etc), to make them ty

null 8.3k Dec 31, 2022
🛠 A flexible and easy template created to speed up the development of your iOS application based on the MVP pattern.

Model-View-Presenter template A flexible and easy template created to speed up the development of your iOS application based on the MVP pattern. This

Tamerlan Satualdypov 22 Nov 9, 2022
Control Nvidia jetbot which is based on Jetson Nano via iOS app

jetbot-remote-control This is very basic example project which control Nvidia Jetbot remotely via iOS app Based on jetbot of Nvidia, adding sample sou

Nguyen Quoc Thanh 13 Oct 6, 2021
XCode Preview template for UIkit based project.

SwiftPreview XCode Preview template for UIkit based project. Support custom file template class inherit from UIView and UIViewController. How to use?

Won Heo 10 Mar 31, 2022
Lightweight app to view your WoT (BB, Blitz) stats (XVM based)

KTTC Lite Приложение для танкистов, следящих за своей статистикой! Функционал Базовая статистика аккаунта WoT, WoT Blitz Расширенная статистика XVM (W

Ярослав Стрельников 3 Aug 31, 2022