JSONSchema.swift icon indicating copy to clipboard operation
JSONSchema.swift copied to clipboard

Fix multipleOf constraint for integers

Open mittsh opened this issue 8 years ago • 2 comments

Proposal to fix the multipleOf constraint for integers. In fact, it wasn't working so far for the type integer. If the value passed was a Swift Int, we got value as? Double being evaluated to nil.

I made some changes, to try to coherce both number and value into an Int to use the standard modulo operator. And fallback on floating point comparison.

Also, I've replaced the previous flooring method by truncatingRemainder(dividingBy:) for floating point division.

Let me know what you think @kylef I'm happy to make any changes if coding style or implementation isn't matching the current project standards. Thanks in advance!

mittsh avatar Mar 02 '17 14:03 mittsh

Looks like these changes are breaking one of the tests (https://github.com/json-schema-org/JSON-Schema-Test-Suite/blob/8f867168d17497a775141704960906db83634a04/tests/draft4/multipleOf.json#L48-L52)

JSONSchemaTests.JSONSchemaCases
  testEverything, XCTAssertEqual failed: ("false") is not equal to ("true") - Failed validation: ["0.0075 is not a multiple of 0.0001"]
  /Users/travis/build/kylef/JSONSchema.swift/Tests/JSONSchemaCases.swift:113

      case .invalid(let errors):
        XCTAssertEqual(result.valid, test.value, "Failed validation: \(errors)")
      }

Could you take a look into this please?

kylef avatar Mar 06 '17 12:03 kylef

Absolutely, yes. I'll have a look. Thank you! 😄

mittsh avatar Mar 06 '17 15:03 mittsh