@morphism/runtime
npm install @morphism/runtimeA combination of io-ts and io-ts-types, exported under
a namespace called Runtime, that acts as an all-in-one input/output
validation library.
If we have an endpoint that accepts a payload of the following shape:
``typescript`
{
username: string;
hoursWorked: string;
isOvertime?: string;
date: number
}`
and, after parsing/validation, needs to be transformed to:typescript`
{
username: string;
hoursWorked: number;
isOvertime: boolean;
date: Date
}username
where
- is a required parameter that we want a special error message for isOvertime
- is an optional argument we want to fill with a default value whendate
it isn't provided
- is a UNIX epoch representing a DatehoursWorked
- is just some number
Then we could use @morphism/runtime to describe the above logic like so:`typescript
import { Runtime } from "@morphism/runtime";
type Request = Runtime.ToType
namespace Request {
export const runtime = () =>
Runtime.strict({
username: Runtime.withMessage(
Runtime.string(),
() => 'Some special error message for just username'
),
hoursWorked: Runtime.numberFromString(),
isOvertime: Runtime.withFallback(Runtime.booleanFromString(), false),
dateRange: Runtime.dateFromUnixTime(),
});
}
`Request now represents the shape our Request.runtime function validates,`
and can be used like so:typescript``
pipe(
input,
Request.runtime().decode,
Either.fold(
(errors: Runtime.Errors) => ...,
(request: Request) => ...
)
)