🔶
Swift ValidatorsiOS 上的字符串验证。
内容
ReactiveSwift + SwiftValidators
想要在 ReactiveSwift
中使用 SwiftValidators
? SwiftValidatorsReactiveExtensions
提供了一组扩展,与 ValidatingProperty
稳定配合使用。
安装
SwiftValidators 支持 iOS 9.0+、Xcode 8 和 Swift 3.0
use_frameworks!
target 'MyProject' do
...
pod 'SwiftValidators'
...
end
它也可以通过 SPM 使用
import PackageDescription
let package = Package(
name: "MyProject",
targets: [],
dependencies: [
.Package(url: "https://github.com/gkaimakas/SwiftValidators.git",
majorVersion: 6)
]
)
操作流程
用法
验证通过使用Validator
的apply
函数完成。您可以手动创建Validator
,或者使用Validator类中的静态函数中的已经存在的之一。
Validator
的apply函数接收一个符合StringConvertible
协议的可空值作为输入。默认情况下,String
、NSString
、Int
、Float
、Double
和Bool
都符合StringConvertible
。
如果您使用的是静态Validator函数并且想指定当输入为nil时Validator
的行为,请将nilResponse
参数设置为true或false。默认情况下,nilResponse设置为false。
Validator.exactLength(3).apply("abc") //returns true
Validator.exactLength(3).apply(true) //returns false (the string representation of true is 'true')
Validator.exactLength(3).apply(nil) //returns false since `nilResponse` is set to false by default
Valuidator.exactLength(3, nilResponse: true).apply(nil) //returns true since we set nilResponse to true
有关如何调用每个验证器的更多示例,请参阅单元测试。
逻辑运算符
您可以使用逻辑运算符AND
、OR
和NOT
(分别表示为 &&、|| 和 !)组合运算符。
let combinedANDValidator = Validator.required() && Validator.isTrue()
当值不为空且为“true”时,combinedANDValidator
将返回true
。
let combinedORValidator = Validator.isTrue() || Validators.isFalse()
当值为“true”或“false”时,combinedORValidator
将返回true
,否则返回false。
let reversedValidator = !Validator.isTrue()
当值等于“true”时,reversedValidator
将返回false
,对所有其他值返回true。
可用验证器
名称 | 描述 | 类型 | 参数 | 示例 |
---|---|---|---|---|
contains | 检查是否包含种子 | func | String, Bool(nilReponse=false) | Validator.contains("some seed").apply("ee") |
equals | 检查是否等于另一个 | func | String, Bool(nilReponse=false) | Validator.equals("aa").apply("aa") |
exactLength | 检查是否具有确切长度 | func | Int, Bool(nilReponse=false) | Validator.exactLength(2).apply("aa") |
isASCII | 检查是否是有效的ASCII字符串 | func | Bool(nilReponse=false) | Validator.isASCII().apply("SDGSFG") |
isAfter | 检查是否在日期之后 | func | String, String, Bool(nilReponse=false) | Validator.isAfter("23/07/2015", format: "dd/MM/yyyy").apply("24/07/2015") |
isAlpha | 检查是否只包含字母 | func | Bool(nilReponse=false) | Validator.isAlpha().apply("abc") |
isAlphanumeric | 检查是否只包含字母和数字 | func | Bool(nilReponse=false) | Validator.isAlphanumeric().apply("abc123") |
isBase64 | 检查是否是有效的base64字符串 | func | Bool(nilReponse=false) | Validator.isBase64().apply("some string") |
isBefore | 检查它是否在日期之前 | func | String, String, Bool(nilReponse=false) | Validator.isBefore("25/09/1987", format: "dd/MM/yyyy").apply("29/03/1994") |
isBool | 检查它是否是布尔值 | func | Bool(nilReponse=false) | Validator.isBool().apply("true") |
isCreditCard | 检查它是否是信用卡号码 | func | Bool(nilReponse=false) | Validator.isCreditCard().apply("123") |
isDate | 检查它是否是一个有效的日期 | func | String, Bool(nilReponse=false) | Validator.isDate("dd/MM/yyyy").apply("25/09/1987") |
isEmail | 检查它是否是电子邮件 | func | Bool(nilReponse=false) | Validator.isEmail().apply("[email protected]") |
isEmpty | 检查它是否是一个空字符串 | func | Bool(nilReponse=false) | Validator.isEmpty().apply("") |
isFQDN | 检查它是否是完全限定的域名 | func | FQDNOptions或空,Bool(nilReponse=false) | Validator.isFQDN().apply("ABC") |
isFalse | 检查它是否是false | func | Bool(nilReponse=false) | Validator.isFalse().apply("false") |
isFloat | 检查它是否是浮点数 | func | Bool(nilReponse=false) | Validator.isFloat().apply("2.3e24") |
isHexColor | 检查它是否是有效的十六进制颜色 | func | Bool(nilReponse=false) | Validator.isHexColor().apply("#fafafa") |
isHexadecimal | 检查它是否是十六进制值 | func | Bool(nilReponse=false) | Validator.isHexadecimal().apply("abcdef") |
isIP | 检查它是否是有效的IP(4 |6) | func | Bool(nilReponse=false) | Validator.isIP().apply("0.0.0.0") |
isIPv4 | 检查它是否是有效的IPv4 | func | Bool(nilReponse=false) | Validator.isIPv4().apply("0.0.0.0") |
isIPv6 | 检查它是否是有效的IPv6 | func | Bool(nilReponse=false) | Validator.isIPv6().apply("::") |
isISBN | 检查它是否是有效的ISBN | func | ISBN, Bool(nilReponse=false) | Validator.isISBN(.v13).apply("asdf") |
isIn | 检查该值是否存在于提供的数组中 | func | Array, Bool(nilReponse=false) | Validator.isIn(["a","b","c"]).apply("a") |
isInt | 检查它是否是有效的整数 | func | Bool(nilReponse=false) | Validator.isInt().apply("123") |
isLowercase | 检查它是否只包含小写字母 | func | Bool(nilReponse=false) | Validator.isLowercase().apply("asdf") |
isMongoId | 检查它是否是十六进制mongo ID | func | Bool(nilReponse=false) | Validator.isMongoId()("adfsdffsg") |
isNumeric | 检查它是否是数值型 | func | Bool(nilReponse=false) | Validator.isNumeric().apply("+123") |
isPhone | 检查它是否是有效的电话号码 | func | 电话号码,Bool nilReponse=false) | Validator.isPhone(.el_GR).apply("6944848966") |
isPostalCode | 检查它是否是有效的邮政编码 | func | 邮政编码,Bool nilResponse=false) | Validator.isPostalCode(.GR).apply("30 006") |
isTrue | 检查它是否是true | func | Bool(nilReponse=false) | Validator.isTrue().apply("true") |
isURL | 检查它是否是有效的URL | func | Bool(nilReponse=false) | Validator.isURL().apply("http://www.google.com") |
isUUID | 检查它是否是有效的UUID | func | Bool(nilReponse=false) | Validator.isUUID().apply("243-124245-2235-123") |
isUppercase | 检查它是否只包含大写字母 | func | Bool(nilReponse=false) | Validator.isUppercase().apply("ABC") |
maxLength | 检查长度是否不超过最大长度 | func | Int, Bool(nilReponse=false) | Validator.maxLength(2).apply("ab") |
minLength | 检查长度是否不小于 | func | Int, Bool(nilReponse=false) | Validator.minLength(1).apply("213") |
required | 检查它是否不是空字符串 | func | Bool(nilReponse=false) | Validator.required().apply("") |
regex | 检查值是否与正则表达式完全匹配 | func | String, Bool(nilReponse=false) | Validator.regex(pattern).apply("abcd") |
*FQDNOptions是一个用于isFQDN配置用途的类。可以通过这种方式实例化
FQDNOptions(requireTLD: Bool, allowUnderscores: Bool, allowTrailingDot: Bool)
许可证MIT
Copyright (c) George Kaimakas [email protected]
Permission is hereby granted, free of charge, to any person obtaining
acopy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.