@railway-ts/pipelines 0.1.11 → 0.1.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/README.md +113 -100
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -2,9 +2,39 @@
2
2
 
3
3
  [![npm version](https://img.shields.io/npm/v/@railway-ts/pipelines.svg)](https://www.npmjs.com/package/@railway-ts/pipelines) [![Build Status](https://github.com/sakobu/railway-ts-pipelines/workflows/CI/badge.svg)](https://github.com/sakobu/railway-ts-pipelines/actions) [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) [![TypeScript](https://img.shields.io/badge/TypeScript-5.0+-blue)](https://www.typescriptlang.org/) [![Coverage](https://img.shields.io/codecov/c/github/sakobu/railway-ts-pipelines)](https://codecov.io/gh/sakobu/railway-ts-pipelines)
4
4
 
5
- Railway-oriented programming for TypeScript. Result and Option types that don't suck.
5
+ Railway-oriented programming for TypeScript. Typed `Result<T, E>` pipelines with first-class validation and optional `Option<T>` support — small, modular, and fully tree-shakable.
6
6
 
7
- Small, focused API surface. Errors propagate automatically, you handle them once at the end.
7
+ ## Why?
8
+
9
+ Most TypeScript projects end up combining:
10
+
11
+ - A validation library (Zod, Valibot, Yup)
12
+ - A Result library (neverthrow, fp-ts, custom)
13
+ - Manual async error wiring
14
+
15
+ This library unifies those around a single `Result<T, E>` model so validation, async operations, and business logic compose without adapters.
16
+
17
+ No runtime. No global context. No required framework. Import only what you need.
18
+
19
+ ## Design
20
+
21
+ The library is layered and modular:
22
+
23
+ - **Result** — explicit success/failure, no exceptions
24
+ - **Option** — nullable handling without null checks
25
+ - **Schema** — parse unknown input into typed values
26
+ - **Composition** — build sync and async pipelines
27
+
28
+ Each layer works independently. Combine them when it makes sense.
29
+
30
+ Fully tree-shakable:
31
+
32
+ - `result` — 582 B
33
+ - `option` — 402 B
34
+ - `composition` — 233 B
35
+ - `schema` — ~3 kB
36
+
37
+ **~4.2 kB total** (minified + brotli)
8
38
 
9
39
  ## Install
10
40
 
@@ -55,63 +85,48 @@ await compute({ x: '10', y: '2' }).then(console.log); // { valid: true, data: 5
55
85
 
56
86
  Validate at boundaries, chain operations, branch once at the end. Errors propagate automatically.
57
87
 
58
- ## Documentation
59
-
60
- - **[Getting Started](docs/GETTING_STARTED.md)** — Guided walkthrough from first principles to full pipelines
61
- - **[Recipes](docs/RECIPES.md)** — Point-free composition, error recovery, async pipelines, validation patterns
62
- - **[Advanced](docs/ADVANCED.md)** — Symbol branding, type inference, implementation details
63
- - **[Examples](examples/)** — Working code you can run
64
-
65
- For a complete real-world pipeline, see the [Launch Decision Pipeline](docs/RECIPES.md#full-example-launch-decision-pipeline) -- validates input, fetches weather data, and makes a GO/NO-GO decision.
66
-
67
- ## What's Included
68
-
69
- - **Result\<T, E\>** -- typed success/failure with map, flatMap, match, and recovery
70
- - **Option\<T\>** -- nullable handling without null checks
71
- - **Schema validation** -- parse unknown data, accumulate all errors, infer TypeScript types
72
- - **Composition** -- `pipe`, `flow`, `pipeAsync`, `flowAsync`, `curry`
73
- - **Async** -- sync-preserving overloads, parallel field validation, seamless sync/async mixing
74
- - **Standard Schema v1** -- `toStandardSchema()` for tRPC, TanStack Form, React Hook Form, and other consumers
75
-
76
- ## Ecosystem
77
-
78
- - **[@railway-ts/use-form](https://github.com/sakobu/railway-ts-use-form)** -- Type-safe React form hook. Define a schema here, get validated forms with autocomplete, error handling, and native HTML bindings.
79
-
80
- ## API Reference
81
-
82
- ### Option
88
+ ## Real-World Edge Case
83
89
 
84
- Handle nullable values without `if (x !== null)` everywhere.
90
+ Cross-field validation + async operations:
85
91
 
86
92
  ```typescript
87
- import { pipe } from '@railway-ts/pipelines/composition';
88
- import { some, mapWith, match } from '@railway-ts/pipelines/option';
89
-
90
- const user = some({ name: 'Alice', age: 25 });
91
- const name = pipe(
92
- user,
93
- mapWith((u) => u.name),
93
+ import {
94
+ object,
95
+ required,
96
+ chain,
97
+ string,
98
+ minLength,
99
+ refineAt,
100
+ validate,
101
+ } from '@railway-ts/pipelines/schema';
102
+ import { pipeAsync } from '@railway-ts/pipelines/composition';
103
+ import { flatMapWith, tapWith } from '@railway-ts/pipelines/result';
104
+
105
+ const signupSchema = chain(
106
+ object({
107
+ password: required(chain(string(), minLength(8))),
108
+ confirmPassword: required(string()),
109
+ }),
110
+ refineAt('confirmPassword', (d) => d.password === d.confirmPassword, 'Passwords must match'),
94
111
  );
95
112
 
96
- match(name, {
97
- some: (n) => console.log(n),
98
- none: () => console.log('No user'),
99
- }); // Output: Alice
113
+ const result = await pipeAsync(
114
+ validate(input, signupSchema),
115
+ flatMapWith(createUser),
116
+ tapWith((user) => sendWelcomeEmail(user.email)),
117
+ );
100
118
  ```
101
119
 
102
- | Category | Functions |
103
- |----------|-----------|
104
- | **Core** | `some`, `none`, `isSome`, `isNone` |
105
- | **Transform** | `map`, `flatMap`, `bimap`, `filter`, `tap` |
106
- | **Curried** | `mapWith`, `flatMapWith`, `filterWith`, `tapWith` |
107
- | **Unwrap** | `unwrap`, `unwrapOr`, `unwrapOrElse` |
108
- | **Combine** | `combine` |
109
- | **Convert** | `fromNullable`, `mapToResult` |
110
- | **Branch** | `match` |
120
+ - Accumulates validation errors
121
+ - Supports cross-field constraints
122
+ - Mixes sync + async steps seamlessly
123
+ - Branch once at the end
124
+
125
+ ## Core Modules
111
126
 
112
127
  ### Result
113
128
 
114
- Explicit error handling. No exceptions, no try-catch pyramids.
129
+ Typed success/failure without exceptions.
115
130
 
116
131
  ```typescript
117
132
  import { pipe } from '@railway-ts/pipelines/composition';
@@ -130,22 +145,37 @@ match(result, {
130
145
  }); // Output: 15
131
146
  ```
132
147
 
133
- | Category | Functions |
134
- |----------|-----------|
135
- | **Core** | `ok`, `err`, `isOk`, `isErr` |
136
- | **Transform** | `map`, `mapErr`, `flatMap`, `bimap`, `filter`, `tap`, `tapErr` |
137
- | **Curried** | `mapWith`, `flatMapWith`, `mapErrWith`, `filterWith`, `tapWith`, `tapErrWith` |
138
- | **Recovery** | `orElse`, `orElseWith` |
139
- | **Unwrap** | `unwrap`, `unwrapOr`, `unwrapOrElse` |
140
- | **Combine** | `combine`, `combineAll` |
141
- | **Convert** | `fromTry`, `fromTryWithError`, `fromPromise`, `fromPromiseWithError`, `toPromise`, `mapToOption` |
142
- | **Branch** | `match` |
148
+ `ok` · `err` · `isOk` · `isErr` · `map` · `flatMap` · `mapErr` · `bimap` · `filter` · `tap` · `tapErr` · `orElse` · `combine` · `combineAll` · `fromPromise` · `fromTry` · `match` · `unwrapOr` — [full API](docs/API.md#result)
149
+
150
+ ### Option
151
+
152
+ Nullable handling without `if (x !== null)` everywhere.
153
+
154
+ ```typescript
155
+ import { pipe } from '@railway-ts/pipelines/composition';
156
+ import { some, mapWith, match } from '@railway-ts/pipelines/option';
157
+
158
+ const user = some({ name: 'Alice', age: 25 });
159
+ const name = pipe(
160
+ user,
161
+ mapWith((u) => u.name),
162
+ );
163
+
164
+ match(name, {
165
+ some: (n) => console.log(n),
166
+ none: () => console.log('No user'),
167
+ }); // Output: Alice
168
+ ```
169
+
170
+ `some` · `none` · `isSome` · `isNone` · `map` · `flatMap` · `bimap` · `filter` · `tap` · `combine` · `fromNullable` · `mapToResult` · `match` · `unwrapOr` — [full API](docs/API.md#option)
171
+
172
+ Use independently, or convert to Result when needed.
143
173
 
144
174
  ### Schema
145
175
 
146
- Parse untrusted data into typed values. Accumulates all validation errors.
176
+ Parse unknown data into typed values. Accumulates all validation errors.
147
177
 
148
- > **Standard Schema v1 compliant** — use `toStandardSchema()` for interop with tRPC, TanStack Form, React Hook Form, and other Standard Schema consumers. See [Recipes → Standard Schema Interop](docs/RECIPES.md#standard-schema-interop).
178
+ > **Standard Schema v1 compliant** — use `toStandardSchema()` for interop with tRPC, TanStack Form, React Hook Form, and other Standard Schema consumers. See [Recipes -> Standard Schema Interop](docs/RECIPES.md#standard-schema-interop).
149
179
 
150
180
  ```typescript
151
181
  import {
@@ -174,28 +204,11 @@ const result = validate(input, userSchema);
174
204
  // Result<User, ValidationError[]>
175
205
  ```
176
206
 
177
- | Category | Functions |
178
- |----------|-----------|
179
- | **Primitives** | `string`, `number`, `boolean`, `date`, `bigint` |
180
- | **Parsers** | `parseNumber`, `parseString`, `parseBool`, `parseBigInt`, `parseDate`, `parseISODate`, `parseJSON`, `parseURL`, `parseEnum` |
181
- | **Structures** | `object`, `array`, `tuple`, `tupleOf` |
182
- | **Unions** | `union`, `discriminatedUnion`, `literal` |
183
- | **Modifiers** | `required`, `optional`, `nullable`, `emptyAsOptional` |
184
- | **String Constraints** | `minLength`, `maxLength`, `pattern`, `nonEmpty`, `email`, `phoneNumber` |
185
- | **Number Constraints** | `min`, `max`, `integer`, `finite`, `between`, `positive`, `negative`, `nonZero`, `divisibleBy`, `precision` |
186
- | **Date Constraints** | `dateRange`, `pastDate`, `futureDate`, `todayOrFuture` |
187
- | **Enums** | `stringEnum`, `enumValue`, `oneOf` |
188
- | **Boolean Constraints** | `matches` |
189
- | **Array Constraints** | `minItems`, `maxItems`, `notEmpty`, `unique` |
190
- | **Combinators** | `chain`, `transform`, `refine` |
191
- | **Cross-field** | `refineAt`, `refineAtAsync` |
192
- | **Async** | `chainAsync`, `refineAsync`, `refineAtAsync` |
193
- | **Utilities** | `validate`, `validateAndFormatResult`, `formatErrors`, `toStandardSchema`, `ROOT_ERROR_KEY` |
194
- | **Types** | `Validator`, `AsyncValidator`, `MaybeAsyncValidator`, `Schema`, `SyncSchema`, `InferSchemaType`, `ValidatorMapOutput`, `ValidationError`, `ValidationResult`, `StandardSchemaV1` |
207
+ `object` · `array` · `tuple` · `required` · `optional` · `chain` · `string` · `parseNumber` · `email` · `min` · `max` · `union` · `discriminatedUnion` · `refine` · `refineAt` · `transform` · `validate` · `toStandardSchema` — [full API](docs/API.md#schema)
195
208
 
196
209
  ### Composition
197
210
 
198
- Build pipelines. No nested function calls.
211
+ Build readable pipelines. No nested function calls.
199
212
 
200
213
  ```typescript
201
214
  import { pipe, flow, pipeAsync, flowAsync } from '@railway-ts/pipelines/composition';
@@ -222,32 +235,32 @@ const processOrder = flowAsync(validateOrder, chargePayment, createShipment);
222
235
  await processOrder(orderInput);
223
236
  ```
224
237
 
225
- | Category | Functions |
226
- |----------|-----------|
227
- | **Sync** | `pipe`, `flow`, `curry`, `uncurry`, `tupled`, `untupled` |
228
- | **Async** | `pipeAsync`, `flowAsync` |
229
- | **Types** | `MaybeAsync` |
238
+ `pipe` · `flow` · `pipeAsync` · `flowAsync` · `curry` · `uncurry` · `tupled` · `untupled` — [full API](docs/API.md#composition)
230
239
 
231
- ## Examples
240
+ Sync and async composition share the same mental model.
232
241
 
233
- Clone and run:
242
+ ## Ecosystem
234
243
 
235
- ```bash
236
- git clone https://github.com/sakobu/railway-ts-pipelines.git
237
- cd railway-ts-pipelines
238
- bun install
239
- bun run examples/index.ts
240
- ```
244
+ - **[@railway-ts/use-form](https://github.com/sakobu/railway-ts-use-form)** — Type-safe React form hook built directly on the Schema layer. Define a schema once — use it for validation, type inference, and form state.
245
+
246
+ ## Documentation
247
+
248
+ - **[Getting Started](docs/GETTING_STARTED.md)** — Learn the concepts, one at a time
249
+ - **[Recipes](docs/RECIPES.md)** — Patterns for real work: async pipelines, error recovery, validation, Standard Schema
250
+ - **[API Reference](docs/API.md)** — Every function signature and description
251
+ - **[Examples](examples/)** — Working code you can run
252
+
253
+ For a complete real-world pipeline, see the [Launch Decision Pipeline](docs/RECIPES.md#full-example-launch-decision-pipeline) -- validates input, fetches weather data, and makes a GO/NO-GO decision.
241
254
 
242
- **What's in there:**
255
+ ## Philosophy
243
256
 
244
- - `option/` - Nullable handling patterns, curried helpers
245
- - `result/` - Error handling patterns, curried helpers, recovery
246
- - `schema/` - Validation (basic, unions, tuples, async validation)
247
- - `composition/` - Function composition (sync and async)
248
- - `complete-pipelines/` - Full examples with validation + async + logic
257
+ - Explicit over implicit
258
+ - No exceptions for control flow
259
+ - No required runtime
260
+ - No hidden global state
261
+ - Import only what you use
249
262
 
250
- Start with `examples/complete-pipelines/async-launch.ts` for a real-world pattern.
263
+ Small pieces. Composable layers. One error model.
251
264
 
252
265
  ## Contributing
253
266
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@railway-ts/pipelines",
3
- "version": "0.1.11",
3
+ "version": "0.1.13",
4
4
  "description": "Railway-oriented pipelines for TypeScript: a lightweight fp-style Result/Option and schema layer for handling errors and validating boundaries.",
5
5
  "type": "module",
6
6
  "exports": {