@railway-ts/use-form 0.1.26 → 0.1.27
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/dist/index.d.ts +4 -3
- package/docs/API.md +47 -11
- package/docs/GETTING_STARTED.md +17 -17
- package/docs/RECIPES.md +23 -7
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -454,13 +454,14 @@ type NativeCheckboxGroupOptionProps = {
|
|
|
454
454
|
* type Zip = FieldTypeAtPath<User, "address.zip">; // number
|
|
455
455
|
*/
|
|
456
456
|
type FieldTypeAtPath<T, P extends string> = P extends `${infer K}.${infer Rest}` ? K extends keyof T ? FieldTypeAtPath<NonNullable<T[K]>, Rest> : unknown : P extends keyof T ? T[P] : unknown;
|
|
457
|
+
type IsTuple<T extends readonly unknown[]> = number extends T['length'] ? false : true;
|
|
458
|
+
type TupleIndexPaths<T extends readonly unknown[]> = `${Extract<keyof T, number>}`;
|
|
457
459
|
type ExtractFieldPaths<T> = T extends readonly unknown[] ? never : T extends object ? {
|
|
458
|
-
[K in keyof T]-?: K extends string | number ? NonNullable<T[K]> extends Date |
|
|
460
|
+
[K in keyof T]-?: K extends string | number ? NonNullable<T[K]> extends Date | ((...args: never[]) => unknown) ? `${K}` : NonNullable<T[K]> extends readonly unknown[] ? IsTuple<NonNullable<T[K]>> extends true ? `${K}` | `${K}.${TupleIndexPaths<NonNullable<T[K]>>}` : `${K}` : T[K] extends object | undefined ? T[K] extends {
|
|
459
461
|
type: string;
|
|
460
462
|
} ? T[K] extends infer U ? U extends {
|
|
461
463
|
type: infer Type;
|
|
462
|
-
} ? Type extends string ?
|
|
463
|
-
`${K}` | `${K}.${ExtractFieldPaths<NonNullable<T[K]>>}` : never : never : never : `${K}` | `${K}.${ExtractFieldPaths<NonNullable<T[K]>>}` : `${K}` : never;
|
|
464
|
+
} ? Type extends string ? `${K}` | `${K}.${ExtractFieldPaths<NonNullable<T[K]>>}` : never : never : never : `${K}` | `${K}.${ExtractFieldPaths<NonNullable<T[K]>>}` : `${K}` : never;
|
|
464
465
|
}[keyof T] : never;
|
|
465
466
|
/**
|
|
466
467
|
* Extracts the array item type from an array field.
|
package/docs/API.md
CHANGED
|
@@ -1326,7 +1326,7 @@ Conditionally apply validators based on a runtime predicate. When the predicate
|
|
|
1326
1326
|
function when<T>(
|
|
1327
1327
|
predicate: (data: T) => boolean,
|
|
1328
1328
|
thenValidator: Validator<T, T>,
|
|
1329
|
-
elseValidator?: Validator<T, T
|
|
1329
|
+
elseValidator?: Validator<T, T>
|
|
1330
1330
|
): Validator<T, T>;
|
|
1331
1331
|
```
|
|
1332
1332
|
|
|
@@ -1341,7 +1341,17 @@ function when<T>(
|
|
|
1341
1341
|
**Example:**
|
|
1342
1342
|
|
|
1343
1343
|
```typescript
|
|
1344
|
-
import {
|
|
1344
|
+
import {
|
|
1345
|
+
chain,
|
|
1346
|
+
object,
|
|
1347
|
+
required,
|
|
1348
|
+
optional,
|
|
1349
|
+
string,
|
|
1350
|
+
stringEnum,
|
|
1351
|
+
refineAt,
|
|
1352
|
+
when,
|
|
1353
|
+
type InferSchemaType,
|
|
1354
|
+
} from '@railway-ts/pipelines/schema';
|
|
1345
1355
|
|
|
1346
1356
|
const baseSchema = object({
|
|
1347
1357
|
accountType: required(stringEnum(['personal', 'business'])),
|
|
@@ -1356,10 +1366,18 @@ const accountValidator = chain(
|
|
|
1356
1366
|
when<AccountData>(
|
|
1357
1367
|
(d) => d.accountType === 'business',
|
|
1358
1368
|
chain(
|
|
1359
|
-
refineAt(
|
|
1360
|
-
|
|
1361
|
-
|
|
1362
|
-
|
|
1369
|
+
refineAt(
|
|
1370
|
+
'companyName',
|
|
1371
|
+
(d) => !!d.companyName,
|
|
1372
|
+
'Company name is required for business accounts'
|
|
1373
|
+
),
|
|
1374
|
+
refineAt(
|
|
1375
|
+
'taxId',
|
|
1376
|
+
(d) => !!d.taxId,
|
|
1377
|
+
'Tax ID is required for business accounts'
|
|
1378
|
+
)
|
|
1379
|
+
)
|
|
1380
|
+
)
|
|
1363
1381
|
);
|
|
1364
1382
|
```
|
|
1365
1383
|
|
|
@@ -1373,7 +1391,7 @@ Async version of `when`. Use when the branch validators involve async operations
|
|
|
1373
1391
|
function whenAsync<T>(
|
|
1374
1392
|
predicate: (data: T) => boolean,
|
|
1375
1393
|
thenValidator: MaybeAsyncValidator<T, T>,
|
|
1376
|
-
elseValidator?: MaybeAsyncValidator<T, T
|
|
1394
|
+
elseValidator?: MaybeAsyncValidator<T, T>
|
|
1377
1395
|
): AsyncValidator<T, T>;
|
|
1378
1396
|
```
|
|
1379
1397
|
|
|
@@ -1388,7 +1406,16 @@ function whenAsync<T>(
|
|
|
1388
1406
|
**Example:**
|
|
1389
1407
|
|
|
1390
1408
|
```typescript
|
|
1391
|
-
import {
|
|
1409
|
+
import {
|
|
1410
|
+
chainAsync,
|
|
1411
|
+
object,
|
|
1412
|
+
required,
|
|
1413
|
+
optional,
|
|
1414
|
+
string,
|
|
1415
|
+
stringEnum,
|
|
1416
|
+
refineAtAsync,
|
|
1417
|
+
whenAsync,
|
|
1418
|
+
} from '@railway-ts/pipelines/schema';
|
|
1392
1419
|
|
|
1393
1420
|
const staffValidator = chainAsync(
|
|
1394
1421
|
object({
|
|
@@ -1397,8 +1424,12 @@ const staffValidator = chainAsync(
|
|
|
1397
1424
|
}),
|
|
1398
1425
|
whenAsync<StaffData>(
|
|
1399
1426
|
(d) => d.role === 'doctor',
|
|
1400
|
-
refineAtAsync(
|
|
1401
|
-
|
|
1427
|
+
refineAtAsync(
|
|
1428
|
+
'license',
|
|
1429
|
+
async (d) => await verifyMedicalLicense(d.license),
|
|
1430
|
+
'Invalid medical license'
|
|
1431
|
+
)
|
|
1432
|
+
)
|
|
1402
1433
|
);
|
|
1403
1434
|
```
|
|
1404
1435
|
|
|
@@ -1492,7 +1523,7 @@ type UserAddresses = User['addresses']; // Address[]
|
|
|
1492
1523
|
|
|
1493
1524
|
### ExtractFieldPaths
|
|
1494
1525
|
|
|
1495
|
-
Extract all valid dot-notation field paths from a type.
|
|
1526
|
+
Extract all valid dot-notation field paths from a type. Dates and variable-length arrays are terminal values. Fixed-length tuples emit indexed sub-paths.
|
|
1496
1527
|
|
|
1497
1528
|
```typescript
|
|
1498
1529
|
import { type ExtractFieldPaths } from '@railway-ts/use-form';
|
|
@@ -1508,6 +1539,11 @@ type User = {
|
|
|
1508
1539
|
|
|
1509
1540
|
type Paths = ExtractFieldPaths<User>;
|
|
1510
1541
|
// "name" | "address" | "address.city" | "address.zip" | "tags"
|
|
1542
|
+
|
|
1543
|
+
// With a tuple field:
|
|
1544
|
+
type Form = { position: [number, number, number]; tags: string[] };
|
|
1545
|
+
type FormPaths = ExtractFieldPaths<Form>;
|
|
1546
|
+
// "position" | "position.0" | "position.1" | "position.2" | "tags"
|
|
1511
1547
|
```
|
|
1512
1548
|
|
|
1513
1549
|
### FieldPath
|
package/docs/GETTING_STARTED.md
CHANGED
|
@@ -400,23 +400,23 @@ Array helpers provide full type safety -- `helpers.getFieldProps(index, 'name')`
|
|
|
400
400
|
|
|
401
401
|
**All array operations:**
|
|
402
402
|
|
|
403
|
-
| Member | What it does
|
|
404
|
-
| ----------------------------------------------- |
|
|
405
|
-
| `values`
|
|
406
|
-
| `push(item)`
|
|
407
|
-
| `remove(index)`
|
|
408
|
-
| `insert(index, item)`
|
|
409
|
-
| `swap(indexA, indexB)`
|
|
410
|
-
| `replace(index, item)`
|
|
411
|
-
| `getFieldProps(index, field)`
|
|
412
|
-
| `getSelectFieldProps(index, field)`
|
|
413
|
-
| `getSliderProps(index, field)`
|
|
414
|
-
| `getCheckboxProps(index, field)`
|
|
415
|
-
| `getSwitchProps(index, field)`
|
|
416
|
-
| `getFileFieldProps(index, field)`
|
|
417
|
-
| `getRadioGroupOptionProps(index, field, value)`
|
|
418
|
-
| `getFieldError(index, field)`
|
|
419
|
-
| `getFieldId(index, field, optionValue?)`
|
|
403
|
+
| Member | What it does |
|
|
404
|
+
| ----------------------------------------------- | -------------------------------------- |
|
|
405
|
+
| `values` | Current array items |
|
|
406
|
+
| `push(item)` | Add to end |
|
|
407
|
+
| `remove(index)` | Remove by index |
|
|
408
|
+
| `insert(index, item)` | Insert at position |
|
|
409
|
+
| `swap(indexA, indexB)` | Swap two items |
|
|
410
|
+
| `replace(index, item)` | Replace item at index |
|
|
411
|
+
| `getFieldProps(index, field)` | Get text/number input props |
|
|
412
|
+
| `getSelectFieldProps(index, field)` | Get select element props |
|
|
413
|
+
| `getSliderProps(index, field)` | Get range input props |
|
|
414
|
+
| `getCheckboxProps(index, field)` | Get checkbox input props |
|
|
415
|
+
| `getSwitchProps(index, field)` | Get switch/toggle props |
|
|
416
|
+
| `getFileFieldProps(index, field)` | Get file input props |
|
|
417
|
+
| `getRadioGroupOptionProps(index, field, value)` | Get radio button props |
|
|
418
|
+
| `getFieldError(index, field)` | Get error for item field |
|
|
419
|
+
| `getFieldId(index, field, optionValue?)` | Get unique ID for labels/accessibility |
|
|
420
420
|
|
|
421
421
|
---
|
|
422
422
|
|
package/docs/RECIPES.md
CHANGED
|
@@ -645,10 +645,18 @@ const accountSchema = chain(
|
|
|
645
645
|
when<AccountData>(
|
|
646
646
|
(d) => d.accountType === 'business',
|
|
647
647
|
chain(
|
|
648
|
-
refineAt(
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
648
|
+
refineAt(
|
|
649
|
+
'companyName',
|
|
650
|
+
(d) => !!d.companyName,
|
|
651
|
+
'Company name is required for business accounts'
|
|
652
|
+
),
|
|
653
|
+
refineAt(
|
|
654
|
+
'taxId',
|
|
655
|
+
(d) => !!d.taxId,
|
|
656
|
+
'Tax ID is required for business accounts'
|
|
657
|
+
)
|
|
658
|
+
)
|
|
659
|
+
)
|
|
652
660
|
);
|
|
653
661
|
|
|
654
662
|
function AccountForm() {
|
|
@@ -691,14 +699,22 @@ function AccountForm() {
|
|
|
691
699
|
Each `refineAt` inside `when` only checks its own field — the `when` wrapper already gates execution to business accounts. For async conditional checks, use `whenAsync` with `chainAsync`:
|
|
692
700
|
|
|
693
701
|
```typescript
|
|
694
|
-
import {
|
|
702
|
+
import {
|
|
703
|
+
chainAsync,
|
|
704
|
+
whenAsync,
|
|
705
|
+
refineAtAsync,
|
|
706
|
+
} from '@railway-ts/pipelines/schema';
|
|
695
707
|
|
|
696
708
|
const staffSchema = chainAsync(
|
|
697
709
|
baseStaffSchema,
|
|
698
710
|
whenAsync<StaffData>(
|
|
699
711
|
(d) => d.role === 'doctor',
|
|
700
|
-
refineAtAsync(
|
|
701
|
-
|
|
712
|
+
refineAtAsync(
|
|
713
|
+
'license',
|
|
714
|
+
async (d) => await verifyMedicalLicense(d.license),
|
|
715
|
+
'Invalid medical license'
|
|
716
|
+
)
|
|
717
|
+
)
|
|
702
718
|
);
|
|
703
719
|
```
|
|
704
720
|
|
package/package.json
CHANGED