Skip to content

Commit

Permalink
Support optionals (if without else)
Browse files Browse the repository at this point in the history
  • Loading branch information
eunikolsky committed Jun 19, 2021
1 parent cc775cb commit a0dff92
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
4 changes: 4 additions & 0 deletions LightweightValidation/ConjunctionBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ public struct ConjunctionBuilder {
result && validation
}
}

public static func buildOptional <E> (_ validation: V<(), E>?) -> V<(), E> {
validation ?? .empty
}
}

public extension V {
Expand Down
26 changes: 26 additions & 0 deletions LightweightValidationTests/ConjunctionBuilderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,31 @@ class ConjunctionBuilderTests: XCTestCase {
}
XCTAssertEqual(sut.error, ["always fails"])
}

func testDisabledConditionalStepShouldBeIgnored() {
let forceFail = false

let sut = Response.SimpleValidationResult.all {
true <?> StringError("never happens")

if forceFail {
false <?> StringError("always fails")
}
}
XCTAssertTrue(sut.isValue)
}

func testEnabledConditionalStepShouldNotBeIgnored() {
let forceFail = true

let sut = Response.SimpleValidationResult.all {
true <?> StringError("never happens")

if forceFail {
false <?> StringError("always fails")
}
}
XCTAssertEqual(sut.error, ["always fails"])
}
}
#endif

0 comments on commit a0dff92

Please sign in to comment.