Chain Validation Steps, Keeping Every Error Type
Write the code, see it run, pass the checks.
The problem
- hard
- Code
- Pro
Export a generic Either<E, A>: { ok: true, value } or { ok: false, error }, with ok(value) and err(error) to make them, and andThen(result, fn), which passes a successful value to the next step fn and returns an error unchanged without calling fn. Then write validateQuantity(text) from three steps: not-a-number unless the trimmed text is a number, not-whole unless it's a whole number, out-of-range unless it's from 1 to 99. Type andThen so the error type grows with each step (the first step's errors or the next one's), so validateQuantity's error is exactly the union of the three codes.
Your code must pass
- Accepts a valid quantity
- Reports the first failing step's error
- Skips later steps after an error
- Has no type errors
- andThen joins the error types
- fn gets the previous step's value
- validateQuantity's error is the union of every step's