Skip to content

Fix multipleOf constraint for integers #19

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions Sources/Validators.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down