All Projects → andresinaka → Swiftcop

andresinaka / Swiftcop

Licence: mit
SwiftCop is a validation library fully written in Swift and inspired by the clarity of Ruby On Rails Active Record validations.

Programming Languages

swift
15916 projects

Projects that are alternatives of or similar to Swiftcop

NeverBounceAPI-PHP
This package provides convenient methods to integrate the NeverBounce API into your project.
Stars: ✭ 22 (-95.96%)
Mutual labels:  validation, email-validation
Validatetor
Android library for fast and simple string validation
Stars: ✭ 136 (-75%)
Mutual labels:  validation, email-validation
Dss
Digital Signature Service : creation, extension and validation of advanced electronic signatures
Stars: ✭ 415 (-23.71%)
Mutual labels:  validation
Pandera
A light-weight, flexible, and expressive pandas data validation library
Stars: ✭ 506 (-6.99%)
Mutual labels:  validation
Bunny
BunnyJS - Lightweight native (vanilla) JavaScript (JS) and ECMAScript 6 (ES6) browser library, package of small stand-alone components without dependencies: FormData, upload, image preview, HTML5 validation, Autocomplete, Dropdown, Calendar, Datepicker, Ajax, Datatable, Pagination, URL, Template engine, Element positioning, smooth scrolling, routing, inversion of control and more. Simple syntax and architecture. Next generation jQuery and front-end framework. Documentation and examples available.
Stars: ✭ 473 (-13.05%)
Mutual labels:  validation
Guard
A high-performance, extensible argument validation library.
Stars: ✭ 427 (-21.51%)
Mutual labels:  validation
Anchorme.js
Tiny, fast, efficient, feature rich Javascript library to detect links / URLs / Emails in text and convert them to clickable HTML anchor links
Stars: ✭ 479 (-11.95%)
Mutual labels:  email-validation
Indicative
Indicative is a simple yet powerful data validator for Node.js and browsers. It makes it so simple to write async validations on nested set of data.
Stars: ✭ 412 (-24.26%)
Mutual labels:  validation
Accord
Accord: A sane validation library for Scala
Stars: ✭ 519 (-4.6%)
Mutual labels:  validation
Ngx Errors
A declarative validation errors module for reactive forms.
Stars: ✭ 472 (-13.24%)
Mutual labels:  validation
Enforce
Python 3.5+ runtime type checking for integration testing and data validation
Stars: ✭ 502 (-7.72%)
Mutual labels:  validation
Io Ts
Runtime type system for IO decoding/encoding
Stars: ✭ 5,086 (+834.93%)
Mutual labels:  validation
Phonenumberkit
A Swift framework for parsing, formatting and validating international phone numbers. Inspired by Google's libphonenumber.
Stars: ✭ 4,362 (+701.84%)
Mutual labels:  validation
Validator.js
⁉️轻量级的 JavaScript 表单验证,字符串验证。没有依赖,支持 UMD ,~3kb。
Stars: ✭ 486 (-10.66%)
Mutual labels:  validation
Graphql Query Complexity
GraphQL query complexity analysis and validation for graphql-js
Stars: ✭ 424 (-22.06%)
Mutual labels:  validation
Govalidator
[Go] Package of validators and sanitizers for strings, numerics, slices and structs
Stars: ✭ 5,163 (+849.08%)
Mutual labels:  validation
Typesystem
Data validation, serialization, deserialization & form rendering. 🔢
Stars: ✭ 416 (-23.53%)
Mutual labels:  validation
Mimetype
A fast golang library for MIME type and file extension detection, based on magic numbers
Stars: ✭ 452 (-16.91%)
Mutual labels:  validation
Yalfield
Custom Field component with validation for creating easier form-like UI from interface builder.
Stars: ✭ 476 (-12.5%)
Mutual labels:  validation
Validation
PHP Standalone Validation Library
Stars: ✭ 520 (-4.41%)
Mutual labels:  validation

SwiftCop

SwiftCop is a validation library fully written in Swift and inspired by the clarity of Ruby On Rails Active Record validations.

Build Status codecov.io codecov.io

Objective

Build a standard drop-in library for validations in Swift while making it easily extensible for users to create custom validations. And avoid developers from writing over and over again the same code and validations for different projects.

Features

  • Quick validations.
  • Super simple and declarative syntax.
  • Easily extensible.
  • Fully Swift 4.0

Modules

SwiftCop was built around three different concepts:

Trial

Trial is an Enum that implements the TrialProtocol

public protocol TrialProtocol {
	func trial() -> ((_ evidence: String) -> Bool)
}

We can use Trial to quickly validate strings. SwiftCop ships with a very fully featured Trial implementation. The following trials are provided:

Exclusion([String])

This validates that the attributes are not included in the evidence string.

let exclusionTrial = Trial.exclusion([".com",".ar", ".uy"])
let trial = exclusionTrial.trial()

XCTAssertFalse(trial("http://www.nytimes.com"))
XCTAssertFalse(trial("http://www.lanacion.com.ar"))
XCTAssertTrue(trial("http://www.elpais.es"))

Format(String)

This validates whether the evidence matches a given regular expression.

let formatTrial = Trial.format("^#([a-f0-9]{6}|[a-f0-9]{3})$") // hexa number with #
let trial = formatTrial.trial()

