@railway-ts/pipelines 0.1.19 → 0.1.20
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.
- package/docs/API.md +1439 -0
- package/docs/GETTING_STARTED.md +301 -0
- package/docs/RECIPES.md +890 -0
- package/package.json +2 -1
package/docs/API.md
ADDED
|
@@ -0,0 +1,1439 @@
|
|
|
1
|
+
# API Reference
|
|
2
|
+
|
|
3
|
+
Complete reference for every export in `@railway-ts/pipelines`.
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
## Result
|
|
8
|
+
|
|
9
|
+
```typescript
|
|
10
|
+
import { ... } from '@railway-ts/pipelines/result';
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Typed success/failure. No exceptions, no try-catch pyramids.
|
|
14
|
+
|
|
15
|
+
> **Implementation note: Symbol branding.** `Result` uses a unique symbol brand (`RESULT_BRAND`) to prevent structural typing issues. Plain objects like `{ ok: true, value: 42 }` won't satisfy the `Result` type — only values created through `ok()` and `err()` are valid. This prevents subtle bugs from accidental duck typing.
|
|
16
|
+
|
|
17
|
+
### Constructors
|
|
18
|
+
|
|
19
|
+
#### `ok`
|
|
20
|
+
|
|
21
|
+
```typescript
|
|
22
|
+
function ok<T, E = never>(value: T): Result<T, E>;
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
Create a Result containing a success value.
|
|
26
|
+
|
|
27
|
+
#### `err`
|
|
28
|
+
|
|
29
|
+
```typescript
|
|
30
|
+
function err<E>(error: E): Result<never, E>;
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
Create a Result containing an error.
|
|
34
|
+
|
|
35
|
+
### Type Guards
|
|
36
|
+
|
|
37
|
+
#### `isOk`
|
|
38
|
+
|
|
39
|
+
```typescript
|
|
40
|
+
function isOk<T, E>(result: Result<T, E>): result is { ok: true; value: T };
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
Check if a Result is an Ok variant. Narrows the type so `result.value` is accessible.
|
|
44
|
+
|
|
45
|
+
#### `isErr`
|
|
46
|
+
|
|
47
|
+
```typescript
|
|
48
|
+
function isErr<T, E>(result: Result<T, E>): result is { ok: false; error: E };
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
Check if a Result is an Err variant. Narrows the type so `result.error` is accessible.
|
|
52
|
+
|
|
53
|
+
### Transformations
|
|
54
|
+
|
|
55
|
+
#### `map`
|
|
56
|
+
|
|
57
|
+
```typescript
|
|
58
|
+
function map<T, E, U>(result: Result<T, E>, fn: (value: T) => U): Result<U, E>;
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
Transform the Ok value. Err passes through unchanged.
|
|
62
|
+
|
|
63
|
+
#### `flatMap`
|
|
64
|
+
|
|
65
|
+
```typescript
|
|
66
|
+
function flatMap<T, U, E1, E2>(result: Result<T, E1>, fn: (value: T) => Result<U, E2>): Result<U, E1 | E2>;
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
Transform the Ok value with a function that returns a Result. Useful for chaining operations that can fail.
|
|
70
|
+
|
|
71
|
+
#### `mapErr`
|
|
72
|
+
|
|
73
|
+
```typescript
|
|
74
|
+
function mapErr<T, E, F>(result: Result<T, E>, fn: (error: E) => F): Result<T, F>;
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
Transform the Err value. Ok passes through unchanged.
|
|
78
|
+
|
|
79
|
+
#### `bimap`
|
|
80
|
+
|
|
81
|
+
```typescript
|
|
82
|
+
function bimap<T, E, U, F>(result: Result<T, E>, okFn: (value: T) => U, errFn: (error: E) => F): Result<U, F>;
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
Transform both Ok and Err branches in one call.
|
|
86
|
+
|
|
87
|
+
#### `filter`
|
|
88
|
+
|
|
89
|
+
```typescript
|
|
90
|
+
function filter<T, E1, E2>(result: Result<T, E1>, predicate: (value: T) => boolean, error: E2): Result<T, E1 | E2>;
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
Keep the Ok value if the predicate passes, otherwise return the provided error. Err passes through unchanged.
|
|
94
|
+
|
|
95
|
+
### Side Effects
|
|
96
|
+
|
|
97
|
+
#### `tap`
|
|
98
|
+
|
|
99
|
+
```typescript
|
|
100
|
+
function tap<T, E>(result: Result<T, E>, fn: (value: T) => void): Result<T, E>;
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
Execute a side effect on the Ok value without changing the Result. Useful for logging.
|
|
104
|
+
|
|
105
|
+
#### `tapErr`
|
|
106
|
+
|
|
107
|
+
```typescript
|
|
108
|
+
function tapErr<T, E>(result: Result<T, E>, fn: (error: E) => void): Result<T, E>;
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
Execute a side effect on the Err value without changing the Result.
|
|
112
|
+
|
|
113
|
+
### Recovery
|
|
114
|
+
|
|
115
|
+
#### `orElse`
|
|
116
|
+
|
|
117
|
+
```typescript
|
|
118
|
+
function orElse<T1, E1, T2, E2>(result: Result<T1, E1>, fn: (error: E1) => Result<T2, E2>): Result<T1 | T2, E2>;
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
Recover from an Err by applying `fn` to the error. Ok passes through unchanged.
|
|
122
|
+
|
|
123
|
+
### Pattern Matching
|
|
124
|
+
|
|
125
|
+
#### `match`
|
|
126
|
+
|
|
127
|
+
```typescript
|
|
128
|
+
function match<T, E, R1, R2>(result: Result<T, E>, patterns: { ok: (value: T) => R1; err: (error: E) => R2 }): R1 | R2;
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
Handle both Ok and Err cases, returning a single value.
|
|
132
|
+
|
|
133
|
+
```typescript
|
|
134
|
+
const message = match(result, {
|
|
135
|
+
ok: (value) => `Got ${value}`,
|
|
136
|
+
err: (error) => `Failed: ${error}`,
|
|
137
|
+
});
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
### Unwrap
|
|
141
|
+
|
|
142
|
+
#### `unwrap`
|
|
143
|
+
|
|
144
|
+
```typescript
|
|
145
|
+
function unwrap<T, E>(result: Result<T, E>, errorMsg?: string): T;
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
Extract the Ok value, throwing if Err. **Prototyping only** — prefer `match` or `unwrapOr`.
|
|
149
|
+
|
|
150
|
+
#### `unwrapOr`
|
|
151
|
+
|
|
152
|
+
```typescript
|
|
153
|
+
function unwrapOr<T, E>(result: Result<T, E>, defaultValue: T): T;
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
Extract the Ok value, or return a default if Err.
|
|
157
|
+
|
|
158
|
+
#### `unwrapOrElse`
|
|
159
|
+
|
|
160
|
+
```typescript
|
|
161
|
+
function unwrapOrElse<T, E>(result: Result<T, E>, defaultFn: () => T): T;
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
Extract the Ok value, or compute a default lazily if Err.
|
|
165
|
+
|
|
166
|
+
### Combining
|
|
167
|
+
|
|
168
|
+
#### `combine`
|
|
169
|
+
|
|
170
|
+
```typescript
|
|
171
|
+
function combine<T, E>(results: readonly Result<T, E>[]): Result<T[], E>;
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
Combine an array of Results into a single Result. Short-circuits on the first Err. Preserves tuple types for up to 10 elements.
|
|
175
|
+
|
|
176
|
+
```typescript
|
|
177
|
+
combine([ok(1), ok(2), ok(3)]); // ok([1, 2, 3])
|
|
178
|
+
combine([ok(1), err('fail'), ok(3)]); // err('fail')
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
#### `combineAll`
|
|
182
|
+
|
|
183
|
+
```typescript
|
|
184
|
+
function combineAll<T, E>(results: readonly Result<T, E>[]): Result<T[], E[]>;
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
Like `combine`, but collects **all** errors instead of short-circuiting.
|
|
188
|
+
|
|
189
|
+
```typescript
|
|
190
|
+
combineAll([ok(1), err('a'), err('b')]); // err(['a', 'b'])
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
> **Implementation note: Tuple preservation.** Both `combine` and `combineAll` use 10 overloads to preserve exact tuple types instead of widening to arrays. `combine([ok(42), ok('hello')])` returns `Result<[number, string], E>`, not `Result<(number | string)[], E>`. This gives you type-safe destructuring at each position. Arrays longer than 10 elements fall back to union array types.
|
|
194
|
+
|
|
195
|
+
#### `partition`
|
|
196
|
+
|
|
197
|
+
```typescript
|
|
198
|
+
function partition<T, E>(results: Result<T, E>[]): { successes: T[]; failures: E[] };
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
Separates an array of Results into successes and failures. Unlike `combineAll` which is all-or-nothing, `partition` preserves both sides — useful for batch processing where partial success is meaningful.
|
|
202
|
+
|
|
203
|
+
```typescript
|
|
204
|
+
partition([ok(1), err('a'), ok(2), err('b')]);
|
|
205
|
+
// { successes: [1, 2], failures: ['a', 'b'] }
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
### Conversions
|
|
209
|
+
|
|
210
|
+
#### `mapToOption`
|
|
211
|
+
|
|
212
|
+
```typescript
|
|
213
|
+
function mapToOption<T, E>(result: Result<T, E>): Option<T>;
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
Convert to Option. Ok becomes Some, Err becomes None.
|
|
217
|
+
|
|
218
|
+
#### `fromTry`
|
|
219
|
+
|
|
220
|
+
```typescript
|
|
221
|
+
function fromTry<T>(f: () => T): Result<T, string>;
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
Execute a function that might throw. Returns Ok on success, Err with the error message on throw.
|
|
225
|
+
|
|
226
|
+
#### `fromTryWithError`
|
|
227
|
+
|
|
228
|
+
```typescript
|
|
229
|
+
function fromTryWithError<T>(f: () => T): Result<T, Error>;
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
Like `fromTry`, but preserves the full Error object (stack trace, custom properties).
|
|
233
|
+
|
|
234
|
+
#### `fromPromise`
|
|
235
|
+
|
|
236
|
+
```typescript
|
|
237
|
+
function fromPromise<T>(promise: Promise<T>): Promise<Result<T, string>>;
|
|
238
|
+
```
|
|
239
|
+
|
|
240
|
+
Convert a Promise to a Result. Rejects become Err with the error message as a string.
|
|
241
|
+
|
|
242
|
+
#### `fromPromiseWithError`
|
|
243
|
+
|
|
244
|
+
```typescript
|
|
245
|
+
function fromPromiseWithError<T, E = unknown>(
|
|
246
|
+
promise: Promise<T>,
|
|
247
|
+
errorFn?: (error: unknown) => E,
|
|
248
|
+
): Promise<Result<T, E>>;
|
|
249
|
+
```
|
|
250
|
+
|
|
251
|
+
Like `fromPromise`, but lets you transform the error with a custom function.
|
|
252
|
+
|
|
253
|
+
#### `toPromise`
|
|
254
|
+
|
|
255
|
+
```typescript
|
|
256
|
+
function toPromise<T, E>(result: Result<T, E>): Promise<T>;
|
|
257
|
+
```
|
|
258
|
+
|
|
259
|
+
Convert a Result to a Promise. Ok resolves, Err rejects.
|
|
260
|
+
|
|
261
|
+
### Curried Variants (Point-Free)
|
|
262
|
+
|
|
263
|
+
These return functions suitable for use in `pipe` and `flow`.
|
|
264
|
+
|
|
265
|
+
#### `mapWith`
|
|
266
|
+
|
|
267
|
+
```typescript
|
|
268
|
+
function mapWith<T, U>(fn: (value: T) => U): <E>(result: Result<T, E>) => Result<U, E>;
|
|
269
|
+
```
|
|
270
|
+
|
|
271
|
+
#### `flatMapWith`
|
|
272
|
+
|
|
273
|
+
```typescript
|
|
274
|
+
function flatMapWith<T, U, E2>(fn: (value: T) => Result<U, E2>): <E1>(result: Result<T, E1>) => Result<U, E1 | E2>;
|
|
275
|
+
function flatMapWith<T, U, E2>(
|
|
276
|
+
fn: (value: T) => Promise<Result<U, E2>>,
|
|
277
|
+
): <E1>(result: Result<T, E1>) => Promise<Result<U, E1 | E2>>;
|
|
278
|
+
```
|
|
279
|
+
|
|
280
|
+
Supports both sync and async step functions.
|
|
281
|
+
|
|
282
|
+
#### `mapErrWith`
|
|
283
|
+
|
|
284
|
+
```typescript
|
|
285
|
+
function mapErrWith<E, F>(fn: (error: E) => F): <T>(result: Result<T, E>) => Result<T, F>;
|
|
286
|
+
```
|
|
287
|
+
|
|
288
|
+
#### `filterWith`
|
|
289
|
+
|
|
290
|
+
```typescript
|
|
291
|
+
function filterWith<T, E2>(
|
|
292
|
+
predicate: (value: T) => boolean,
|
|
293
|
+
error: E2,
|
|
294
|
+
): <E1>(result: Result<T, E1>) => Result<T, E1 | E2>;
|
|
295
|
+
```
|
|
296
|
+
|
|
297
|
+
#### `tapWith`
|
|
298
|
+
|
|
299
|
+
```typescript
|
|
300
|
+
function tapWith<T>(fn: (value: T) => void): <E>(result: Result<T, E>) => Result<T, E>;
|
|
301
|
+
function tapWith<T>(fn: (value: T) => Promise<void>): <E>(result: Result<T, E>) => Promise<Result<T, E>>;
|
|
302
|
+
```
|
|
303
|
+
|
|
304
|
+
Supports both sync and async side effects.
|
|
305
|
+
|
|
306
|
+
#### `tapErrWith`
|
|
307
|
+
|
|
308
|
+
```typescript
|
|
309
|
+
function tapErrWith<E>(fn: (error: E) => void): <T>(result: Result<T, E>) => Result<T, E>;
|
|
310
|
+
function tapErrWith<E>(fn: (error: E) => Promise<void>): <T>(result: Result<T, E>) => Promise<Result<T, E>>;
|
|
311
|
+
```
|
|
312
|
+
|
|
313
|
+
#### `orElseWith`
|
|
314
|
+
|
|
315
|
+
```typescript
|
|
316
|
+
function orElseWith<E1, T2, E2>(fn: (error: E1) => Result<T2, E2>): <T1>(result: Result<T1, E1>) => Result<T1 | T2, E2>;
|
|
317
|
+
```
|
|
318
|
+
|
|
319
|
+
---
|
|
320
|
+
|
|
321
|
+
## Option
|
|
322
|
+
|
|
323
|
+
```typescript
|
|
324
|
+
import { ... } from '@railway-ts/pipelines/option';
|
|
325
|
+
```
|
|
326
|
+
|
|
327
|
+
Nullable handling without null checks.
|
|
328
|
+
|
|
329
|
+
> **Implementation note: Symbol branding.** Like `Result`, `Option` uses a unique symbol brand (`OPTION_BRAND`). Only values created through `some()` and `none()` satisfy the `Option` type.
|
|
330
|
+
|
|
331
|
+
### Constructors
|
|
332
|
+
|
|
333
|
+
#### `some`
|
|
334
|
+
|
|
335
|
+
```typescript
|
|
336
|
+
function some<T>(value: T): Option<T>;
|
|
337
|
+
```
|
|
338
|
+
|
|
339
|
+
Create an Option containing a value.
|
|
340
|
+
|
|
341
|
+
#### `none`
|
|
342
|
+
|
|
343
|
+
```typescript
|
|
344
|
+
function none<T = never>(): Option<T>;
|
|
345
|
+
```
|
|
346
|
+
|
|
347
|
+
Create an Option containing nothing.
|
|
348
|
+
|
|
349
|
+
### Type Guards
|
|
350
|
+
|
|
351
|
+
#### `isSome`
|
|
352
|
+
|
|
353
|
+
```typescript
|
|
354
|
+
function isSome<T>(option: Option<T>): option is { some: true; value: T };
|
|
355
|
+
```
|
|
356
|
+
|
|
357
|
+
Check if an Option is a Some variant.
|
|
358
|
+
|
|
359
|
+
#### `isNone`
|
|
360
|
+
|
|
361
|
+
```typescript
|
|
362
|
+
function isNone<T>(option: Option<T>): option is { some: false };
|
|
363
|
+
```
|
|
364
|
+
|
|
365
|
+
Check if an Option is a None variant.
|
|
366
|
+
|
|
367
|
+
### Transformations
|
|
368
|
+
|
|
369
|
+
#### `map`
|
|
370
|
+
|
|
371
|
+
```typescript
|
|
372
|
+
function map<T, U>(option: Option<T>, fn: (value: T) => U): Option<U>;
|
|
373
|
+
```
|
|
374
|
+
|
|
375
|
+
Transform the Some value. None passes through.
|
|
376
|
+
|
|
377
|
+
#### `flatMap`
|
|
378
|
+
|
|
379
|
+
```typescript
|
|
380
|
+
function flatMap<T, U>(option: Option<T>, fn: (value: T) => Option<U>): Option<U>;
|
|
381
|
+
```
|
|
382
|
+
|
|
383
|
+
Transform the Some value with a function that returns an Option.
|
|
384
|
+
|
|
385
|
+
#### `bimap`
|
|
386
|
+
|
|
387
|
+
```typescript
|
|
388
|
+
function bimap<T, U>(option: Option<T>, someFn: (value: T) => Option<U>, noneFn: () => Option<U>): Option<U>;
|
|
389
|
+
```
|
|
390
|
+
|
|
391
|
+
Handle both Some and None branches, each returning a new Option.
|
|
392
|
+
|
|
393
|
+
#### `filter`
|
|
394
|
+
|
|
395
|
+
```typescript
|
|
396
|
+
function filter<T>(option: Option<T>, predicate: (value: T) => boolean): Option<T>;
|
|
397
|
+
```
|
|
398
|
+
|
|
399
|
+
Keep the Some value if the predicate passes, otherwise return None.
|
|
400
|
+
|
|
401
|
+
### Side Effects
|
|
402
|
+
|
|
403
|
+
#### `tap`
|
|
404
|
+
|
|
405
|
+
```typescript
|
|
406
|
+
function tap<T>(option: Option<T>, fn: (value: T) => void): Option<T>;
|
|
407
|
+
```
|
|
408
|
+
|
|
409
|
+
Execute a side effect on the Some value without changing the Option.
|
|
410
|
+
|
|
411
|
+
### Pattern Matching
|
|
412
|
+
|
|
413
|
+
#### `match`
|
|
414
|
+
|
|
415
|
+
```typescript
|
|
416
|
+
function match<T, R1, R2>(option: Option<T>, patterns: { some: (value: T) => R1; none: () => R2 }): R1 | R2;
|
|
417
|
+
```
|
|
418
|
+
|
|
419
|
+
Handle both Some and None cases, returning a single value.
|
|
420
|
+
|
|
421
|
+
### Unwrap
|
|
422
|
+
|
|
423
|
+
#### `unwrap`
|
|
424
|
+
|
|
425
|
+
```typescript
|
|
426
|
+
function unwrap<T>(option: Option<T>, errorMsg?: string): T;
|
|
427
|
+
```
|
|
428
|
+
|
|
429
|
+
Extract the Some value, throwing if None. **Prototyping only** — prefer `match` or `unwrapOr`.
|
|
430
|
+
|
|
431
|
+
#### `unwrapOr`
|
|
432
|
+
|
|
433
|
+
```typescript
|
|
434
|
+
function unwrapOr<T>(option: Option<T>, defaultValue: T): T;
|
|
435
|
+
```
|
|
436
|
+
|
|
437
|
+
Extract the Some value, or return a default if None.
|
|
438
|
+
|
|
439
|
+
#### `unwrapOrElse`
|
|
440
|
+
|
|
441
|
+
```typescript
|
|
442
|
+
function unwrapOrElse<T>(option: Option<T>, defaultFn: () => T): T;
|
|
443
|
+
```
|
|
444
|
+
|
|
445
|
+
Extract the Some value, or compute a default lazily if None.
|
|
446
|
+
|
|
447
|
+
### Combining
|
|
448
|
+
|
|
449
|
+
#### `combine`
|
|
450
|
+
|
|
451
|
+
```typescript
|
|
452
|
+
function combine<T>(options: readonly Option<T>[]): Option<T[]>;
|
|
453
|
+
```
|
|
454
|
+
|
|
455
|
+
Combine an array of Options into a single Option. Returns None if any element is None. Preserves tuple types for up to 10 elements.
|
|
456
|
+
|
|
457
|
+
> **Implementation note: Tuple preservation.** Like Result's `combine`, uses 10 overloads to preserve exact tuple types. `combine([some(1), some('a')])` returns `Option<[number, string]>`, not `Option<(number | string)[]>`.
|
|
458
|
+
|
|
459
|
+
### Conversions
|
|
460
|
+
|
|
461
|
+
#### `fromNullable`
|
|
462
|
+
|
|
463
|
+
```typescript
|
|
464
|
+
function fromNullable<T>(value: T | null | undefined): Option<T>;
|
|
465
|
+
```
|
|
466
|
+
|
|
467
|
+
Create an Option from a nullable value. `null`/`undefined` become None, everything else becomes Some.
|
|
468
|
+
|
|
469
|
+
#### `mapToResult`
|
|
470
|
+
|
|
471
|
+
```typescript
|
|
472
|
+
function mapToResult<T, E>(option: Option<T>, error: E): Result<T, E>;
|
|
473
|
+
```
|
|
474
|
+
|
|
475
|
+
Convert to Result. Some becomes Ok, None becomes Err with the provided error.
|
|
476
|
+
|
|
477
|
+
### Curried Variants (Point-Free)
|
|
478
|
+
|
|
479
|
+
#### `mapWith`
|
|
480
|
+
|
|
481
|
+
```typescript
|
|
482
|
+
function mapWith<T, U>(fn: (value: T) => U): (option: Option<T>) => Option<U>;
|
|
483
|
+
```
|
|
484
|
+
|
|
485
|
+
#### `flatMapWith`
|
|
486
|
+
|
|
487
|
+
```typescript
|
|
488
|
+
function flatMapWith<T, U>(fn: (value: T) => Option<U>): (option: Option<T>) => Option<U>;
|
|
489
|
+
function flatMapWith<T, U>(fn: (value: T) => Promise<Option<U>>): (option: Option<T>) => Promise<Option<U>>;
|
|
490
|
+
```
|
|
491
|
+
|
|
492
|
+
Supports both sync and async step functions.
|
|
493
|
+
|
|
494
|
+
#### `filterWith`
|
|
495
|
+
|
|
496
|
+
```typescript
|
|
497
|
+
function filterWith<T>(predicate: (value: T) => boolean): (option: Option<T>) => Option<T>;
|
|
498
|
+
```
|
|
499
|
+
|
|
500
|
+
#### `tapWith`
|
|
501
|
+
|
|
502
|
+
```typescript
|
|
503
|
+
function tapWith<T>(fn: (value: T) => void): (option: Option<T>) => Option<T>;
|
|
504
|
+
function tapWith<T>(fn: (value: T) => Promise<void>): (option: Option<T>) => Promise<Option<T>>;
|
|
505
|
+
```
|
|
506
|
+
|
|
507
|
+
Supports both sync and async side effects.
|
|
508
|
+
|
|
509
|
+
#### `mapToResultWith`
|
|
510
|
+
|
|
511
|
+
```typescript
|
|
512
|
+
function mapToResultWith<E>(error: E): <T>(option: Option<T>) => Result<T, E>;
|
|
513
|
+
```
|
|
514
|
+
|
|
515
|
+
Curried version of `mapToResult`. Partially applies the error value, returning a function suitable for `pipe` and `flow`.
|
|
516
|
+
|
|
517
|
+
---
|
|
518
|
+
|
|
519
|
+
## Schema
|
|
520
|
+
|
|
521
|
+
```typescript
|
|
522
|
+
import { ... } from '@railway-ts/pipelines/schema';
|
|
523
|
+
```
|
|
524
|
+
|
|
525
|
+
Parse untrusted data into typed values. Accumulates all validation errors.
|
|
526
|
+
|
|
527
|
+
### Core Types
|
|
528
|
+
|
|
529
|
+
#### `Validator<I, O>`
|
|
530
|
+
|
|
531
|
+
```typescript
|
|
532
|
+
type Validator<I, O = I> = (value: I, path?: string[]) => Result<O, ValidationError[]>;
|
|
533
|
+
```
|
|
534
|
+
|
|
535
|
+
A synchronous validator function.
|
|
536
|
+
|
|
537
|
+
#### `AsyncValidator<I, O>`
|
|
538
|
+
|
|
539
|
+
```typescript
|
|
540
|
+
type AsyncValidator<I, O = I> = (value: I, path?: string[]) => Promise<Result<O, ValidationError[]>>;
|
|
541
|
+
```
|
|
542
|
+
|
|
543
|
+
An asynchronous validator that always returns a Promise.
|
|
544
|
+
|
|
545
|
+
#### `MaybeAsyncValidator<I, O>`
|
|
546
|
+
|
|
547
|
+
```typescript
|
|
548
|
+
type MaybeAsyncValidator<I, O = I> = (
|
|
549
|
+
value: I,
|
|
550
|
+
path?: string[],
|
|
551
|
+
) => Result<O, ValidationError[]> | Promise<Result<O, ValidationError[]>>;
|
|
552
|
+
```
|
|
553
|
+
|
|
554
|
+
A validator that may return either sync or async. Used when mixing sync and async field validators.
|
|
555
|
+
|
|
556
|
+
#### `Schema<T>`
|
|
557
|
+
|
|
558
|
+
```typescript
|
|
559
|
+
type Schema<T> = { [K in keyof T]: MaybeAsyncValidator<unknown, T[K]> };
|
|
560
|
+
```
|
|
561
|
+
|
|
562
|
+
A validation schema where keys map to validators. Accepts both sync and async validators.
|
|
563
|
+
|
|
564
|
+
#### `SyncSchema<T>`
|
|
565
|
+
|
|
566
|
+
```typescript
|
|
567
|
+
type SyncSchema<T> = { [K in keyof T]: Validator<unknown, T[K]> };
|
|
568
|
+
```
|
|
569
|
+
|
|
570
|
+
A schema where all validators are synchronous. Preserves sync return types.
|
|
571
|
+
|
|
572
|
+
#### `ValidationError`
|
|
573
|
+
|
|
574
|
+
```typescript
|
|
575
|
+
type ValidationError = { path: string[]; message: string };
|
|
576
|
+
```
|
|
577
|
+
|
|
578
|
+
A validation error with path information and a message.
|
|
579
|
+
|
|
580
|
+
#### `ValidationResult<T>`
|
|
581
|
+
|
|
582
|
+
```typescript
|
|
583
|
+
type ValidationResult<T> = { valid: true; data: T } | { valid: false; errors: Record<string, string> };
|
|
584
|
+
```
|
|
585
|
+
|
|
586
|
+
The result of a validation-and-format operation.
|
|
587
|
+
|
|
588
|
+
#### `InferSchemaType<V>`
|
|
589
|
+
|
|
590
|
+
```typescript
|
|
591
|
+
type InferSchemaType<V> = ProcessType<InferType<V>>;
|
|
592
|
+
```
|
|
593
|
+
|
|
594
|
+
Infer the output TypeScript type from a validator or schema.
|
|
595
|
+
|
|
596
|
+
```typescript
|
|
597
|
+
const schema = object({ name: required(string()), age: required(number()) });
|
|
598
|
+
type User = InferSchemaType<typeof schema>; // { name: string; age: number }
|
|
599
|
+
```
|
|
600
|
+
|
|
601
|
+
#### `ValidatorMapOutput<M>`
|
|
602
|
+
|
|
603
|
+
```typescript
|
|
604
|
+
type ValidatorMapOutput<M extends Record<string, MaybeAsyncValidator<unknown, unknown>>>
|
|
605
|
+
```
|
|
606
|
+
|
|
607
|
+
Extract the union of output types from a validator map. Used by `discriminatedUnion`.
|
|
608
|
+
|
|
609
|
+
### Structures
|
|
610
|
+
|
|
611
|
+
#### `object`
|
|
612
|
+
|
|
613
|
+
```typescript
|
|
614
|
+
function object<T>(schema: SyncSchema<T>, options?: { strict?: boolean }): Validator<unknown, T>;
|
|
615
|
+
function object<T>(schema: Schema<T>, options?: { strict?: boolean }): MaybeAsyncValidator<unknown, T>;
|
|
616
|
+
```
|
|
617
|
+
|
|
618
|
+
Validate objects against a schema. Each key maps to a validator. Strict mode (default) rejects extra properties. Fields are validated in parallel when async.
|
|
619
|
+
|
|
620
|
+
```typescript
|
|
621
|
+
const userSchema = object({
|
|
622
|
+
name: required(string()),
|
|
623
|
+
age: required(chain(parseNumber(), min(18))),
|
|
624
|
+
});
|
|
625
|
+
```
|
|
626
|
+
|
|
627
|
+
#### `array`
|
|
628
|
+
|
|
629
|
+
```typescript
|
|
630
|
+
function array<I, O>(itemValidator: Validator<I, O>): Validator<unknown, O[]>;
|
|
631
|
+
function array<I, O>(itemValidator: MaybeAsyncValidator<I, O>): MaybeAsyncValidator<unknown, O[]>;
|
|
632
|
+
```
|
|
633
|
+
|
|
634
|
+
Validate arrays where each item is validated by the provided validator.
|
|
635
|
+
|
|
636
|
+
#### `tuple`
|
|
637
|
+
|
|
638
|
+
```typescript
|
|
639
|
+
function tuple<V extends ReadonlyArray<Validator<unknown, unknown>>>(validators: V): Validator<unknown, TupleType<V>>;
|
|
640
|
+
```
|
|
641
|
+
|
|
642
|
+
Validate a heterogeneous tuple where each position has its own validator.
|
|
643
|
+
|
|
644
|
+
```typescript
|
|
645
|
+
const validate = tuple([string(), number(), boolean()]);
|
|
646
|
+
validate(['a', 1, true]); // ok(["a", 1, true])
|
|
647
|
+
```
|
|
648
|
+
|
|
649
|
+
#### `tupleOf`
|
|
650
|
+
|
|
651
|
+
```typescript
|
|
652
|
+
function tupleOf<T>(elementValidator: Validator<unknown, T>, length: number): Validator<unknown, T[]>;
|
|
653
|
+
```
|
|
654
|
+
|
|
655
|
+
Validate a homogeneous tuple with a fixed length. All elements share the same type.
|
|
656
|
+
|
|
657
|
+
```typescript
|
|
658
|
+
const point3D = tupleOf(number(), 3); // [number, number, number]
|
|
659
|
+
```
|
|
660
|
+
|
|
661
|
+
### Field Modifiers
|
|
662
|
+
|
|
663
|
+
#### `required`
|
|
664
|
+
|
|
665
|
+
```typescript
|
|
666
|
+
function required<I, O>(validator: Validator<I, O>, message?: string): Validator<I | undefined | null, O>;
|
|
667
|
+
```
|
|
668
|
+
|
|
669
|
+
Require a value to be defined (not null or undefined).
|
|
670
|
+
|
|
671
|
+
#### `optional`
|
|
672
|
+
|
|
673
|
+
```typescript
|
|
674
|
+
function optional<I, O>(validator: Validator<I, O>): Validator<I | undefined | null, O | undefined>;
|
|
675
|
+
```
|
|
676
|
+
|
|
677
|
+
Allow null/undefined values. Returns `undefined` for missing values, otherwise applies the validator.
|
|
678
|
+
|
|
679
|
+
#### `nullable`
|
|
680
|
+
|
|
681
|
+
```typescript
|
|
682
|
+
function nullable(message?: string): Validator<unknown, null>;
|
|
683
|
+
```
|
|
684
|
+
|
|
685
|
+
Accept only `null` values.
|
|
686
|
+
|
|
687
|
+
#### `emptyAsOptional`
|
|
688
|
+
|
|
689
|
+
```typescript
|
|
690
|
+
function emptyAsOptional<I, O>(validator: Validator<I, O>): Validator<I | undefined | null, O | undefined>;
|
|
691
|
+
```
|
|
692
|
+
|
|
693
|
+
Treat empty strings, empty arrays, and empty objects as optional (returns `undefined`).
|
|
694
|
+
|
|
695
|
+
#### `literal`
|
|
696
|
+
|
|
697
|
+
```typescript
|
|
698
|
+
function literal<T>(expectedValue: T, message?: string): Validator<unknown, T>;
|
|
699
|
+
```
|
|
700
|
+
|
|
701
|
+
Accept only an exact value match.
|
|
702
|
+
|
|
703
|
+
```typescript
|
|
704
|
+
const validate = literal('active');
|
|
705
|
+
validate('active'); // ok("active")
|
|
706
|
+
validate('completed'); // err(...)
|
|
707
|
+
```
|
|
708
|
+
|
|
709
|
+
### Primitives
|
|
710
|
+
|
|
711
|
+
#### `string`
|
|
712
|
+
|
|
713
|
+
```typescript
|
|
714
|
+
function string(message?: string): Validator<unknown, string>;
|
|
715
|
+
```
|
|
716
|
+
|
|
717
|
+
Ensure the value is a string.
|
|
718
|
+
|
|
719
|
+
#### `number`
|
|
720
|
+
|
|
721
|
+
```typescript
|
|
722
|
+
function number(message?: string): Validator<unknown, number>;
|
|
723
|
+
```
|
|
724
|
+
|
|
725
|
+
Ensure the value is a number (not NaN).
|
|
726
|
+
|
|
727
|
+
#### `boolean`
|
|
728
|
+
|
|
729
|
+
```typescript
|
|
730
|
+
function boolean(message?: string): Validator<unknown, boolean>;
|
|
731
|
+
```
|
|
732
|
+
|
|
733
|
+
Ensure the value is a boolean.
|
|
734
|
+
|
|
735
|
+
#### `date`
|
|
736
|
+
|
|
737
|
+
```typescript
|
|
738
|
+
function date(message?: string): Validator<unknown, Date>;
|
|
739
|
+
```
|
|
740
|
+
|
|
741
|
+
Ensure the value is a valid Date object.
|
|
742
|
+
|
|
743
|
+
#### `bigint`
|
|
744
|
+
|
|
745
|
+
```typescript
|
|
746
|
+
function bigint(message?: string): Validator<unknown, bigint>;
|
|
747
|
+
```
|
|
748
|
+
|
|
749
|
+
Ensure the value is a bigint.
|
|
750
|
+
|
|
751
|
+
### Parsers
|
|
752
|
+
|
|
753
|
+
Coerce untrusted input into typed values.
|
|
754
|
+
|
|
755
|
+
#### `parseNumber`
|
|
756
|
+
|
|
757
|
+
```typescript
|
|
758
|
+
function parseNumber(message?: string): Validator<unknown, number>;
|
|
759
|
+
```
|
|
760
|
+
|
|
761
|
+
Parse strings and numbers into numbers. `'25'` becomes `25`.
|
|
762
|
+
|
|
763
|
+
#### `parseString`
|
|
764
|
+
|
|
765
|
+
```typescript
|
|
766
|
+
function parseString(message?: string): Validator<unknown, string>;
|
|
767
|
+
```
|
|
768
|
+
|
|
769
|
+
Coerce non-null/undefined values to strings. `12345` becomes `'12345'`.
|
|
770
|
+
|
|
771
|
+
#### `parseBool`
|
|
772
|
+
|
|
773
|
+
```typescript
|
|
774
|
+
function parseBool(message?: string): Validator<unknown, boolean>;
|
|
775
|
+
```
|
|
776
|
+
|
|
777
|
+
Parse booleans, `0`/`1`, and strings like `'true'`/`'false'`, `'yes'`/`'no'`.
|
|
778
|
+
|
|
779
|
+
#### `parseBigInt`
|
|
780
|
+
|
|
781
|
+
```typescript
|
|
782
|
+
function parseBigInt(message?: string): Validator<unknown, bigint>;
|
|
783
|
+
```
|
|
784
|
+
|
|
785
|
+
Parse BigInt values, strings, and integer numbers into bigints.
|
|
786
|
+
|
|
787
|
+
#### `parseDate`
|
|
788
|
+
|
|
789
|
+
```typescript
|
|
790
|
+
function parseDate(message?: string): Validator<unknown, Date>;
|
|
791
|
+
```
|
|
792
|
+
|
|
793
|
+
Parse Date objects, date strings, and numeric timestamps into Date objects.
|
|
794
|
+
|
|
795
|
+
#### `parseISODate`
|
|
796
|
+
|
|
797
|
+
```typescript
|
|
798
|
+
function parseISODate(message?: string): Validator<unknown, Date>;
|
|
799
|
+
```
|
|
800
|
+
|
|
801
|
+
Parse strict ISO date strings (`YYYY-MM-DD`) into Date objects. Validates calendar correctness.
|
|
802
|
+
|
|
803
|
+
#### `parseJSON`
|
|
804
|
+
|
|
805
|
+
```typescript
|
|
806
|
+
function parseJSON(message?: string): Validator<unknown, unknown>;
|
|
807
|
+
```
|
|
808
|
+
|
|
809
|
+
Parse JSON strings into objects. Already-parsed objects pass through.
|
|
810
|
+
|
|
811
|
+
#### `parseURL`
|
|
812
|
+
|
|
813
|
+
```typescript
|
|
814
|
+
function parseURL(message?: string): Validator<unknown, URL>;
|
|
815
|
+
```
|
|
816
|
+
|
|
817
|
+
Parse string URLs into URL objects.
|
|
818
|
+
|
|
819
|
+
#### `parseEnum`
|
|
820
|
+
|
|
821
|
+
```typescript
|
|
822
|
+
function parseEnum<T extends Record<string, string | number>>(
|
|
823
|
+
enumObject: T,
|
|
824
|
+
message?: string,
|
|
825
|
+
): Validator<unknown, T[keyof T]>;
|
|
826
|
+
```
|
|
827
|
+
|
|
828
|
+
Parse input into TypeScript enum values. Supports case-insensitive key matching.
|
|
829
|
+
|
|
830
|
+
```typescript
|
|
831
|
+
enum Status {
|
|
832
|
+
Pending = 'PENDING',
|
|
833
|
+
Approved = 'APPROVED',
|
|
834
|
+
}
|
|
835
|
+
const validate = parseEnum(Status);
|
|
836
|
+
validate('pending'); // ok("PENDING")
|
|
837
|
+
```
|
|
838
|
+
|
|
839
|
+
### String Constraints
|
|
840
|
+
|
|
841
|
+
#### `minLength`
|
|
842
|
+
|
|
843
|
+
```typescript
|
|
844
|
+
function minLength(min: number, message?: string): Validator<string>;
|
|
845
|
+
```
|
|
846
|
+
|
|
847
|
+
Ensure a string has at least `min` characters.
|
|
848
|
+
|
|
849
|
+
#### `maxLength`
|
|
850
|
+
|
|
851
|
+
```typescript
|
|
852
|
+
function maxLength(max: number, message?: string): Validator<string>;
|
|
853
|
+
```
|
|
854
|
+
|
|
855
|
+
Ensure a string has at most `max` characters.
|
|
856
|
+
|
|
857
|
+
#### `pattern`
|
|
858
|
+
|
|
859
|
+
```typescript
|
|
860
|
+
function pattern(regex: RegExp, message?: string): Validator<string>;
|
|
861
|
+
```
|
|
862
|
+
|
|
863
|
+
Ensure a string matches a regular expression.
|
|
864
|
+
|
|
865
|
+
#### `nonEmpty`
|
|
866
|
+
|
|
867
|
+
```typescript
|
|
868
|
+
function nonEmpty(message?: string): Validator<string>;
|
|
869
|
+
```
|
|
870
|
+
|
|
871
|
+
Ensure a string is not empty after trimming whitespace.
|
|
872
|
+
|
|
873
|
+
#### `email`
|
|
874
|
+
|
|
875
|
+
```typescript
|
|
876
|
+
function email(message?: string): Validator<string>;
|
|
877
|
+
```
|
|
878
|
+
|
|
879
|
+
Ensure a string is a valid email address.
|
|
880
|
+
|
|
881
|
+
#### `phoneNumber`
|
|
882
|
+
|
|
883
|
+
```typescript
|
|
884
|
+
function phoneNumber(message?: string): Validator<string>;
|
|
885
|
+
```
|
|
886
|
+
|
|
887
|
+
Ensure a string is a valid phone number. Accepts international formats with `+`, spaces, dashes, and parentheses.
|
|
888
|
+
|
|
889
|
+
### Number Constraints
|
|
890
|
+
|
|
891
|
+
#### `min`
|
|
892
|
+
|
|
893
|
+
```typescript
|
|
894
|
+
function min(value: number, message?: string): Validator<number>;
|
|
895
|
+
```
|
|
896
|
+
|
|
897
|
+
Ensure a number is at least `value` (inclusive).
|
|
898
|
+
|
|
899
|
+
#### `max`
|
|
900
|
+
|
|
901
|
+
```typescript
|
|
902
|
+
function max(value: number, message?: string): Validator<number>;
|
|
903
|
+
```
|
|
904
|
+
|
|
905
|
+
Ensure a number is at most `value` (inclusive).
|
|
906
|
+
|
|
907
|
+
#### `between`
|
|
908
|
+
|
|
909
|
+
```typescript
|
|
910
|
+
function between(min: number, max: number, message?: string): Validator<number>;
|
|
911
|
+
```
|
|
912
|
+
|
|
913
|
+
Ensure a number is between `min` and `max` (inclusive).
|
|
914
|
+
|
|
915
|
+
#### `integer`
|
|
916
|
+
|
|
917
|
+
```typescript
|
|
918
|
+
function integer(message?: string): Validator<number>;
|
|
919
|
+
```
|
|
920
|
+
|
|
921
|
+
Ensure a number is an integer.
|
|
922
|
+
|
|
923
|
+
#### `positive`
|
|
924
|
+
|
|
925
|
+
```typescript
|
|
926
|
+
function positive(message?: string): Validator<number>;
|
|
927
|
+
```
|
|
928
|
+
|
|
929
|
+
Ensure a number is greater than zero.
|
|
930
|
+
|
|
931
|
+
#### `negative`
|
|
932
|
+
|
|
933
|
+
```typescript
|
|
934
|
+
function negative(message?: string): Validator<number>;
|
|
935
|
+
```
|
|
936
|
+
|
|
937
|
+
Ensure a number is less than zero.
|
|
938
|
+
|
|
939
|
+
#### `nonZero`
|
|
940
|
+
|
|
941
|
+
```typescript
|
|
942
|
+
function nonZero(message?: string): Validator<number>;
|
|
943
|
+
```
|
|
944
|
+
|
|
945
|
+
Ensure a number is not zero.
|
|
946
|
+
|
|
947
|
+
#### `divisibleBy`
|
|
948
|
+
|
|
949
|
+
```typescript
|
|
950
|
+
function divisibleBy(divisor: number, message?: string): Validator<number>;
|
|
951
|
+
```
|
|
952
|
+
|
|
953
|
+
Ensure a number is divisible by `divisor`.
|
|
954
|
+
|
|
955
|
+
#### `precision`
|
|
956
|
+
|
|
957
|
+
```typescript
|
|
958
|
+
function precision(maxDecimalPlaces: number, message?: string): Validator<number>;
|
|
959
|
+
```
|
|
960
|
+
|
|
961
|
+
Ensure a number has at most `maxDecimalPlaces` decimal places.
|
|
962
|
+
|
|
963
|
+
#### `finite`
|
|
964
|
+
|
|
965
|
+
```typescript
|
|
966
|
+
function finite(message?: string): Validator<number>;
|
|
967
|
+
```
|
|
968
|
+
|
|
969
|
+
Ensure a number is finite (not `Infinity` or `-Infinity`).
|
|
970
|
+
|
|
971
|
+
### Date Constraints
|
|
972
|
+
|
|
973
|
+
#### `dateRange`
|
|
974
|
+
|
|
975
|
+
```typescript
|
|
976
|
+
function dateRange(min: Date, max: Date, message?: string): Validator<Date>;
|
|
977
|
+
```
|
|
978
|
+
|
|
979
|
+
Ensure a Date is within a range (inclusive).
|
|
980
|
+
|
|
981
|
+
#### `pastDate`
|
|
982
|
+
|
|
983
|
+
```typescript
|
|
984
|
+
function pastDate(message?: string): Validator<Date>;
|
|
985
|
+
```
|
|
986
|
+
|
|
987
|
+
Ensure a Date is in the past.
|
|
988
|
+
|
|
989
|
+
#### `futureDate`
|
|
990
|
+
|
|
991
|
+
```typescript
|
|
992
|
+
function futureDate(message?: string): Validator<Date>;
|
|
993
|
+
```
|
|
994
|
+
|
|
995
|
+
Ensure a Date is in the future.
|
|
996
|
+
|
|
997
|
+
#### `todayOrFuture`
|
|
998
|
+
|
|
999
|
+
```typescript
|
|
1000
|
+
function todayOrFuture(message?: string): Validator<Date>;
|
|
1001
|
+
```
|
|
1002
|
+
|
|
1003
|
+
Ensure a Date is today or in the future (calendar date comparison).
|
|
1004
|
+
|
|
1005
|
+
### Boolean Constraints
|
|
1006
|
+
|
|
1007
|
+
#### `matches`
|
|
1008
|
+
|
|
1009
|
+
```typescript
|
|
1010
|
+
function matches(expected: boolean, message?: string): Validator<boolean>;
|
|
1011
|
+
```
|
|
1012
|
+
|
|
1013
|
+
Ensure a boolean matches the expected value.
|
|
1014
|
+
|
|
1015
|
+
### Array Constraints
|
|
1016
|
+
|
|
1017
|
+
#### `minItems`
|
|
1018
|
+
|
|
1019
|
+
```typescript
|
|
1020
|
+
function minItems<T>(min: number, message?: string): Validator<T[], T[]>;
|
|
1021
|
+
```
|
|
1022
|
+
|
|
1023
|
+
Ensure an array has at least `min` items.
|
|
1024
|
+
|
|
1025
|
+
#### `maxItems`
|
|
1026
|
+
|
|
1027
|
+
```typescript
|
|
1028
|
+
function maxItems<T>(max: number, message?: string): Validator<T[], T[]>;
|
|
1029
|
+
```
|
|
1030
|
+
|
|
1031
|
+
Ensure an array has at most `max` items.
|
|
1032
|
+
|
|
1033
|
+
#### `notEmpty`
|
|
1034
|
+
|
|
1035
|
+
```typescript
|
|
1036
|
+
function notEmpty<T>(message?: string): Validator<T[], T[]>;
|
|
1037
|
+
```
|
|
1038
|
+
|
|
1039
|
+
Ensure an array is not empty.
|
|
1040
|
+
|
|
1041
|
+
#### `unique`
|
|
1042
|
+
|
|
1043
|
+
```typescript
|
|
1044
|
+
function unique<T>(message?: string, keyExtractor?: (item: T) => unknown): Validator<T[], T[]>;
|
|
1045
|
+
```
|
|
1046
|
+
|
|
1047
|
+
Ensure all array elements are unique. Optional `keyExtractor` for comparing objects.
|
|
1048
|
+
|
|
1049
|
+
```typescript
|
|
1050
|
+
unique<{ id: number }>('Duplicate ID', (item) => item.id);
|
|
1051
|
+
```
|
|
1052
|
+
|
|
1053
|
+
### Enums
|
|
1054
|
+
|
|
1055
|
+
#### `stringEnum`
|
|
1056
|
+
|
|
1057
|
+
```typescript
|
|
1058
|
+
function stringEnum<T extends string>(allowedValues: T[], message?: string): Validator<unknown, T>;
|
|
1059
|
+
```
|
|
1060
|
+
|
|
1061
|
+
Validate that a value is a string and one of the allowed values. Combines `string()` + membership check.
|
|
1062
|
+
|
|
1063
|
+
#### `enumValue`
|
|
1064
|
+
|
|
1065
|
+
```typescript
|
|
1066
|
+
function enumValue<T extends Record<string, string | number>>(
|
|
1067
|
+
enumObject: T,
|
|
1068
|
+
message?: string,
|
|
1069
|
+
excludedValues?: T[keyof T][],
|
|
1070
|
+
): Validator<unknown, T[keyof T]>;
|
|
1071
|
+
```
|
|
1072
|
+
|
|
1073
|
+
Validate against a TypeScript enum. Supports excluding specific values.
|
|
1074
|
+
|
|
1075
|
+
#### `oneOf`
|
|
1076
|
+
|
|
1077
|
+
```typescript
|
|
1078
|
+
function oneOf<T>(allowedValues: T[], message?: string): Validator<T>;
|
|
1079
|
+
```
|
|
1080
|
+
|
|
1081
|
+
Ensure a value is one of the allowed values.
|
|
1082
|
+
|
|
1083
|
+
### Unions
|
|
1084
|
+
|
|
1085
|
+
#### `union`
|
|
1086
|
+
|
|
1087
|
+
```typescript
|
|
1088
|
+
function union<I, O1, O2>(
|
|
1089
|
+
validators: [Validator<I, O1>, Validator<I, O2>],
|
|
1090
|
+
options?: { collectAllErrors?: boolean; errorPrefix?: string },
|
|
1091
|
+
): Validator<I, O1 | O2>;
|
|
1092
|
+
```
|
|
1093
|
+
|
|
1094
|
+
Try validators in order. Returns the first success, or all errors if none match. Supports up to 5 typed variants, plus a generic fallback. Async-aware.
|
|
1095
|
+
|
|
1096
|
+
```typescript
|
|
1097
|
+
const validate = union([string(), number()]);
|
|
1098
|
+
validate('hello'); // ok("hello")
|
|
1099
|
+
validate(42); // ok(42)
|
|
1100
|
+
```
|
|
1101
|
+
|
|
1102
|
+
#### `discriminatedUnion`
|
|
1103
|
+
|
|
1104
|
+
```typescript
|
|
1105
|
+
function discriminatedUnion<M extends Record<string, Validator<unknown, unknown>>>(
|
|
1106
|
+
discriminantField: string,
|
|
1107
|
+
validatorMap: M,
|
|
1108
|
+
fallbackMessage?: string,
|
|
1109
|
+
): Validator<unknown, ValidatorMapOutput<M>>;
|
|
1110
|
+
```
|
|
1111
|
+
|
|
1112
|
+
Select a validator based on a discriminant field's value. More efficient than `union` for tagged objects.
|
|
1113
|
+
|
|
1114
|
+
```typescript
|
|
1115
|
+
const validate = discriminatedUnion('type', {
|
|
1116
|
+
text: object({ type: required(literal('text')), content: required(string()) }),
|
|
1117
|
+
image: object({ type: required(literal('image')), url: required(string()) }),
|
|
1118
|
+
});
|
|
1119
|
+
```
|
|
1120
|
+
|
|
1121
|
+
### Combinators
|
|
1122
|
+
|
|
1123
|
+
#### `chain`
|
|
1124
|
+
|
|
1125
|
+
```typescript
|
|
1126
|
+
function chain<A, B>(v1: Validator<A, B>): Validator<A, B>;
|
|
1127
|
+
function chain<A, B, C>(v1: Validator<A, B>, v2: Validator<B, C>): Validator<A, C>;
|
|
1128
|
+
// ... up to 10 validators
|
|
1129
|
+
```
|
|
1130
|
+
|
|
1131
|
+
Compose validators in sequence. Each output feeds into the next. Short-circuits on first error.
|
|
1132
|
+
|
|
1133
|
+
```typescript
|
|
1134
|
+
const validate = chain(string(), nonEmpty(), minLength(8));
|
|
1135
|
+
validate('password123'); // ok("password123")
|
|
1136
|
+
```
|
|
1137
|
+
|
|
1138
|
+
#### `chainAsync`
|
|
1139
|
+
|
|
1140
|
+
```typescript
|
|
1141
|
+
function chainAsync<A, B>(v1: MaybeAsyncValidator<A, B>): AsyncValidator<A, B>;
|
|
1142
|
+
function chainAsync<A, B, C>(v1: MaybeAsyncValidator<A, B>, v2: MaybeAsyncValidator<B, C>): AsyncValidator<A, C>;
|
|
1143
|
+
// ... up to 10 validators
|
|
1144
|
+
```
|
|
1145
|
+
|
|
1146
|
+
Like `chain`, but accepts async validators. Always returns an `AsyncValidator`.
|
|
1147
|
+
|
|
1148
|
+
```typescript
|
|
1149
|
+
const validate = chainAsync(
|
|
1150
|
+
string(),
|
|
1151
|
+
email(),
|
|
1152
|
+
refineAsync(async (e) => !(await db.users.exists({ email: e })), 'Taken'),
|
|
1153
|
+
);
|
|
1154
|
+
```
|
|
1155
|
+
|
|
1156
|
+
#### `transform`
|
|
1157
|
+
|
|
1158
|
+
```typescript
|
|
1159
|
+
function transform<I, O>(transformer: (value: I) => O): Validator<I, O>;
|
|
1160
|
+
```
|
|
1161
|
+
|
|
1162
|
+
Create a validator that transforms a value during validation.
|
|
1163
|
+
|
|
1164
|
+
```typescript
|
|
1165
|
+
const normalize = chain(
|
|
1166
|
+
string(),
|
|
1167
|
+
email(),
|
|
1168
|
+
transform((s) => s.toLowerCase()),
|
|
1169
|
+
);
|
|
1170
|
+
```
|
|
1171
|
+
|
|
1172
|
+
#### `refine`
|
|
1173
|
+
|
|
1174
|
+
```typescript
|
|
1175
|
+
function refine<T>(predicate: (value: T) => boolean, message?: string): Validator<T, T>;
|
|
1176
|
+
```
|
|
1177
|
+
|
|
1178
|
+
Create a validator with a custom predicate.
|
|
1179
|
+
|
|
1180
|
+
```typescript
|
|
1181
|
+
const isEven = refine<number>((n) => n % 2 === 0, 'Must be even');
|
|
1182
|
+
```
|
|
1183
|
+
|
|
1184
|
+
#### `refineAsync`
|
|
1185
|
+
|
|
1186
|
+
```typescript
|
|
1187
|
+
function refineAsync<T>(predicate: (value: T) => Promise<boolean>, message?: string): AsyncValidator<T, T>;
|
|
1188
|
+
```
|
|
1189
|
+
|
|
1190
|
+
Async version of `refine`. Use for database lookups, API checks, etc.
|
|
1191
|
+
|
|
1192
|
+
### Cross-Field Validation
|
|
1193
|
+
|
|
1194
|
+
#### `refineAt`
|
|
1195
|
+
|
|
1196
|
+
```typescript
|
|
1197
|
+
function refineAt<T>(targetPath: string | string[], predicate: (value: T) => boolean, message: string): Validator<T, T>;
|
|
1198
|
+
```
|
|
1199
|
+
|
|
1200
|
+
Validate across fields, attaching the error to a specific field path. Receives the entire parent object.
|
|
1201
|
+
|
|
1202
|
+
```typescript
|
|
1203
|
+
const validate = chain(
|
|
1204
|
+
object({ password: required(string()), confirm: required(string()) }),
|
|
1205
|
+
refineAt('confirm', (d) => d.password === d.confirm, 'Passwords must match'),
|
|
1206
|
+
);
|
|
1207
|
+
```
|
|
1208
|
+
|
|
1209
|
+
#### `refineAtAsync`
|
|
1210
|
+
|
|
1211
|
+
```typescript
|
|
1212
|
+
function refineAtAsync<T>(
|
|
1213
|
+
targetPath: string | string[],
|
|
1214
|
+
predicate: (value: T) => Promise<boolean>,
|
|
1215
|
+
message: string,
|
|
1216
|
+
): AsyncValidator<T, T>;
|
|
1217
|
+
```
|
|
1218
|
+
|
|
1219
|
+
Async version of `refineAt`.
|
|
1220
|
+
|
|
1221
|
+
```typescript
|
|
1222
|
+
const validate = chainAsync(
|
|
1223
|
+
object({ username: required(string()) }),
|
|
1224
|
+
refineAtAsync('username', async (d) => !(await db.users.exists({ username: d.username })), 'Taken'),
|
|
1225
|
+
);
|
|
1226
|
+
```
|
|
1227
|
+
|
|
1228
|
+
### Validation & Formatting
|
|
1229
|
+
|
|
1230
|
+
#### `validate`
|
|
1231
|
+
|
|
1232
|
+
```typescript
|
|
1233
|
+
function validate<T>(value: unknown, validator: Validator<unknown, T>): Result<T, ValidationError[]>;
|
|
1234
|
+
function validate<T>(
|
|
1235
|
+
value: unknown,
|
|
1236
|
+
validator: MaybeAsyncValidator<unknown, T>,
|
|
1237
|
+
): Result<T, ValidationError[]> | Promise<Result<T, ValidationError[]>>;
|
|
1238
|
+
```
|
|
1239
|
+
|
|
1240
|
+
Run a validator against a value. Returns sync when the validator is sync.
|
|
1241
|
+
|
|
1242
|
+
#### `validateAndFormatResult`
|
|
1243
|
+
|
|
1244
|
+
```typescript
|
|
1245
|
+
function validateAndFormatResult<T>(input: unknown, schema: Validator<unknown, T>): ValidationResult<T>;
|
|
1246
|
+
function validateAndFormatResult<T>(
|
|
1247
|
+
input: unknown,
|
|
1248
|
+
schema: MaybeAsyncValidator<unknown, T>,
|
|
1249
|
+
): ValidationResult<T> | Promise<ValidationResult<T>>;
|
|
1250
|
+
```
|
|
1251
|
+
|
|
1252
|
+
Validate and format in one call. Returns `{ valid: true, data }` or `{ valid: false, errors }`.
|
|
1253
|
+
|
|
1254
|
+
#### `formatErrors`
|
|
1255
|
+
|
|
1256
|
+
```typescript
|
|
1257
|
+
function formatErrors(errors: ValidationError[]): Record<string, string>;
|
|
1258
|
+
```
|
|
1259
|
+
|
|
1260
|
+
Convert validation errors to a flat object. Array paths use bracket notation.
|
|
1261
|
+
|
|
1262
|
+
```typescript
|
|
1263
|
+
formatErrors([
|
|
1264
|
+
{ path: ['name'], message: 'Required' },
|
|
1265
|
+
{ path: ['items', '1'], message: 'Invalid' },
|
|
1266
|
+
]);
|
|
1267
|
+
// { name: 'Required', 'items[1]': 'Invalid' }
|
|
1268
|
+
```
|
|
1269
|
+
|
|
1270
|
+
#### `ROOT_ERROR_KEY`
|
|
1271
|
+
|
|
1272
|
+
```typescript
|
|
1273
|
+
const ROOT_ERROR_KEY = '_root';
|
|
1274
|
+
```
|
|
1275
|
+
|
|
1276
|
+
The key used in formatted errors for root-level validation errors (empty path).
|
|
1277
|
+
|
|
1278
|
+
### Standard Schema
|
|
1279
|
+
|
|
1280
|
+
#### `toStandardSchema`
|
|
1281
|
+
|
|
1282
|
+
```typescript
|
|
1283
|
+
function toStandardSchema<O>(validator: Validator<unknown, O>): StandardSchemaV1<unknown, O>;
|
|
1284
|
+
function toStandardSchema<O>(validator: MaybeAsyncValidator<unknown, O>): StandardSchemaV1<unknown, O>;
|
|
1285
|
+
```
|
|
1286
|
+
|
|
1287
|
+
Wrap a validator as a Standard Schema v1 object for interop with tRPC, TanStack Form, React Hook Form, and other consumers.
|
|
1288
|
+
|
|
1289
|
+
```typescript
|
|
1290
|
+
const standardSchema = toStandardSchema(object({ name: required(string()), email: required(email()) }));
|
|
1291
|
+
```
|
|
1292
|
+
|
|
1293
|
+
#### `StandardSchemaV1`
|
|
1294
|
+
|
|
1295
|
+
```typescript
|
|
1296
|
+
interface StandardSchemaV1<Input = unknown, Output = Input> {
|
|
1297
|
+
readonly '~standard': {
|
|
1298
|
+
readonly version: 1;
|
|
1299
|
+
readonly vendor: string;
|
|
1300
|
+
readonly validate: (value: unknown) => Result<Output> | Promise<Result<Output>>;
|
|
1301
|
+
};
|
|
1302
|
+
}
|
|
1303
|
+
```
|
|
1304
|
+
|
|
1305
|
+
The Standard Schema v1 interface type. Re-exported for type annotations.
|
|
1306
|
+
|
|
1307
|
+
---
|
|
1308
|
+
|
|
1309
|
+
## Composition
|
|
1310
|
+
|
|
1311
|
+
```typescript
|
|
1312
|
+
import { ... } from '@railway-ts/pipelines/composition';
|
|
1313
|
+
```
|
|
1314
|
+
|
|
1315
|
+
Build pipelines. No nested function calls.
|
|
1316
|
+
|
|
1317
|
+
> **Implementation note: `this: void`.** All callback parameters in `pipe`, `flow`, `pipeAsync`, and `flowAsync` are annotated with `this: void`. This ensures composed functions don't depend on `this` context — TypeScript will catch methods that rely on `this` being bound, enforcing referential transparency.
|
|
1318
|
+
|
|
1319
|
+
### Types
|
|
1320
|
+
|
|
1321
|
+
#### `MaybeAsync<T>`
|
|
1322
|
+
|
|
1323
|
+
```typescript
|
|
1324
|
+
type MaybeAsync<T> = T | Promise<T>;
|
|
1325
|
+
```
|
|
1326
|
+
|
|
1327
|
+
A value that may or may not be wrapped in a Promise.
|
|
1328
|
+
|
|
1329
|
+
### Sync
|
|
1330
|
+
|
|
1331
|
+
#### `pipe`
|
|
1332
|
+
|
|
1333
|
+
```typescript
|
|
1334
|
+
function pipe<A>(value: A): A;
|
|
1335
|
+
function pipe<A, B>(value: A, fn1: (a: A) => B): B;
|
|
1336
|
+
function pipe<A, B, C>(value: A, fn1: (a: A) => B, fn2: (b: B) => C): C;
|
|
1337
|
+
// ... up to 20 functions
|
|
1338
|
+
```
|
|
1339
|
+
|
|
1340
|
+
Execute a value through a series of functions. Each output feeds into the next.
|
|
1341
|
+
|
|
1342
|
+
```typescript
|
|
1343
|
+
const result = pipe(
|
|
1344
|
+
5,
|
|
1345
|
+
(x) => x * 2,
|
|
1346
|
+
(x) => x + 1,
|
|
1347
|
+
); // 11
|
|
1348
|
+
```
|
|
1349
|
+
|
|
1350
|
+
#### `flow`
|
|
1351
|
+
|
|
1352
|
+
```typescript
|
|
1353
|
+
function flow<A, B>(fn1: (a: A) => B): (a: A) => B;
|
|
1354
|
+
function flow<A, B, C>(fn1: (a: A) => B, fn2: (b: B) => C): (a: A) => C;
|
|
1355
|
+
// ... up to 20 functions
|
|
1356
|
+
```
|
|
1357
|
+
|
|
1358
|
+
Compose functions left-to-right into a reusable pipeline. Like `pipe` but returns a function.
|
|
1359
|
+
|
|
1360
|
+
```typescript
|
|
1361
|
+
const process = flow(
|
|
1362
|
+
(x: number) => x * 2,
|
|
1363
|
+
(x) => x + 1,
|
|
1364
|
+
);
|
|
1365
|
+
process(5); // 11
|
|
1366
|
+
```
|
|
1367
|
+
|
|
1368
|
+
#### `curry`
|
|
1369
|
+
|
|
1370
|
+
```typescript
|
|
1371
|
+
function curry<A, B, R>(fn: (a: A, b: B) => R): (a: A) => (b: B) => R;
|
|
1372
|
+
function curry<A, B, C, R>(fn: (a: A, b: B, c: C) => R): (a: A) => (b: B) => (c: C) => R;
|
|
1373
|
+
// ... up to 4 parameters
|
|
1374
|
+
```
|
|
1375
|
+
|
|
1376
|
+
Convert a multi-argument function into a chain of single-argument functions.
|
|
1377
|
+
|
|
1378
|
+
#### `uncurry`
|
|
1379
|
+
|
|
1380
|
+
```typescript
|
|
1381
|
+
function uncurry<A, B, R>(fn: (a: A) => (b: B) => R): (a: A, b: B) => R;
|
|
1382
|
+
function uncurry<A, B, C, R>(fn: (a: A) => (b: B) => (c: C) => R): (a: A, b: B, c: C) => R;
|
|
1383
|
+
// ... up to 4 parameters
|
|
1384
|
+
```
|
|
1385
|
+
|
|
1386
|
+
Reverse of `curry`. Convert a curried function back to multi-argument form.
|
|
1387
|
+
|
|
1388
|
+
#### `tupled`
|
|
1389
|
+
|
|
1390
|
+
```typescript
|
|
1391
|
+
function tupled<A extends unknown[], R>(fn: (...args: A) => R): (args: A) => R;
|
|
1392
|
+
```
|
|
1393
|
+
|
|
1394
|
+
Convert a multi-argument function into one that takes a single tuple argument.
|
|
1395
|
+
|
|
1396
|
+
```typescript
|
|
1397
|
+
const add = (a: number, b: number) => a + b;
|
|
1398
|
+
const tupledAdd = tupled(add);
|
|
1399
|
+
tupledAdd([1, 2]); // 3
|
|
1400
|
+
```
|
|
1401
|
+
|
|
1402
|
+
#### `untupled`
|
|
1403
|
+
|
|
1404
|
+
```typescript
|
|
1405
|
+
function untupled<A extends unknown[], R>(fn: (args: A) => R): (...args: A) => R;
|
|
1406
|
+
```
|
|
1407
|
+
|
|
1408
|
+
Reverse of `tupled`. Convert a tuple-argument function back to multi-argument form.
|
|
1409
|
+
|
|
1410
|
+
### Async
|
|
1411
|
+
|
|
1412
|
+
#### `pipeAsync`
|
|
1413
|
+
|
|
1414
|
+
```typescript
|
|
1415
|
+
function pipeAsync<A>(value: MaybeAsync<A>): Promise<A>;
|
|
1416
|
+
function pipeAsync<A, B>(value: MaybeAsync<A>, fn1: (a: A) => MaybeAsync<B>): Promise<B>;
|
|
1417
|
+
// ... up to 20 functions
|
|
1418
|
+
```
|
|
1419
|
+
|
|
1420
|
+
Like `pipe`, but awaits each step. Accepts both sync and async functions.
|
|
1421
|
+
|
|
1422
|
+
```typescript
|
|
1423
|
+
const data = await pipeAsync(userId, fetchUser, validateUser, enrichProfile);
|
|
1424
|
+
```
|
|
1425
|
+
|
|
1426
|
+
#### `flowAsync`
|
|
1427
|
+
|
|
1428
|
+
```typescript
|
|
1429
|
+
function flowAsync<A, B>(fn1: (a: A) => MaybeAsync<B>): (a: A) => Promise<B>;
|
|
1430
|
+
function flowAsync<A, B, C>(fn1: (a: A) => MaybeAsync<B>, fn2: (b: B) => MaybeAsync<C>): (a: A) => Promise<C>;
|
|
1431
|
+
// ... up to 20 functions
|
|
1432
|
+
```
|
|
1433
|
+
|
|
1434
|
+
Like `flow`, but returns an async pipeline. Accepts both sync and async functions.
|
|
1435
|
+
|
|
1436
|
+
```typescript
|
|
1437
|
+
const processOrder = flowAsync(validateOrder, chargePayment, createShipment);
|
|
1438
|
+
await processOrder(orderInput);
|
|
1439
|
+
```
|