From bfc2c63afc0b4abd820c2f80c8338ff344da75c8 Mon Sep 17 00:00:00 2001 From: Micha Mazaheri Date: Thu, 2 Mar 2017 15:09:51 +0100 Subject: [PATCH] Fix multipleOf constraint for integers --- Sources/Validators.swift | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/Sources/Validators.swift b/Sources/Validators.swift index 6b7d55f..e2b0a8b 100644 --- a/Sources/Validators.swift +++ b/Sources/Validators.swift @@ -222,14 +222,31 @@ func validatePattern(_ pattern: String) -> (_ value: Any) -> ValidationResult { // MARK: Numerical +func validate(_ value: Double, multipleOf number: Double) -> ValidationResult { + if value.truncatingRemainder(dividingBy: number) != 0 { + return .invalid(["\(value) is not a multiple of \(number)"]) + } + return .Valid +} + +func validate(_ value: Int, multipleOf number: Int) -> ValidationResult { + if value % number != 0 { + return .invalid(["\(value) is not a multiple of \(number)"]) + } + return .Valid +} + func validateMultipleOf(_ number: Double) -> (_ value: Any) -> ValidationResult { return { value in if number > 0.0 { if let value = value as? Double { - let result = value / number - if result != floor(result) { - return .invalid(["\(value) is not a multiple of \(number)"]) + return validate(value, multipleOf: number) + } + if let value = value as? Int { + if number.truncatingRemainder(dividingBy: 1) == 0 { + return validate(value, multipleOf: Int(number)) } + return validate(Double(value), multipleOf: number) } }