/
Count.swift
36 lines (31 loc) 路 1.5 KB
/
Count.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
extension Validator where T: Collection {
/// Validates that the data's count is within the supplied `ClosedRange`.
public static func count(_ range: ClosedRange<Int>) -> Validator<T> {
.count(min: range.lowerBound, max: range.upperBound)
}
/// Validates that the data's count is less than or equal the supplied upper bound using `PartialRangeThrough`.
public static func count(_ range: PartialRangeThrough<Int>) -> Validator<T> {
.count(min: nil, max: range.upperBound)
}
/// Validates that the data's count is less than the supplied upper bound using `PartialRangeUpTo`.
public static func count(_ range: PartialRangeUpTo<Int>) -> Validator {
.count(min: nil, max: range.upperBound.advanced(by: -1))
}
/// Validates that the data's count is greater than or equal to the supplied lower bound using `PartialRangeFrom`.
public static func count(_ range: PartialRangeFrom<Int>) -> Validator<T> {
.count(min: range.lowerBound, max: nil)
}
/// Validates that the data's count is within the supplied `Range`.
public static func count(_ range: Swift.Range<Int>) -> Validator<T> {
.count(min: range.lowerBound, max: range.upperBound.advanced(by: -1))
}
static func count(min: Int?, max: Int?) -> Validator<T> {
let suffix: String
if T.self is String.Type {
suffix = "character"
} else {
suffix = "item"
}
return .range(min: min, max: max, \.count, suffix)
}
}