https://github.com/heitorgcosta/quiver
Validation, searching and filtering made easy for swift.
https://github.com/heitorgcosta/quiver
carthage cocoapods filtering searching swift swift-4 swift4 validation xcode xcode9
Last synced: 9 months ago
JSON representation
Validation, searching and filtering made easy for swift.
- Host: GitHub
- URL: https://github.com/heitorgcosta/quiver
- Owner: heitorgcosta
- License: mit
- Created: 2017-09-25T14:15:11.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2019-10-07T21:46:49.000Z (over 6 years ago)
- Last Synced: 2025-04-08T13:51:37.821Z (12 months ago)
- Topics: carthage, cocoapods, filtering, searching, swift, swift-4, swift4, validation, xcode, xcode9
- Language: Swift
- Homepage:
- Size: 66.4 KB
- Stars: 27
- Watchers: 6
- Forks: 3
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Quiver
[](https://opensource.org/licenses/MIT)


[](https://img.shields.io/cocoapods/v/Quiver.svg)
[](https://github.com/Carthage/Carthage)
[](https://travis-ci.org/heitorgcosta/Quiver)
[](https://codecov.io/gh/heitorgcosta/Quiver)
[](https://codebeat.co/projects/github-com-heitorgcosta-quiver-master)
Quiver is a library that provides an easy way to validate, search and filter objects.
# Installation
## Cocoapods
```
pod 'Quiver', '~> 1.2'
```
## Carthage
```
github "heitorgcosta/Quiver" ~> 1.2
```
# Usage
## Validating
Objects can be easily validated by implementing the `Validatable` protocol.
To do so, `validations(with:)` must be implemented. The mapping is made with the new Swift 4 smart keypaths.
```swift
struct Person {
var name: String?
var age: Int?
}
extension Person: Validatable {
func validations(with mapper: ValidatorMapper) {
// Name is required and must be at least 4 characters long.
mapper[\Person.name] = [.required(),
.length(min: 4)]
// Age is required and must be 18 or over
mapper[\Person.age] = [.required(),
.greaterOrEqual(to: 18)]
}
}
```
Any object implementing `Validatable` can call `validate()`, which returns a result object.
```swift
let person = Person(name: "Hector", age: 23)
let result = person.validate()
print(result.success) // Prints 'true'
```
The results also contains the errors occurred.
```swift
let person = Person(name: "Hector", age: nil)
let result = person.validate()
print(result.success) // Prints 'false'
print(result.error.items.count) // Prints 1, since it does not fulfill the 'required' validation
```
Also, each validator can contain a custom message defined by you.
```swift
extension Person: Validatable {
func validations(with mapper: ValidatorMapper) {
mapper[\Person.name] = [.required(message: "The name is required"),
.length(min: 4, message: "The name should be at least 4 characters long")]
mapper[\Person.age] = [.required(message: "The age is required"),
.greaterOrEqual(to: 18, message: "The age should be 18 or over")]
}
}
let person = Person(name: "Heitor", age: 17)
let result = person.validate()
print(result.error.firstItem?.message ?? "No errors found") // Will print 'The age should be 18 or over'
```
Validators are still in the beginning and more will be added in the future.
## Searching
First, objects to be searched must implement the `Searchable` protocol. The `searchableFields()` function should return all searchable properties.
```swift
struct Person {
var name: String
var age: Int
}
extension Person: Searchable {
func searchableFields() -> [Any?] {
return [name, age]
}
}
```
Then, any array of this object can use the `search()` function to get an array with the matching results.
```swift
// If any searchable field of a person contains "John", it is returned in the result array.
let results = personsArray.search(with: "John") // Search is not case sensitive by default.
let caseSensitiveResults = personsArray.search(with: "John", caseSensitive: true) // Explicit case sensitivity
```
## Filtering
An array of objects can be filtered using the validators included in this library using the `filter(by:with:)`, using a keypath and an array of validators.
```swift
let persons: [Person] = [] // Just imagine a great collection of persons
let filtered = persons.filter(by: \Person.name, with: [.length(min: 4)]) // Filter persons that contains name with length of at least 4 characters
```
# License
Quiver is released under the [MIT License](https://opensource.org/licenses/MIT).