validup 0.5.0 → 1.0.0
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 +58 -12
- package/dist/index.d.mts +410 -47
- package/dist/index.mjs +571 -266
- package/dist/index.mjs.map +1 -1
- package/package.json +5 -4
package/README.md
CHANGED
|
@@ -223,6 +223,7 @@ interface IBuilder<T extends Record<string, any>, C = unknown> {
|
|
|
223
223
|
oneOf(): IBuilder<T, C>;
|
|
224
224
|
pathsToInclude(...paths: (keyof T & string)[]): IBuilder<T, C>;
|
|
225
225
|
pathsToExclude(...paths: (keyof T & string)[]): IBuilder<T, C>;
|
|
226
|
+
pathsStrict(strict?: boolean): IBuilder<T, C>;
|
|
226
227
|
build(): Container<T, C>;
|
|
227
228
|
}
|
|
228
229
|
```
|
|
@@ -381,25 +382,32 @@ The wildcard token `'*'` (also exported as `GroupKey.WILDCARD`) bypasses the fil
|
|
|
381
382
|
|
|
382
383
|
## Optional Values
|
|
383
384
|
|
|
384
|
-
Mark a mount as `optional` to skip it when the input value is "missing". You decide what counts as missing via `optionalValue
|
|
385
|
+
Mark a mount as `optional` to skip it when the input value is "missing". You decide what counts as missing via `optionalValue`. The vocabulary is atomic — each entry matches exactly one runtime value, with `'falsy'` as the composite shortcut:
|
|
385
386
|
|
|
386
387
|
| `optionalValue` | Treats as optional |
|
|
387
388
|
|---------------------------|-----------------------------------------------|
|
|
388
389
|
| `'undefined'` (default) | only `undefined` |
|
|
389
|
-
| `'null'` | `null`
|
|
390
|
-
| `'
|
|
390
|
+
| `'null'` | only `null` (NOT `undefined`) |
|
|
391
|
+
| `'empty_string'` | only `''` |
|
|
392
|
+
| `'zero'` | only `0` |
|
|
393
|
+
| `'false'` | only `false` |
|
|
394
|
+
| `'nan'` | only `NaN` |
|
|
395
|
+
| `'falsy'` | any falsy value (`undefined`, `null`, `''`, `0`, `false`, `NaN`) |
|
|
396
|
+
|
|
397
|
+
Pass an array to skip on any of several atoms:
|
|
391
398
|
|
|
392
399
|
```typescript
|
|
393
400
|
import { OptionalValue } from 'validup';
|
|
394
401
|
|
|
395
|
-
container.mount('
|
|
396
|
-
{ optional: true, optionalValue: OptionalValue.NULL },
|
|
397
|
-
|
|
402
|
+
container.mount('name',
|
|
403
|
+
{ optional: true, optionalValue: [OptionalValue.UNDEFINED, OptionalValue.NULL, OptionalValue.EMPTY_STRING] },
|
|
404
|
+
isString,
|
|
398
405
|
);
|
|
399
406
|
|
|
400
|
-
await container.run({
|
|
401
|
-
await container.run({});
|
|
402
|
-
await container.run({
|
|
407
|
+
await container.run({ name: undefined }); // ✅ skipped
|
|
408
|
+
await container.run({ name: null }); // ✅ skipped
|
|
409
|
+
await container.run({ name: '' }); // ✅ skipped
|
|
410
|
+
await container.run({ name: 'Peter' }); // ✅ validated
|
|
403
411
|
```
|
|
404
412
|
|
|
405
413
|
Set `optionalInclude: true` to copy the optional value through into the output instead of dropping it:
|
|
@@ -413,7 +421,27 @@ container.mount('nickname',
|
|
|
413
421
|
await container.run({}); // → { nickname: undefined }
|
|
414
422
|
```
|
|
415
423
|
|
|
416
|
-
|
|
424
|
+
Set `optionalAs: <value>` to collapse every optional sentinel to one canonical value — useful when the consumer expects a single shape (e.g. `null` for "no value provided" while the form input holds `''`):
|
|
425
|
+
|
|
426
|
+
```typescript
|
|
427
|
+
container.mount('description',
|
|
428
|
+
{
|
|
429
|
+
optional: true,
|
|
430
|
+
optionalValue: ['undefined', 'null', 'empty_string'],
|
|
431
|
+
optionalAs: null,
|
|
432
|
+
},
|
|
433
|
+
isString,
|
|
434
|
+
);
|
|
435
|
+
|
|
436
|
+
await container.run({ description: '' }); // → { description: null }
|
|
437
|
+
await container.run({ description: undefined }); // → { description: null }
|
|
438
|
+
await container.run({ description: null }); // → { description: null }
|
|
439
|
+
await container.run({ description: 'real' }); // → { description: 'real' }
|
|
440
|
+
```
|
|
441
|
+
|
|
442
|
+
`optionalAs` wins over `optionalInclude` when paired; presence (not value) matters, so `{ optionalAs: undefined }` deliberately emits the key as `undefined`.
|
|
443
|
+
|
|
444
|
+
For cases the `optionalValue` vocabulary can't express (e.g. context-dependent), pass `optional` as a predicate. Custom predicates win when present and `optionalValue` is ignored:
|
|
417
445
|
|
|
418
446
|
```typescript
|
|
419
447
|
container.mount('name',
|
|
@@ -463,6 +491,20 @@ await container.run(input, { pathsToExclude: ['password'] }); // skip the
|
|
|
463
491
|
|
|
464
492
|
The same options can be set at the container level via `new Container({ pathsToInclude: [...] })`. Run-time options take precedence.
|
|
465
493
|
|
|
494
|
+
By default a filter path that matches no mount is silently ignored. Add `pathsStrict: true` to **fail loud** instead — the run throws a structural `PathsStrictViolationError` (caught with `isPathsStrictViolation`, re-thrown by `safeRun`/`safeRunSync`) listing the unmatched paths, so a renamed mount key can't make a scoped validation silently disappear:
|
|
495
|
+
|
|
496
|
+
```typescript
|
|
497
|
+
import { isPathsStrictViolation } from 'validup';
|
|
498
|
+
|
|
499
|
+
try {
|
|
500
|
+
await container.run(input, { pathsToInclude: ['clientId'], pathsStrict: true });
|
|
501
|
+
} catch (e) {
|
|
502
|
+
if (isPathsStrictViolation(e)) {
|
|
503
|
+
e.pathsToInclude; // → ['clientId'] (no mount matched)
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
```
|
|
507
|
+
|
|
466
508
|
## Defaults
|
|
467
509
|
|
|
468
510
|
Provide fallback values for keys that end up missing or `undefined` after validation:
|
|
@@ -607,6 +649,8 @@ const out = container.runSync(input); // returns T directly, not Promise<T>
|
|
|
607
649
|
|
|
608
650
|
`runSync()` throws synchronously if a validator returns a Promise or if a nested container does not implement `runSync` — those are *structural* failures (not validation outcomes), so they're surfaced verbatim and never wrapped into `Result.failure` by `safeRunSync`. Use the async `run()` for any graph that mixes sync and async validators.
|
|
609
651
|
|
|
652
|
+
`run()` and `runSync()` are **not two implementations**. Both drive one shared mount-resolution body, so every mount option (groups, `optional` / `optionalValue` / `optionalAs`, path filters, `defaults`, `pathsStrict`, `cache`) resolves identically in both — a sync run can't silently drift from its async twin. The only differences are the two structural probes above.
|
|
653
|
+
|
|
610
654
|
## Localized Messages
|
|
611
655
|
|
|
612
656
|
`Issue.message` is rendered eagerly in English at construction time. For i18n / custom locales, every issue also carries a structured `data?: Record<string, unknown>` field (populated by the runtime where the message references a non-trivial value — e.g. `{ name: 'email' }` on the wrapping group at a failing mount). Pair it with `formatIssue` and a `code → template` map to render at the consumer side:
|
|
@@ -790,6 +834,7 @@ class Container<
|
|
|
790
834
|
| `oneOf` | `ContainerOptions` | Succeed when any mount succeeds |
|
|
791
835
|
| `pathsToInclude` | `ContainerOptions`, `ContainerRunOptions` | Only mount paths in this list are executed |
|
|
792
836
|
| `pathsToExclude` | `ContainerOptions`, `ContainerRunOptions` | Mount paths in this list are skipped |
|
|
837
|
+
| `pathsStrict` | `ContainerOptions`, `ContainerRunOptions` | Throw `PathsStrictViolationError` when an include/exclude path matches no mount |
|
|
793
838
|
| `defaults` | `ContainerRunOptions` | Fallback values for missing/`undefined` keys |
|
|
794
839
|
| `context` | `ContainerRunOptions` | Caller-supplied value surfaced on `ValidatorContext.context` |
|
|
795
840
|
| `signal` | `ContainerRunOptions` | `AbortSignal` — checked between mounts and forwarded to `ctx.signal` |
|
|
@@ -799,8 +844,9 @@ class Container<
|
|
|
799
844
|
| `path` | `ContainerRunOptions` | Used internally when nesting; rarely set by hand |
|
|
800
845
|
| `group` (mount) | `MountOptions` | Group(s) this mount belongs to (`string \| string[]`) |
|
|
801
846
|
| `optional` | `MountOptions` | Skip this mount when value is "optional" |
|
|
802
|
-
| `optionalValue` | `MountOptions`
|
|
847
|
+
| `optionalValue` | `MountOptions`, `ContainerRunOptions`, `ContainerOptions` | What counts as optional. Precedence: mount > run > container > core (`'undefined'`). Single atom or array of atoms. |
|
|
803
848
|
| `optionalInclude` | `MountOptions` | Copy optional value into the output instead of dropping it |
|
|
849
|
+
| `optionalAs` | `MountOptions`, `ContainerRunOptions`, `ContainerOptions` | Canonical value written to the output when the mount is optional. Implies include; wins over `optionalInclude`. Precedence: mount > run > container. Presence-not-value activation (so `{ optionalAs: undefined }` is a meaningful directive). |
|
|
804
850
|
|
|
805
851
|
### defineSchema
|
|
806
852
|
|
|
@@ -819,7 +865,7 @@ See [Builder API](#builder-api-compile-time-typing). Each `.mount(...)` call ret
|
|
|
819
865
|
| `createValidupError` | Build a `ValidupError` carrying one `IssueItem` (sugar for the most common single-issue failure shape). Caller throws. |
|
|
820
866
|
| `IssueCode` | Vocabulary of well-known issue codes (`VALUE_INVALID`, `REQUIRED`, `MIN_LENGTH`, …; full table above) |
|
|
821
867
|
| `GroupKey` | `WILDCARD = '*'` |
|
|
822
|
-
| `OptionalValue` | `UNDEFINED` / `NULL` / `FALSY`
|
|
868
|
+
| `OptionalValue` | `UNDEFINED` (default) / `NULL` / `EMPTY_STRING` / `ZERO` / `FALSE` / `NAN` / `FALSY` (each atom matches exactly one value; `FALSY` is the composite; arrays compose multiple atoms) |
|
|
823
869
|
|
|
824
870
|
### Validator Composition
|
|
825
871
|
|