0.1.9 • Published 3 years ago

@morphism/runtime v0.1.9

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

@morphism/runtime

A 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.

Examples

If we have an endpoint that accepts a payload of the following shape:

{
  username: string;
  hoursWorked: string;
  isOvertime?: string;
  date: number
}

and, after parsing/validation, needs to be transformed to:

{
  username: string;
  hoursWorked: number;
  isOvertime: boolean;
  date: Date
}

where

  • username 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 when it isn't provided
  • date is a UNIX epoch representing a Date
  • hoursWorked is just some number

Then we could use @morphism/runtime to describe the above logic like so:

import { Runtime } from "@morphism/runtime";

type Request = Runtime.ToType<typeof runtime>;
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:

pipe(
  input,
  Request.runtime().decode,
  Either.fold(
    (errors: Runtime.Errors) => ...,
    (request: Request) => ...
  )
)
0.1.8

3 years ago

0.1.9

3 years ago

0.1.8-alpha.7

4 years ago

0.1.8-alpha.4

4 years ago

0.1.8-alpha.5

4 years ago

0.1.8-alpha.2

4 years ago

0.1.8-alpha.3

4 years ago

0.1.8-alpha.1

4 years ago

0.1.7

4 years ago

0.1.7-alpha.1

4 years ago

0.1.6

4 years ago

0.1.6-alpha.1

4 years ago

0.1.6-alpha.0

4 years ago

0.1.6-alpha.5

4 years ago

0.1.6-alpha.4

4 years ago

0.1.6-alpha.3

4 years ago

0.1.6-alpha.2

4 years ago

0.1.6-alpha.6

4 years ago

0.1.2

4 years ago

0.1.1

4 years ago

0.1.4

4 years ago

0.1.3

4 years ago

0.1.5

4 years ago

0.1.0

4 years ago

0.0.2-alpha.58

4 years ago

0.0.2-alpha.55

4 years ago

0.0.2-alpha.53

4 years ago