Swift51.com
Swift 头像
Swift  2016-09-08 16:16

Swift 验证库 SwiftCop

回复:0  查看:4599  感兴趣:34  赞:0  

SwiftCop 是一个用 Swift 写的验证库,它受启发于 Ruby On Rails Active Record validations.

SwiftCop 可以验证 Email、字符是否包含指定内容、长度验证等等。

例子效果:

Swift 验证库 SwiftCop

上图例子代码:

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's create a cop!
    let swiftCop = SwiftCop()

    override func viewDidLoad() {
        super.viewDidLoad()

        //Let's add all the suspects
        swiftCop.addSuspect(Suspect(view: self.fullName, sentence: "More Than Two Words Needed"){
            return $0.componentsSeparatedByString(" ").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}.joinWithSeparator("\n")

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

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


示例代码:

//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(evidence: "http://www.nytimes.com"))

//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(evidence: "#57b5b5"))

//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(evidence: "http://www.nytimes.com"))

//Email
//This validates whether the evidence is an email or not.
let emailTrial = Trial.Email
let trial = emailTrial.trial()
XCTAssertTrue(trial(evidence: "test@test.com"))

//Length(Length,Any)
//This validates the length of given evidence:
let lengthTrial = Trial.Length(.Is, 10)
let trial = lengthTrial.trial()
XCTAssertTrue(trial(evidence: "0123456789"))

let lengthTrial = Trial.Length(.Minimum, 10)
let trial = lengthTrial.trial()
XCTAssertTrue(trial(evidence: "0123456789"))

let lengthTrial = Trial.Length(.Maximum, 10)
let trial = lengthTrial.trial()     
XCTAssertTrue(trial(evidence: "0123456789"))

let interval = Trial.Length(.In, 2..<5 as HalfOpenInterval)
let trial = interval.trial()
XCTAssertTrue(trial(evidence: "1234"))

let interval = Trial.Length(.In, 2...5 as ClosedInterval)
let trial = interval.trial()
XCTAssertFalse(trial(evidence: "123456"))

相关开源代码