简体   繁体   English

蒸汽快速定制验证器

[英]vapor swift custom validator

I'm trying to create a custom password validator in Vapor. 我正在尝试在Vapor中创建自定义密码验证器。 I can't seem to figure out how to make the Matches work. 我似乎无法弄清楚如何使匹配工作。

Orignally posted as answer by the question author within the question itself. 在问题本身中由问题作者正式发布作为答案。

The documentation can be found here https://vapor.github.io/documentation/guide/validation.html 文档可以在这里找到https://vapor.github.io/documentation/guide/validation.html

import Vapor

class PasswordValidation: ValidationSuite {

    static func validate(input value: String) throws {
        // 1 upper 1 lower 1 special 1 number at least 8 long
        let regex =  Matches("^(?=.*[A-Z])(?=.*[a-z])(?=.*[!@#$&*])(?=.*[0-9]).{8}$")

        /*
        let evaluation = Matches.validate(regex)
        try evaluation.validate(input: value)
        */

        let evaluation = OnlyAlphanumeric.self
                         && Count.min(8)
                         && Matches.validate(Matches<regex & value>)

        try evaluation.validate(input: value)
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM