validup 0.5.1 → 2.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 +34 -7
- package/dist/index.d.mts +238 -408
- package/dist/index.mjs +534 -476
- package/dist/index.mjs.map +1 -1
- package/package.json +5 -4
package/README.md
CHANGED
|
@@ -166,7 +166,13 @@ When validation fails, the container throws a `ValidupError` containing a list o
|
|
|
166
166
|
type Issue = IssueItem | IssueGroup;
|
|
167
167
|
```
|
|
168
168
|
|
|
169
|
-
This recursive structure preserves the path of failure, so consumers can render rich field-level error messages.
|
|
169
|
+
This recursive structure preserves the path of failure, so consumers can render rich field-level error messages. Every node carries an **absolute** path — a leaf three groups deep still reads `['user', 'contact', 'email']` — so `flattenIssueItems` produces a directly indexable list with no tree walk needed.
|
|
170
|
+
|
|
171
|
+
> **Where the model lives.** `Issue`, `IssueItem`, `IssueGroup`, `IssueCode`, `IssueDataByCode`, the `defineIssue*` factories, the `isIssue*` guards, `flattenIssue*`, `prefixIssuePath`, `formatIssue` and `interpolate` come from [**`@ebec/core`**](https://github.com/tada5hi/ebec) — an extensible error-class library that also owns the issue model.
|
|
172
|
+
>
|
|
173
|
+
> They live outside validup so other libraries can share the shape without taking on validup's runtime. Issue trees then compose across libraries, because both sides reference the same types rather than agreeing by structural coincidence.
|
|
174
|
+
>
|
|
175
|
+
> **Import them from `@ebec/core`** — `npm install @ebec/core --save`. As of v2.0.0, `validup` no longer re-exports the model; the examples below import from `@ebec/core` directly. Every `@validup/*` integration package already depends on it directly.
|
|
170
176
|
|
|
171
177
|
## Builder API (compile-time typing)
|
|
172
178
|
|
|
@@ -223,6 +229,7 @@ interface IBuilder<T extends Record<string, any>, C = unknown> {
|
|
|
223
229
|
oneOf(): IBuilder<T, C>;
|
|
224
230
|
pathsToInclude(...paths: (keyof T & string)[]): IBuilder<T, C>;
|
|
225
231
|
pathsToExclude(...paths: (keyof T & string)[]): IBuilder<T, C>;
|
|
232
|
+
pathsStrict(strict?: boolean): IBuilder<T, C>;
|
|
226
233
|
build(): Container<T, C>;
|
|
227
234
|
}
|
|
228
235
|
```
|
|
@@ -490,6 +497,20 @@ await container.run(input, { pathsToExclude: ['password'] }); // skip the
|
|
|
490
497
|
|
|
491
498
|
The same options can be set at the container level via `new Container({ pathsToInclude: [...] })`. Run-time options take precedence.
|
|
492
499
|
|
|
500
|
+
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:
|
|
501
|
+
|
|
502
|
+
```typescript
|
|
503
|
+
import { isPathsStrictViolation } from 'validup';
|
|
504
|
+
|
|
505
|
+
try {
|
|
506
|
+
await container.run(input, { pathsToInclude: ['clientId'], pathsStrict: true });
|
|
507
|
+
} catch (e) {
|
|
508
|
+
if (isPathsStrictViolation(e)) {
|
|
509
|
+
e.pathsToInclude; // → ['clientId'] (no mount matched)
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
```
|
|
513
|
+
|
|
493
514
|
## Defaults
|
|
494
515
|
|
|
495
516
|
Provide fallback values for keys that end up missing or `undefined` after validation:
|
|
@@ -634,12 +655,14 @@ const out = container.runSync(input); // returns T directly, not Promise<T>
|
|
|
634
655
|
|
|
635
656
|
`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.
|
|
636
657
|
|
|
658
|
+
`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.
|
|
659
|
+
|
|
637
660
|
## Localized Messages
|
|
638
661
|
|
|
639
662
|
`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:
|
|
640
663
|
|
|
641
664
|
```typescript
|
|
642
|
-
import { formatIssue, type IssueMessageTemplates } from '
|
|
665
|
+
import { formatIssue, type IssueMessageTemplates } from '@ebec/core';
|
|
643
666
|
|
|
644
667
|
const de: IssueMessageTemplates = {
|
|
645
668
|
value_invalid: 'Feld {name} ist ungültig',
|
|
@@ -653,11 +676,13 @@ for (const issue of error.issues) {
|
|
|
653
676
|
|
|
654
677
|
`formatIssue(issue, templates?, fallback?)`:
|
|
655
678
|
|
|
656
|
-
1. If `templates[issue.code]` exists, returns `interpolate(template, issue.data)` (placeholders use the `{name}` syntax
|
|
679
|
+
1. If `templates[issue.code]` exists, returns `interpolate(template, issue.data)` (placeholders use the `{name}` syntax — single braces).
|
|
657
680
|
2. Otherwise returns `issue.message`.
|
|
658
681
|
3. Otherwise returns `fallback`.
|
|
659
682
|
|
|
660
|
-
|
|
683
|
+
A catalog may be partial: a code it does not cover falls back to that issue's own `message`, per issue rather than all-or-nothing.
|
|
684
|
+
|
|
685
|
+
Custom validators that want to participate in this flow should pass `data` through `defineIssueItem`/`defineIssueGroup` so consumer-side templates can reference field-specific values. `interpolate` is also re-exported for ad-hoc rendering.
|
|
661
686
|
|
|
662
687
|
## Error Handling
|
|
663
688
|
|
|
@@ -714,7 +739,7 @@ interface IssueGroup {
|
|
|
714
739
|
Use the factories — they set `type` correctly and apply default codes:
|
|
715
740
|
|
|
716
741
|
```typescript
|
|
717
|
-
import { defineIssueItem, defineIssueGroup, IssueCode } from '
|
|
742
|
+
import { defineIssueItem, defineIssueGroup, IssueCode } from '@ebec/core';
|
|
718
743
|
|
|
719
744
|
const item = defineIssueItem({
|
|
720
745
|
path: ['email'],
|
|
@@ -773,7 +798,7 @@ defineIssueItem({ code: 'email_taken', path: ['email'], message: '…' });
|
|
|
773
798
|
If you want a typed const for your own codes (so `AppCode.EMAIL_TAKEN` autocompletes alongside `AppCode.REQUIRED`), define one alongside the shipped vocabulary:
|
|
774
799
|
|
|
775
800
|
```typescript
|
|
776
|
-
import { IssueCode } from '
|
|
801
|
+
import { IssueCode } from '@ebec/core';
|
|
777
802
|
|
|
778
803
|
export const AppCode = {
|
|
779
804
|
...IssueCode,
|
|
@@ -817,6 +842,7 @@ class Container<
|
|
|
817
842
|
| `oneOf` | `ContainerOptions` | Succeed when any mount succeeds |
|
|
818
843
|
| `pathsToInclude` | `ContainerOptions`, `ContainerRunOptions` | Only mount paths in this list are executed |
|
|
819
844
|
| `pathsToExclude` | `ContainerOptions`, `ContainerRunOptions` | Mount paths in this list are skipped |
|
|
845
|
+
| `pathsStrict` | `ContainerOptions`, `ContainerRunOptions` | Throw `PathsStrictViolationError` when an include/exclude path matches no mount |
|
|
820
846
|
| `defaults` | `ContainerRunOptions` | Fallback values for missing/`undefined` keys |
|
|
821
847
|
| `context` | `ContainerRunOptions` | Caller-supplied value surfaced on `ValidatorContext.context` |
|
|
822
848
|
| `signal` | `ContainerRunOptions` | `AbortSignal` — checked between mounts and forwarded to `ctx.signal` |
|
|
@@ -844,6 +870,7 @@ See [Builder API](#builder-api-compile-time-typing). Each `.mount(...)` call ret
|
|
|
844
870
|
|-----------------------|------------------------------------------------------|
|
|
845
871
|
| `defineIssueItem` | Construct an `IssueItem` (sets `type`, default code) |
|
|
846
872
|
| `defineIssueGroup` | Construct an `IssueGroup` (sets `type`) |
|
|
873
|
+
| `prefixIssuePath` | Rebase an issue onto a parent path, recursing into groups. Call it when merging issues from a sub-structure into a larger tree — it is what keeps every node's `path` absolute |
|
|
847
874
|
| `createValidupError` | Build a `ValidupError` carrying one `IssueItem` (sugar for the most common single-issue failure shape). Caller throws. |
|
|
848
875
|
| `IssueCode` | Vocabulary of well-known issue codes (`VALUE_INVALID`, `REQUIRED`, `MIN_LENGTH`, …; full table above) |
|
|
849
876
|
| `GroupKey` | `WILDCARD = '*'` |
|
|
@@ -926,7 +953,7 @@ Use one of the official integration packages to bridge an existing validator lib
|
|
|
926
953
|
|
|
927
954
|
What's covered by semver:
|
|
928
955
|
|
|
929
|
-
- **Public exports** — everything re-exported from `validup`'s entry barrel (`Container`, `defineSchema`, `ValidupError`, `Issue` / factories / guards, `IssueCode`, `
|
|
956
|
+
- **Public exports** — everything re-exported from `validup`'s entry barrel (`Container`, `defineSchema`, `ValidupError`, `isValidupError`, `createValidupError`, `GroupKey`, `OptionalValue`, and the rest of `src/index.ts`). As of v2.0.0, the issue model itself (`Issue` / factories / guards, `IssueCode`, `formatIssue`, `flattenIssue*`, `prefixIssuePath`) is **not** part of this list — it is owned and versioned by [`@ebec/core`](https://github.com/tada5hi/ebec), not re-exported from `validup`.
|
|
930
957
|
- **`Container` runtime contract** — `run` / `runSync` / `runParallel` (via `parallel: true`) / `safeRun` / `safeRunSync`, including their throw-contracts as documented on `IContainer` (`safeRun` throws only on abort).
|
|
931
958
|
- **`Issue` and `ValidupError` shape** — the discriminated union (`type: 'item' | 'group'`), `data`, `meta`, `code` widening to `IssueCode | (string & {})`.
|
|
932
959
|
- **Mount API** — variadic `mount(...)` argument forms listed in [Mounting](#mounting).
|