Last active
March 28, 2020 13:10
-
-
Save patroza/16d8e9eaafe209975c0dbe9074ed4f9b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Input = { | |
trainTripId: string | |
pax?: Pax | |
startDate?: Date | |
travelClass?: string | |
} | |
const validateStateProposition = ({ | |
pax, | |
startDate, | |
trainTripId, | |
travelClass, | |
}: Input) => | |
pipe( | |
sequenceT(E.getValidation(NA.getSemigroup<FieldValidationError>()))( | |
pipe( | |
validateId(trainTripId), | |
E.mapLeft(toFieldError("trainTripId")), | |
E.mapLeft(NA.of), | |
), | |
pipe( | |
E.valueOrUndefined(travelClass, TravelClassDefinition.create), | |
E.mapLeft(toFieldError("travelClass")), | |
E.mapLeft(NA.of), | |
), | |
pipe( | |
E.valueOrUndefined(startDate, FutureDate.create), | |
E.mapLeft(toFieldError("startDate")), | |
E.mapLeft(NA.of), | |
), | |
pipe( | |
E.valueOrUndefined(pax, PaxDefinition.create), | |
E.mapLeft(toFieldError("pax")), | |
E.mapLeft(NA.of), | |
), | |
), | |
E.mapLeft(combineValidationErrors), | |
E.map(([trainTripId, travelClass, startDate, pax]) => ({ | |
trainTripId, | |
pax, | |
startDate, | |
travelClass, | |
})), | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Input = { | |
trainTripId: string | |
pax?: Pax | |
startDate?: Date | |
travelClass?: string | |
} | |
const validateStateProposition = ({ | |
pax, | |
startDate, | |
trainTripId, | |
travelClass, | |
}: Input) => | |
pipe( | |
Do(E.getValidation(getMonoid<FieldValidationError>())) | |
.sequenceS({ | |
trainTripId: pipe( | |
validateId(trainTripId), | |
E.mapLeft(toFieldError("trainTripId")), | |
E.mapLeft(NA.of), | |
), | |
travelClass: pipe( | |
E.valueOrUndefined(travelClass, TravelClassDefinition.create), | |
E.mapLeft(toFieldError("travelClass")), | |
E.mapLeft(NA.of), | |
), | |
startDate: pipe( | |
E.valueOrUndefined(startDate, FutureDate.create), | |
E.mapLeft(toFieldError("startDate")), | |
E.mapLeft(NA.of), | |
), | |
pax: pipe( | |
E.valueOrUndefined(pax, PaxDefinition.create), | |
E.mapLeft(toFieldError("pax")), | |
E.mapLeft(NA.of), | |
), | |
}) | |
.done(), | |
E.mapLeft(combineValidationErrors), | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I would say that sample 2, Do notation seems more verbose up front as we build the initial object and require return,
however it is a bit easier to reason about, instead of having to deal with tuples like in the first sample.
In the first sample we also still have to convert the tuple back to an Object.