From 4fa7ed40ccb1dc4e1f8739f31e1d3df15681bbdc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Trev=C3=B6r?= Date: Mon, 1 Jun 2020 23:19:19 +0200 Subject: [PATCH] Add support for PartialRangeUpTo in validators (#2369) * This also improves and fixes documentation * Add tests * Add additional tests for PartialRangeFrom and PartialRangeThrough --- Sources/Vapor/Validation/Validators/Count.swift | 9 +++++++-- Sources/Vapor/Validation/Validators/Range.swift | 11 +++++++++-- Tests/VaporTests/ValidationTests.swift | 7 +++++++ 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/Sources/Vapor/Validation/Validators/Count.swift b/Sources/Vapor/Validation/Validators/Count.swift index 9b05ed97e9..b7ca081396 100644 --- a/Sources/Vapor/Validation/Validators/Count.swift +++ b/Sources/Vapor/Validation/Validators/Count.swift @@ -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) -> Validator { .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) -> 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) -> Validator { .count(min: range.lowerBound, max: nil) } diff --git a/Sources/Vapor/Validation/Validators/Range.swift b/Sources/Vapor/Validation/Validators/Range.swift index 0c623aa34e..489f22a955 100644 --- a/Sources/Vapor/Validation/Validators/Range.swift +++ b/Sources/Vapor/Validation/Validators/Range.swift @@ -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) -> Validator { .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) -> Validator { .range(min: range.lowerBound, max: nil) } @@ -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) -> Validator { + .range(min: nil, max: range.upperBound.advanced(by: -1)) + } +} + extension Validator { static func range( min: U?, max: U?, _ keyPath: KeyPath, diff --git a/Tests/VaporTests/ValidationTests.swift b/Tests/VaporTests/ValidationTests.swift index 9e01ed4fd7..03838965fa 100644 --- a/Tests/VaporTests/ValidationTests.swift +++ b/Tests/VaporTests/ValidationTests.swift @@ -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)) @@ -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)")