@railway-ts/pipelines 0.1.24 → 0.1.26
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/README.md +6 -4
- package/dist/schema/index.cjs +18 -0
- package/dist/schema/index.cjs.map +1 -1
- package/dist/schema/index.d.cts +35 -1
- package/dist/schema/index.d.ts +35 -1
- package/dist/schema/index.mjs +17 -1
- package/dist/schema/index.mjs.map +1 -1
- package/docs/API.md +100 -13
- package/docs/RECIPES.md +30 -1
- package/package.json +1 -1
package/docs/API.md
CHANGED
|
@@ -619,8 +619,14 @@ Extract the union of output types from a validator map. Used by `discriminatedUn
|
|
|
619
619
|
#### `object`
|
|
620
620
|
|
|
621
621
|
```typescript
|
|
622
|
-
function object<T
|
|
623
|
-
|
|
622
|
+
function object<T extends Record<string, unknown>>(
|
|
623
|
+
schema: SyncSchema<T>,
|
|
624
|
+
options?: { strict?: boolean },
|
|
625
|
+
): Validator<unknown, T>;
|
|
626
|
+
function object<T extends Record<string, unknown>>(
|
|
627
|
+
schema: Schema<T>,
|
|
628
|
+
options?: { strict?: boolean },
|
|
629
|
+
): MaybeAsyncValidator<unknown, T>;
|
|
624
630
|
```
|
|
625
631
|
|
|
626
632
|
Validate objects against a schema. Each key maps to a validator. Strict mode (default) rejects extra properties. Fields are validated in parallel when async.
|
|
@@ -645,6 +651,9 @@ Validate arrays where each item is validated by the provided validator.
|
|
|
645
651
|
|
|
646
652
|
```typescript
|
|
647
653
|
function tuple<V extends ReadonlyArray<Validator<unknown, unknown>>>(validators: V): Validator<unknown, TupleType<V>>;
|
|
654
|
+
function tuple<V extends ReadonlyArray<MaybeAsyncValidator<unknown, unknown>>>(
|
|
655
|
+
validators: V,
|
|
656
|
+
): MaybeAsyncValidator<unknown, TupleType<V>>;
|
|
648
657
|
```
|
|
649
658
|
|
|
650
659
|
Validate a heterogeneous tuple where each position has its own validator.
|
|
@@ -657,7 +666,17 @@ validate(['a', 1, true]); // ok(["a", 1, true])
|
|
|
657
666
|
#### `tupleOf`
|
|
658
667
|
|
|
659
668
|
```typescript
|
|
669
|
+
function tupleOf<T>(elementValidator: Validator<unknown, T>, length: 3): Validator<unknown, [T, T, T]>;
|
|
670
|
+
function tupleOf<T>(
|
|
671
|
+
elementValidator: MaybeAsyncValidator<unknown, T>,
|
|
672
|
+
length: 3,
|
|
673
|
+
): MaybeAsyncValidator<unknown, [T, T, T]>;
|
|
674
|
+
// Typed overloads for lengths 1–10 preserve exact tuple types; longer tuples fall back to T[]
|
|
660
675
|
function tupleOf<T>(elementValidator: Validator<unknown, T>, length: number): Validator<unknown, T[]>;
|
|
676
|
+
function tupleOf<T>(
|
|
677
|
+
elementValidator: MaybeAsyncValidator<unknown, T>,
|
|
678
|
+
length: number,
|
|
679
|
+
): MaybeAsyncValidator<unknown, T[]>;
|
|
661
680
|
```
|
|
662
681
|
|
|
663
682
|
Validate a homogeneous tuple with a fixed length. All elements share the same type.
|
|
@@ -672,6 +691,10 @@ const point3D = tupleOf(number(), 3); // [number, number, number]
|
|
|
672
691
|
|
|
673
692
|
```typescript
|
|
674
693
|
function required<I, O>(validator: Validator<I, O>, message?: string): Validator<I | undefined | null, O>;
|
|
694
|
+
function required<I, O>(
|
|
695
|
+
validator: MaybeAsyncValidator<I, O>,
|
|
696
|
+
message?: string,
|
|
697
|
+
): MaybeAsyncValidator<I | undefined | null, O>;
|
|
675
698
|
```
|
|
676
699
|
|
|
677
700
|
Require a value to be defined (not null or undefined).
|
|
@@ -680,6 +703,7 @@ Require a value to be defined (not null or undefined).
|
|
|
680
703
|
|
|
681
704
|
```typescript
|
|
682
705
|
function optional<I, O>(validator: Validator<I, O>): Validator<I | undefined | null, O | undefined>;
|
|
706
|
+
function optional<I, O>(validator: MaybeAsyncValidator<I, O>): MaybeAsyncValidator<I | undefined | null, O | undefined>;
|
|
683
707
|
```
|
|
684
708
|
|
|
685
709
|
Allow null/undefined values. Returns `undefined` for missing values, otherwise applies the validator.
|
|
@@ -696,6 +720,9 @@ Accept only `null` values.
|
|
|
696
720
|
|
|
697
721
|
```typescript
|
|
698
722
|
function emptyAsOptional<I, O>(validator: Validator<I, O>): Validator<I | undefined | null, O | undefined>;
|
|
723
|
+
function emptyAsOptional<I, O>(
|
|
724
|
+
validator: MaybeAsyncValidator<I, O>,
|
|
725
|
+
): MaybeAsyncValidator<I | undefined | null, O | undefined>;
|
|
699
726
|
```
|
|
700
727
|
|
|
701
728
|
Treat empty strings, empty arrays, and empty objects as optional (returns `undefined`).
|
|
@@ -1158,11 +1185,16 @@ validate(42); // ok(42)
|
|
|
1158
1185
|
#### `discriminatedUnion`
|
|
1159
1186
|
|
|
1160
1187
|
```typescript
|
|
1161
|
-
function discriminatedUnion<M extends Record<string, Validator<unknown, unknown>>>(
|
|
1188
|
+
function discriminatedUnion<const M extends Record<string, Validator<unknown, unknown>>>(
|
|
1162
1189
|
discriminantField: string,
|
|
1163
1190
|
validatorMap: M,
|
|
1164
1191
|
fallbackMessage?: string,
|
|
1165
1192
|
): Validator<unknown, ValidatorMapOutput<M>>;
|
|
1193
|
+
function discriminatedUnion<const M extends Record<string, MaybeAsyncValidator<unknown, unknown>>>(
|
|
1194
|
+
discriminantField: string,
|
|
1195
|
+
validatorMap: M,
|
|
1196
|
+
fallbackMessage?: string,
|
|
1197
|
+
): MaybeAsyncValidator<unknown, ValidatorMapOutput<M>>;
|
|
1166
1198
|
```
|
|
1167
1199
|
|
|
1168
1200
|
Select a validator based on a discriminant field's value. More efficient than `union` for tagged objects.
|
|
@@ -1281,6 +1313,57 @@ const validate = chainAsync(
|
|
|
1281
1313
|
);
|
|
1282
1314
|
```
|
|
1283
1315
|
|
|
1316
|
+
### Conditional Validation
|
|
1317
|
+
|
|
1318
|
+
#### `when`
|
|
1319
|
+
|
|
1320
|
+
```typescript
|
|
1321
|
+
function when<T>(
|
|
1322
|
+
predicate: (data: T) => boolean,
|
|
1323
|
+
thenValidator: Validator<T, T>,
|
|
1324
|
+
elseValidator?: Validator<T, T>,
|
|
1325
|
+
): Validator<T, T>;
|
|
1326
|
+
```
|
|
1327
|
+
|
|
1328
|
+
Apply different validators based on a runtime predicate. When the predicate returns `true`, `thenValidator` runs. When `false`, `elseValidator` runs (if provided), otherwise the value passes through unchanged.
|
|
1329
|
+
|
|
1330
|
+
```typescript
|
|
1331
|
+
const validate = chain(
|
|
1332
|
+
object({
|
|
1333
|
+
contactMethod: required(stringEnum(['email', 'phone'] as const)),
|
|
1334
|
+
email: required(string()),
|
|
1335
|
+
phone: required(string()),
|
|
1336
|
+
}),
|
|
1337
|
+
when<FormData>(
|
|
1338
|
+
(d) => d.contactMethod === 'email',
|
|
1339
|
+
refineAt('email', (d) => d.email.includes('@'), 'Valid email required'),
|
|
1340
|
+
refineAt('phone', (d) => d.phone.length >= 10, 'Phone must be at least 10 digits'),
|
|
1341
|
+
),
|
|
1342
|
+
);
|
|
1343
|
+
```
|
|
1344
|
+
|
|
1345
|
+
#### `whenAsync`
|
|
1346
|
+
|
|
1347
|
+
```typescript
|
|
1348
|
+
function whenAsync<T>(
|
|
1349
|
+
predicate: (data: T) => boolean,
|
|
1350
|
+
thenValidator: MaybeAsyncValidator<T, T>,
|
|
1351
|
+
elseValidator?: MaybeAsyncValidator<T, T>,
|
|
1352
|
+
): AsyncValidator<T, T>;
|
|
1353
|
+
```
|
|
1354
|
+
|
|
1355
|
+
Async version of `when`. Use when the branch validators involve async operations (database checks, API calls, etc.).
|
|
1356
|
+
|
|
1357
|
+
```typescript
|
|
1358
|
+
const validate = chainAsync(
|
|
1359
|
+
object({ role: required(string()), license: required(string()) }),
|
|
1360
|
+
whenAsync<StaffForm>(
|
|
1361
|
+
(d) => d.role === 'doctor',
|
|
1362
|
+
refineAtAsync('license', async (d) => await verifyMedicalLicense(d.license), 'Invalid license'),
|
|
1363
|
+
),
|
|
1364
|
+
);
|
|
1365
|
+
```
|
|
1366
|
+
|
|
1284
1367
|
### Validation & Formatting
|
|
1285
1368
|
|
|
1286
1369
|
#### `validate`
|
|
@@ -1422,10 +1505,9 @@ A value that may or may not be wrapped in a Promise.
|
|
|
1422
1505
|
#### `pipe`
|
|
1423
1506
|
|
|
1424
1507
|
```typescript
|
|
1425
|
-
function pipe<A>(value: A): A;
|
|
1426
1508
|
function pipe<A, B>(value: A, fn1: (a: A) => B): B;
|
|
1427
1509
|
function pipe<A, B, C>(value: A, fn1: (a: A) => B, fn2: (b: B) => C): C;
|
|
1428
|
-
// ... up to
|
|
1510
|
+
// ... up to 9 functions
|
|
1429
1511
|
```
|
|
1430
1512
|
|
|
1431
1513
|
Execute a value through a series of functions. Each output feeds into the next.
|
|
@@ -1443,7 +1525,7 @@ const result = pipe(
|
|
|
1443
1525
|
```typescript
|
|
1444
1526
|
function flow<A, B>(fn1: (a: A) => B): (a: A) => B;
|
|
1445
1527
|
function flow<A, B, C>(fn1: (a: A) => B, fn2: (b: B) => C): (a: A) => C;
|
|
1446
|
-
// ... up to
|
|
1528
|
+
// ... up to 10 functions
|
|
1447
1529
|
```
|
|
1448
1530
|
|
|
1449
1531
|
Compose functions left-to-right into a reusable pipeline. Like `pipe` but returns a function.
|
|
@@ -1461,7 +1543,7 @@ process(5); // 11
|
|
|
1461
1543
|
```typescript
|
|
1462
1544
|
function curry<A, B, R>(fn: (a: A, b: B) => R): (a: A) => (b: B) => R;
|
|
1463
1545
|
function curry<A, B, C, R>(fn: (a: A, b: B, c: C) => R): (a: A) => (b: B) => (c: C) => R;
|
|
1464
|
-
// ... up to
|
|
1546
|
+
// ... up to 5 parameters
|
|
1465
1547
|
```
|
|
1466
1548
|
|
|
1467
1549
|
Convert a multi-argument function into a chain of single-argument functions.
|
|
@@ -1471,7 +1553,7 @@ Convert a multi-argument function into a chain of single-argument functions.
|
|
|
1471
1553
|
```typescript
|
|
1472
1554
|
function uncurry<A, B, R>(fn: (a: A) => (b: B) => R): (a: A, b: B) => R;
|
|
1473
1555
|
function uncurry<A, B, C, R>(fn: (a: A) => (b: B) => (c: C) => R): (a: A, b: B, c: C) => R;
|
|
1474
|
-
// ... up to
|
|
1556
|
+
// ... up to 5 parameters
|
|
1475
1557
|
```
|
|
1476
1558
|
|
|
1477
1559
|
Reverse of `curry`. Convert a curried function back to multi-argument form.
|
|
@@ -1479,7 +1561,10 @@ Reverse of `curry`. Convert a curried function back to multi-argument form.
|
|
|
1479
1561
|
#### `tupled`
|
|
1480
1562
|
|
|
1481
1563
|
```typescript
|
|
1482
|
-
function tupled<A
|
|
1564
|
+
function tupled<A, B, R>(fn: (a: A, b: B) => R): (args: [A, B]) => R;
|
|
1565
|
+
function tupled<A, B, C, R>(fn: (a: A, b: B, c: C) => R): (args: [A, B, C]) => R;
|
|
1566
|
+
function tupled<A, B, C, D, R>(fn: (a: A, b: B, c: C, d: D) => R): (args: [A, B, C, D]) => R;
|
|
1567
|
+
function tupled<A, B, C, D, E, R>(fn: (a: A, b: B, c: C, d: D, e: E) => R): (args: [A, B, C, D, E]) => R;
|
|
1483
1568
|
```
|
|
1484
1569
|
|
|
1485
1570
|
Convert a multi-argument function into one that takes a single tuple argument.
|
|
@@ -1493,7 +1578,10 @@ tupledAdd([1, 2]); // 3
|
|
|
1493
1578
|
#### `untupled`
|
|
1494
1579
|
|
|
1495
1580
|
```typescript
|
|
1496
|
-
function untupled<A
|
|
1581
|
+
function untupled<A, B, R>(fn: (args: [A, B]) => R): (a: A, b: B) => R;
|
|
1582
|
+
function untupled<A, B, C, R>(fn: (args: [A, B, C]) => R): (a: A, b: B, c: C) => R;
|
|
1583
|
+
function untupled<A, B, C, D, R>(fn: (args: [A, B, C, D]) => R): (a: A, b: B, c: C, d: D) => R;
|
|
1584
|
+
function untupled<A, B, C, D, E, R>(fn: (args: [A, B, C, D, E]) => R): (a: A, b: B, c: C, d: D, e: E) => R;
|
|
1497
1585
|
```
|
|
1498
1586
|
|
|
1499
1587
|
Reverse of `tupled`. Convert a tuple-argument function back to multi-argument form.
|
|
@@ -1503,9 +1591,8 @@ Reverse of `tupled`. Convert a tuple-argument function back to multi-argument fo
|
|
|
1503
1591
|
#### `pipeAsync`
|
|
1504
1592
|
|
|
1505
1593
|
```typescript
|
|
1506
|
-
function pipeAsync<A>(value: MaybeAsync<A>): Promise<A>;
|
|
1507
1594
|
function pipeAsync<A, B>(value: MaybeAsync<A>, fn1: (a: A) => MaybeAsync<B>): Promise<B>;
|
|
1508
|
-
// ... up to
|
|
1595
|
+
// ... up to 9 functions
|
|
1509
1596
|
```
|
|
1510
1597
|
|
|
1511
1598
|
Like `pipe`, but awaits each step. Accepts both sync and async functions.
|
|
@@ -1519,7 +1606,7 @@ const data = await pipeAsync(userId, fetchUser, validateUser, enrichProfile);
|
|
|
1519
1606
|
```typescript
|
|
1520
1607
|
function flowAsync<A, B>(fn1: (a: A) => MaybeAsync<B>): (a: A) => Promise<B>;
|
|
1521
1608
|
function flowAsync<A, B, C>(fn1: (a: A) => MaybeAsync<B>, fn2: (b: B) => MaybeAsync<C>): (a: A) => Promise<C>;
|
|
1522
|
-
// ... up to
|
|
1609
|
+
// ... up to 10 functions
|
|
1523
1610
|
```
|
|
1524
1611
|
|
|
1525
1612
|
Like `flow`, but returns an async pipeline. Accepts both sync and async functions.
|
package/docs/RECIPES.md
CHANGED
|
@@ -314,7 +314,36 @@ is({ name: 'Alice', email: 'nope' }, userSchema); // false
|
|
|
314
314
|
|
|
315
315
|
### Conditional Validation
|
|
316
316
|
|
|
317
|
+
Use `when` to apply different validators based on runtime data. The value passes through unchanged when the predicate is false and no `elseValidator` is provided:
|
|
318
|
+
|
|
317
319
|
```typescript
|
|
320
|
+
import { chain, object, required, string, stringEnum, when, refineAt } from '@railway-ts/pipelines/schema';
|
|
321
|
+
|
|
322
|
+
type ContactForm = {
|
|
323
|
+
contactMethod: 'email' | 'phone';
|
|
324
|
+
email: string;
|
|
325
|
+
phone: string;
|
|
326
|
+
};
|
|
327
|
+
|
|
328
|
+
const contactSchema = chain(
|
|
329
|
+
object({
|
|
330
|
+
contactMethod: required(stringEnum(['email', 'phone'] as const)),
|
|
331
|
+
email: required(string()),
|
|
332
|
+
phone: required(string()),
|
|
333
|
+
}),
|
|
334
|
+
when<ContactForm>(
|
|
335
|
+
(d) => d.contactMethod === 'email',
|
|
336
|
+
refineAt('email', (d) => d.email.includes('@'), 'Valid email required'),
|
|
337
|
+
refineAt('phone', (d) => d.phone.length >= 10, 'Phone must be at least 10 digits'),
|
|
338
|
+
),
|
|
339
|
+
);
|
|
340
|
+
```
|
|
341
|
+
|
|
342
|
+
For more complex branching (e.g. completely different schemas per variant), use `flatMap` directly:
|
|
343
|
+
|
|
344
|
+
```typescript
|
|
345
|
+
import { flatMap, ok } from '@railway-ts/pipelines/result';
|
|
346
|
+
|
|
318
347
|
const validateConditionally = (input: unknown) => {
|
|
319
348
|
const baseResult = validate(input, baseSchema);
|
|
320
349
|
|
|
@@ -536,7 +565,7 @@ validate('active', statusValidator); // Ok(Status.Active)
|
|
|
536
565
|
import { oneOf } from '@railway-ts/pipelines/schema';
|
|
537
566
|
|
|
538
567
|
const priorityValidator = oneOf([1, 2, 3] as const);
|
|
539
|
-
// Validator<
|
|
568
|
+
// Validator<1 | 2 | 3>
|
|
540
569
|
|
|
541
570
|
validate(2, priorityValidator); // Ok(2)
|
|
542
571
|
validate(5, priorityValidator); // Err(...)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@railway-ts/pipelines",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.26",
|
|
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": {
|