Code Monkey home page Code Monkey logo

quiver's Introduction

Quiver

license Swift 5 Xcode 10.2 CocoaPods Compatible Carthage compatible Build Status codecov codebeat badge

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.

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.

let person = Person(name: "Hector", age: 23)
let result = person.validate()

print(result.success) // Prints 'true'

The results also contains the errors occurred.

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.

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.

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.

// 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.

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.

quiver's People

Contributors

heitorgcosta avatar lucianopalmeida avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

quiver's Issues

[BUG]Can't create custom validator.

As of October 2019, I can't create a custom validator inheriting from Validator. It seems the validate function is not declared as open, but public. When I try to subclass it and implement a custom validate function, I get the following error:

Overriding non-open instance method outside of its defining module

@heitorgcosta @LucianoPAlmeida would you kindly fix this?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.