diff --git a/Sources/Unbox.swift b/Sources/Unbox.swift index 3a5fb11..076a4b1 100644 --- a/Sources/Unbox.swift +++ b/Sources/Unbox.swift @@ -487,8 +487,13 @@ public class Unboxer { } let transformedStringArray = UnboxValueResolver<[String]>(self).resolveOptionalValueForKey(key, isKeyPath: isKeyPath, transform: { stringArray -> [T]? in - return stringArray.flatMap { - return T.transformUnboxedString($0) + do { + return try stringArray.flatMap { + guard let transformedUnboxedString = T.transformUnboxedString($0) else { throw UnboxValueError.InvalidValue($0, key) } + return transformedUnboxedString + } + } catch { + return nil } }) diff --git a/Tests/UnboxTests.swift b/Tests/UnboxTests.swift index fd8ad00..3801b97 100644 --- a/Tests/UnboxTests.swift +++ b/Tests/UnboxTests.swift @@ -1358,13 +1358,13 @@ class UnboxTests: XCTestCase { } } - func testUnboxingArrayOfStringTransformedToInt() { + func testUnboxingArrayOfStringsTransformedToInt() { let dictionary: UnboxableDictionary = ["intArray": ["123", "456", "789"]] struct ModelA: Unboxable { let intArray: [Int] init(unboxer: Unboxer) { - intArray = unboxer.unbox("intArray") + self.intArray = unboxer.unbox("intArray") } } @@ -1374,11 +1374,28 @@ class UnboxTests: XCTestCase { XCTAssertEqual(modelA.intArray[1], 456) XCTAssertEqual(modelA.intArray[2], 789) } catch { - print(error) XCTFail() } } + func testUnboxingArrayOfBadStringsTransformedToInt() { + let dictionary: UnboxableDictionary = ["intArray": ["123", "abc", "789"]] + + struct ModelA: Unboxable { + let intArray: [Int] + init(unboxer: Unboxer) { + self.intArray = unboxer.unbox("intArray") + } + } + + do { + try Unbox(dictionary) as ModelA + XCTFail() + } catch { + // Test Passed + } + } + } private func UnboxTestDictionaryWithAllRequiredKeysWithValidValues(nested: Bool) -> UnboxableDictionary {