Skip to content

Instantly share code, notes, and snippets.

@patroza
Last active March 28, 2020 13:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save patroza/16d8e9eaafe209975c0dbe9074ed4f9b to your computer and use it in GitHub Desktop.
Save patroza/16d8e9eaafe209975c0dbe9074ed4f9b to your computer and use it in GitHub Desktop.
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,
})),
)
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),
)
@patroza
Copy link
Author

patroza commented Mar 28, 2020

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment