歡迎加入QQ討論群258996829

Swift 驗證庫 SwiftCop

發(fā)布時間:2016-09-08 16:16  回復:0  查看:4560  感興趣:34  贊:0   最后回復:2016-09-08 16:16  

SwiftCop 是一個用 Swift 寫的驗證庫,它受啟發(fā)于 Ruby On Rails Active Record validations.

SwiftCop 可以驗證 Email、字符是否包含指定內(nèi)容、長度驗證等等。

例子效果:

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"))

相關(guān)開源代碼

您還未登錄,請先登錄

熱門帖子

最新帖子

?