Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for PartialRangeUpTo in validators #2369

Merged
merged 4 commits into from Jun 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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