https://github.com/aibobrov/SwiftValidation
Type-safe extendable Swift DSL to validate your objects.
https://github.com/aibobrov/SwiftValidation
dsl swift swiftpackagemanager
Last synced: 3 months ago
JSON representation
Type-safe extendable Swift DSL to validate your objects.
- Host: GitHub
- URL: https://github.com/aibobrov/SwiftValidation
- Owner: aibobrov
- Created: 2021-02-08T03:53:54.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2021-06-07T13:18:00.000Z (about 4 years ago)
- Last Synced: 2024-12-06T11:02:46.875Z (7 months ago)
- Topics: dsl, swift, swiftpackagemanager
- Language: Swift
- Homepage:
- Size: 32.2 KB
- Stars: 13
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
- awesome-result-builders - SwiftValidation
README
# SwiftValidation
> Type-safe extendable Swift DSL to validate your objects.

Heavily inspired by Kotlin [valiktor](https://github.com/valiktor/valiktor).
Swift 5.4 DSL to create a validator for the value.
DSL begins with two top level functions `validate` and `validateAny` that conjunction and disjunction sematnics model.
In case of a validation failure `ValidationError` error will be thrown.## Usage
```swift
XCTAssertNoThrow {
try validate(user) {
validate(\.id).isPositive()
validate(\.email) {
isNotEmpty()
isEmail()
}
validate(\.dateString) { // validateAny(\.dateString) also works
any {
isDate(dateDecodingStrategy: .iso8601)
isDate(dateDecodingStrategy: .secondsSince1970)
}
}
}
}
```## Installation
### Swift Package Manager
```swift
// In your `Package.swift`dependencies: [
.package(url: "https://github.com/artbobrov/SwiftValidation", .branch("main")),
...
],
targets: [
.target(
name: ...,
dependencies: [
.product(name: "Example", package: "SwiftValidation"),
...
]
),
...
]
```## Error Handling
Validation can throw `ValidationError`.
KeyPath can be named explicitly by `validate(\.age, name: "age").equals(to: 23)` then the error will be `PartialValidationError` with `PartialValidationError.propertyName == "age"`.
Nested validation is supported.
```swift
try validate(user) {
validate(\.company, name: "company") {
validate(\.catchPhrase, name: "catchPhrase").isNotEmpty()
}
}
```In case of empty `catchPhrase` the propertyName equals to `company.catchPhrase`
## DisjunctionValidator, ConjunctionValidator
`DisjunctionValidator` has similar semantic to boolean disjunction operations.
At least one validator should accept the object to not fail.
In case of empty validators validation does nothing.`ConjunctionValidator` has similar semantic to boolean conjunction operations.
All validators should accept the object to not fail.
In case of empty validators validation does nothing.## Custom Validator
Create a function returning object implementing `Validator` protocol. Associated type `Value` is a type you want to validate.
To support dot notation create extension of `ExtendableValidator`. Associated type `Element` is a type you want to validate.
## Author
* [artbobrov](https://github.com/artbobrov)