Skip to content

Commit

Permalink
Add support for PartialRangeUpTo in validators (#2369)
Browse files Browse the repository at this point in the history
* This also improves and fixes documentation
* Add tests
* Add additional tests for PartialRangeFrom and PartialRangeThrough
  • Loading branch information
Trevör committed Jun 1, 2020
1 parent f32b911 commit 4fa7ed4
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
9 changes: 7 additions & 2 deletions Sources/Vapor/Validation/Validators/Count.swift
Expand Up @@ -4,12 +4,17 @@ extension Validator where T: Collection {
.count(min: range.lowerBound, max: range.upperBound)
}

/// Validates that the data's count is less than the supplied upper bound using `PartialRangeThrough`.
/// 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 less than the supplied lower bound using `PartialRangeFrom`.
/// 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)
}
Expand Down
11 changes: 9 additions & 2 deletions Sources/Vapor/Validation/Validators/Range.swift
Expand Up @@ -11,12 +11,12 @@ extension Validator where T: Comparable {
.range(min: range.lowerBound, max: range.upperBound)
}

/// Validates that the data is less than the supplied upper bound using `PartialRangeThrough`.
/// Validates that the data is less than or equal to the supplied upper bound using `PartialRangeThrough`.
public static func range(_ range: PartialRangeThrough<T>) -> Validator<T> {
.range(min: nil, max: range.upperBound)
}

/// Validates that the data is less than the supplied lower bound using `PartialRangeFrom`.
/// Validates that the data is greater than or equal the supplied lower bound using `PartialRangeFrom`.
public static func range(_ range: PartialRangeFrom<T>) -> Validator<T> {
.range(min: range.lowerBound, max: nil)
}
Expand All @@ -26,6 +26,13 @@ extension Validator where T: Comparable {
}
}

extension Validator where T: Comparable & SignedInteger {
/// Validates that the data is less than the supplied upper bound using `PartialRangeUpTo`
public static func range(_ range: PartialRangeUpTo<T>) -> Validator<T> {
.range(min: nil, max: range.upperBound.advanced(by: -1))
}
}

extension Validator {
static func range<U>(
min: U?, max: U?, _ keyPath: KeyPath<T, U>,
Expand Down
7 changes: 7 additions & 0 deletions Tests/VaporTests/ValidationTests.swift
Expand Up @@ -166,12 +166,17 @@ class ValidationTests: XCTestCase {

func testRange() {
assert(4, passes: .range(-5...5))
assert(4, passes: .range(..<5))
assert(5, fails: .range(..<5), "is greater than maximum of 4")
assert(5, passes: .range(...10))
assert(11, fails: .range(...10), "is greater than maximum of 10")
assert(4, fails: !.range(-5...5), "is between -5 and 5")
assert(5, passes: .range(-5...5))
assert(-5, passes: .range(-5...5))
assert(6, fails: .range(-5...5), "is greater than maximum of 5")
assert(-6, fails: .range(-5...5), "is less than minimum of -5")
assert(.max, passes: .range(5...))
assert(4, fails: .range(5...), "is less than minimum of 5")
assert(-5, passes: .range(-5..<6))
assert(-4, passes: .range(-5..<6))
assert(5, passes: .range(-5..<6))
Expand All @@ -192,6 +197,8 @@ class ValidationTests: XCTestCase {
func testCountItems() {
assert([1], passes: .count(1...6))
assert([1], fails: !.count(1...6), "is between 1 and 6 item(s)")
assert([1], passes: .count(...1))
assert([1], fails: .count(..<1), "is greater than maximum of 0 item(s)")
assert([1, 2, 3], passes: .count(1...6))
assert([1, 2, 3, 4, 5, 6], passes: .count(1...6))
assert([Int](), fails: .count(1...6), "is less than minimum of 1 item(s)")
Expand Down

0 comments on commit 4fa7ed4

Please sign in to comment.