XCTAssertTrue(trial("#57b5b5"))
XCTAssertFalse(trial("57b5b5"))
XCTAssertFalse(trial("#h7b5b5"))

Inclusion([String])

This validates that the attributes are included in the evidence string.

let inclusionTrial = Trial.inclusion([".com",".ar", ".uy"])
let trial = inclusionTrial.trial()

XCTAssertTrue(trial("http://www.nytimes.com"))
XCTAssertTrue(trial("http://www.lanacion.com.ar"))
XCTAssertFalse(trial("http://www.elpais.es"))

Email

This validates whether the evidence is an email or not.

let emailTrial = Trial.email
let trial = emailTrial.trial()
XCTAssertTrue(trial("[email protected]"))

Length(Length,Any)

This validates the length of given evidence:

let lengthTrial = Trial.Length(.Is, 10)
let trial = lengthTrial.trial()
XCTAssertTrue(trial("0123456789"))
let lengthTrial = Trial.Length(.Minimum, 10)
let trial = lengthTrial.trial()
XCTAssertTrue(trial("0123456789"))
let lengthTrial = Trial.Length(.Maximum, 10)
let trial = lengthTrial.trial()		
XCTAssertTrue(trial("0123456789"))
let interval = Trial.Length(.In, 2..<5 as HalfOpenInterval)
let trial = interval.trial()
XCTAssertTrue(trial("1234"))
let interval = Trial.Length(.In, 2...5 as ClosedInterval)
let trial = interval.trial()
XCTAssertFalse(trial("123456"))

Suspect

The Suspect is a Struct that is the glue between some other concepts always used while validating fields. It puts together a UITextField that is going to be the source of the evidence, a sentence that is going to be the text shown if the suspect is guilty (when the Trial returns false) and the Trial itself, that can be a custom made trial for the suspect or you can use one of the trials provided by the library:

Suspect(view: UITextField, sentence: String, trial: TrialProtocol)
Suspect(view: UITextField, sentence: String, trial: (String) -> Bool)

We can check if the Suspect is guilty or not with:

func isGuilty() -> Bool

This method is going to return true if the Trial returns false.

Also we can directly ask for the verdict on the Suspect, this is going to check if it's guilty or not and then return and empty string ("") or the sentence.

For example:

let suspect = Suspect(view: self.dummyTextField, sentence: "Invalid email", trial: .Email)		
let verdict = suspect.verdict() // this can be "" or "Invalid Email"

SwiftCop

Finally we have the guy that is going to enforce the validations! The cop is going to get all the suspects together and give us the tools to check who are the guilty suspects or if there is any guilty suspect at all.

As you can imagine this is going to add a suspect under the vigilance of a cop, we can add as many suspects as we want:

open func addSuspect(_ suspect: Suspect)

This will let us check if there is any guilty suspect between all the suspects under the surveillance of our cop:

public func anyGuilty() -> Bool

This will let us know all the guilty suspects our cop found:

public func allGuilties() -> Array<Suspect>

This will let us check if a UITextField that is suspect is guilty or not:

public func isGuilty(textField: UITextField) -> Suspect?

Example

The example is shipped in the repository:

class ViewController: UIViewController {
	@IBOutlet weak var validationLabel: UILabel!

	@IBOutlet weak var fullNameMessage: UILabel!
	@IBOutlet weak var emailMessage: UILabel!
	@IBOutlet weak var passwordMessage: UILabel!

	@IBOutlet weak var fullName: UITextField!
	@IBOutlet weak var emailTextField: UITextField!
	@IBOutlet weak var password: UITextField!

	let swiftCop = SwiftCop()

	override func viewDidLoad() {
		super.viewDidLoad()

		swiftCop.addSuspect(Suspect(view: self.fullName, sentence: "More Than Two Words Needed"){
			return $0.components(separatedBy: " ").filter{$0 != ""}.count >= 2
		})
		swiftCop.addSuspect(Suspect(view:self.emailTextField, sentence: "Invalid email", trial: Trial.email))
		swiftCop.addSuspect(Suspect(view:self.password, sentence: "Minimum 4 Characters", trial: Trial.length(.minimum, 4)))
	}

	@IBAction func validateFullName(_ sender: UITextField) {
		self.fullNameMessage.text = swiftCop.isGuilty(sender)?.verdict()
	}

	@IBAction func validateEmail(_ sender: UITextField) {
		self.emailMessage.text = swiftCop.isGuilty(sender)?.verdict()
	}

	@IBAction func validatePassword(_ sender: UITextField) {
		self.passwordMessage.text = swiftCop.isGuilty(sender)?.verdict()
	}

	@IBAction func allValid(_ sender: UITextField) {
		let nonGultiesMessage = "Everything fine!"
		let allGuiltiesMessage = swiftCop.allGuilties().map{ return $0.sentence}.joined(separator: "\n")

		self.validationLabel.text = allGuiltiesMessage.characters.count > 0 ? allGuiltiesMessage : nonGultiesMessage
	}

	@IBAction func hideKeyboard(_ sender: AnyObject) {
		self.view.endEditing(true)
	}
}

SwiftCopExampel

Installation

You can just clone the repo and copy the SwiftCop folder to your project or you can use one of the following options:

Setting up with CocoaPods

pod 'SwiftCop'

Then:

import SwiftCop

And you are all set!

Setting up with Carthage

  • TODO
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].