- April 27, 2024
- Mins Read
let constraint = TypeConstraint<Account, Account.Error> {
KeyPathConstraint(\.username) {
BlockConstraint {
$0.count >= 5
} errorBuilder: {
.username
}
}
KeyPathConstraint(\.password) {
GroupConstraint(.all) {
PredicateConstraint {
.characterSet(.lowercaseLetters, mode: .inclusive)
} errorBuilder: {
.password(.missingLowercase)
}
PredicateConstraint{
.characterSet(.uppercaseLetters, mode: .inclusive)
} errorBuilder: {
.password(.missingUppercase)
}
PredicateConstraint {
.characterSet(.decimalDigits, mode: .inclusive)
} errorBuilder: {
.password(.missingDigits)
}
PredicateConstraint {
.characterSet(CharacterSet(charactersIn: “!?@#$%^&*()|\\/<>,.~`_+-=”), mode: .inclusive)
} errorBuilder: {
.password(.missingSpecialChars)
}
PredicateConstraint {
.length(min: 8)
} errorBuilder: {
.password(.tooShort)
}
}
}
BlockConstraint {
$0.password == $0.passwordConfirmation
} errorBuilder: {
.password(.confirmationMismatch)
}
KeyPathConstraint(\.email) {
PredicateConstraint(.email, error: .email)
}
KeyPathConstraint(\.age) {
PredicateConstraint(.range(min: 14), error: .underAge)
}
KeyPathConstraint(\.website) {
PredicateConstraint(.url, error: .website)
.optional()
}
}
let result = constraint.evaluate(with: account)
switch result {
case .success:
handleSuccess()
case .failure(let summary):
handleErrors(summary.errors)
}
Peppermint
is a declarative and lightweight data validation framework.
At the core of it, there are 2 principles:
Every project is unique in it’s own challenges and it’s great when we can focus on solving them instead of spending our time on boilerplate tasks.
With this idea in mind, the framework follows the Protocol Oriented Programming paradigm and was designed from a small set of protocols and structures that can easily be composed to fit your project needs. Thus, you can think of Peppermint
as an adjustable wrench more than a Swiss knife.
Since validation can take place at many levels, Peppermint
is available on iOS, macOS, tvOS, watchOS and native Swift projects, such as server-side apps.
Peppermint
is available only through Swift Package Manager.
You can add Peppermint
to your project in Xcode by going to File > Swift Packages > Add Package Dependency
.
Or, if you want to use it as a dependency to your own package, you can add it to your Package.swift
file:
import PackageDescription
let package = Package(
name: “YOUR_PROJECT_NAME”,
targets: [],
dependencies: [
.Package(url: “https://github.com/nsagora/peppermint”, majorVersion: 1),
]
)
For a comprehensive list of examples try out the Examples.playground
:
Examples
playground from the Project navigatorThe Peppermint
framework is compact and offers you the foundation you need to build data validation around your project needs. In addition, it includes a set of common validation predicates and constraints that most projects can benefit off.
The Predicate
represents the core protocol
and has the role to evaluate
if an input matches on a given validation condition.
At the core of Peppermint
there are the following two predicates, which allows you to compose predicates specific to the project needs:
In addition, the framework offers a set of common validation predicates that your project can benefit of:
On top of that, developers can build more advanced or complex predicates by extending the Predicate
protocol, and/ or by composing or decorating the existing predicates:
A PredicateConstraint
represents a data type that links a Predicate
to an Error
, in order to provide useful feedback for the end users.
A BlockConstraint
represents a data type that links a custom validation closure to an Error
that describes why the evaluation has failed. It’s a shortcut of a PredicateConstraint
that is initialised with a BlockPredicate
.
A GroupConstraint
represents a composition of constraints that allows the evaluation to be made on:
To provide context, a GroupConstraint
allows us to constraint a piece of data as being required and also as being a valid email.
Horizon SDK is a state of the art real-time video recording / photo shooting iOS library. Some of the features ...