Skip to content

Commit

Permalink
docs: Add a note about runtime validations on README
Browse files Browse the repository at this point in the history
  • Loading branch information
gustavoguichard committed Jun 12, 2024
1 parent d98c52f commit d5f92c0
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ A set of types and functions to make compositions easy and safe.
## Table of contents
- [Quickstart](#quickstart)
- [Composing type-safe functions](#composing-type-safe-functions)
- [Adding runtime validation to the Composable](#adding-runtime-validation-to-the-composable)
- [Creating primitive composables](#creating-primitive-composables)
- [Sequential composition](#sequential-composition)
- [Using non-composables (mapping)](#using-non-composables-mapping)
Expand Down Expand Up @@ -91,6 +92,26 @@ We can also extend the same reasoning to functions that return promises in a tra

This library also defines several operations besides the `pipe` to compose functions in arbitrary ways, giving a powerful tool for the developer to reason about the data flow **without worrying about mistakenly connecting the wrong parameters** or **forgetting to unwrap some promise** or **handle some error** along the way.

### Adding runtime validation to the Composable
To ensure type safety at runtime, use the `applySchema` or `withSchema` functions to validate external inputs against defined schemas. These schemas can be specified with libraries such as [Zod](https://github.com/colinhacks/zod/) or [ArkType](https://github.com/arktypeio/arktype).

Note that the resulting `Composable` will have unknown types for the parameters now that we rely on runtime validation.

```ts
import { applySchema } from 'composable-functions'
import { z } from 'zod'

const addAndReturnWithRuntimeValidation = applySchema(
z.number(),
z.number(),
)(addAndReturnString)

// Or you could have defined schemas and implementation in one shot:
const add = withSchema(z.number(), z.number())((a, b) => a + b)
```

For more information and examples, check the [Handling external input](./with-schema.md) guide.

## Creating primitive composables

A `Composable` is a function that returns a `Promise<Result<T>>` where `T` is any type you want to return. Values of the type `Result` will represent either a failure (which carries a list of errors) or a success, where the computation has returned a value within the type `T`.
Expand Down

0 comments on commit d5f92c0

Please sign in to comment.