ValidateKit
ValidateKit是一个用于验证模型数据的框架。
特性
- 验证规则
- 自定义规则/抛出/值
- 范围 (n...m, n..., ...m)
- 计数 (n...m, n..., ...m, min, max, empty)
- 为空
- 字符集 (字母数字,数字,手机号码,自定义集合)
- 电子邮件
- 网址
- 支付卡
- 包含
- 规则操作
- 和
&&
- 或
||
- 非
!
- 和
- 开放协议式实现
- 全面的测试覆盖率
- 代码文档
要求
iOS 8.0+ / macOS 10.10+ / tvOS 9.0+ / watchOS 2.0+
Xcode 10.0+
Swift 4.2+
示例
示例应用程序是查看ValidateKit
功能的最优方式。只需打开ValidateKit.xcodeproj
并运行Example
方案即可。
安装
CocoaPods
ValidateKit 通过 CocoaPods 提供。要安装它,只需在您的 Podfile 中添加以下行
pod 'ValidateKit'
或者
pod 'ValidateKit', :git => 'https://github.com/redwerk/ValidateKit.git', :branch => 'master'
Carthage
Carthage 是一个去中心化的依赖项管理器,用于构建您的依赖项并提供二进制框架。
要使用 Carthage 在 Xcode 项目中集成 ValidateKit,在 Cartfile
中指定它
github 'redwerk/ValidateKit'
运行 carthage update
构建框架,并将构建好的 ValidateKit.framework
拖入您的 Xcode 项目中。
在您的应用程序目标的“构建阶段”设置标签页中,点击“+”图标,选择“New Run Script Phase”并将框架路径添加如Carthage 入门步骤 4、5 和 6中所述
Swift 包管理器
要使用 Apple 的 Swift 包管理器 进行集成,将以下内容作为依赖项添加到您的 Package.swift
dependencies: [
.package(url: "https://github.com/redwerk/ValidateKit.git", from: "1.0.0")
]
手动集成
如果您不希望使用上述任何依赖项管理器,您可以手动将 ValidateKit 集成到项目中。只需将 Sources
文件夹拖入您的 Xcode 项目中即可。
使用
import Foundation
import ValidateKit
struct User {
var id: Int
var name: String
var email: String?
var age: Int
var about: String
var hobbies: [String] = ["a", "b", "c"]
var phone: String
}
extension User: Validatable {
private static var nameCharacterSet: CharacterSet {
var characterSet = CharacterSet()
characterSet.formUnion(.lowercaseLetters)
characterSet.formUnion(.uppercaseLetters)
return characterSet
}
static func conditions() throws -> Conditions<User> {
var conditions = Conditions(User.self)
conditions.add(\.id, "id", .range(0...100) || .range(10000...))
conditions.add(\.email, "email", .email && !.nil)
conditions.add(\.age, "age", .range(1...))
conditions.add(\.name, "name", .count(3...20) && .characters(nameCharacterSet))
conditions.add(\.about, "description length", .count(30...))
conditions.add(\.hobbies, "hobbies", !.empty)
conditions.add(\.phone, "phone", .phoneCharacters)
return conditions
}
}
// ...
let user = User(...)
do {
try user.validate()
} catch let error {
print(error.localizedDescription)
}
贡献
欢迎贡献
许可
ValidateKit
Copyright (c) 2019 Redwerk [email protected]
Permission is hereby granted, free of charge, to any person obtaining a copy
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.