validup 0.2.2 → 0.3.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/LICENSE +201 -21
- package/README.md +668 -113
- package/dist/index.d.mts +614 -0
- package/dist/index.mjs +766 -438
- package/dist/index.mjs.map +1 -1
- package/package.json +16 -18
- package/dist/constants.d.ts +0 -17
- package/dist/container/check.d.ts +0 -2
- package/dist/container/index.d.ts +0 -3
- package/dist/container/module.d.ts +0 -23
- package/dist/container/types.d.ts +0 -120
- package/dist/error/base.d.ts +0 -5
- package/dist/error/check.d.ts +0 -5
- package/dist/error/index.d.ts +0 -2
- package/dist/helpers/error.d.ts +0 -2
- package/dist/helpers/index.d.ts +0 -3
- package/dist/helpers/optional-value.d.ts +0 -8
- package/dist/helpers/path-stringify.d.ts +0 -6
- package/dist/index.cjs +0 -494
- package/dist/index.cjs.map +0 -1
- package/dist/index.d.ts +0 -7
- package/dist/issue/check.d.ts +0 -4
- package/dist/issue/constants.d.ts +0 -4
- package/dist/issue/define.d.ts +0 -5
- package/dist/issue/index.d.ts +0 -4
- package/dist/issue/types.d.ts +0 -47
- package/dist/types.d.ts +0 -24
- package/dist/utils/has-own-property.d.ts +0 -1
- package/dist/utils/index.d.ts +0 -2
- package/dist/utils/object.d.ts +0 -1
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,614 @@
|
|
|
1
|
+
import { Path } from "pathtrace";
|
|
2
|
+
import { BaseError, interpolate } from "@ebec/core";
|
|
3
|
+
|
|
4
|
+
//#region src/helpers/defaults.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* Slice a parent `defaults` map down to entries that target paths inside a
|
|
7
|
+
* single nested container mount, with the mount-key prefix stripped so the
|
|
8
|
+
* child sees them as local keys. Exact-key matches (the default targets the
|
|
9
|
+
* mount root itself) are skipped — the parent's post-loop fill handles those
|
|
10
|
+
* once the child has returned, since the child is run with `flat: true` and
|
|
11
|
+
* cannot address its own root.
|
|
12
|
+
*/
|
|
13
|
+
declare function resolveDefaults(defaults: Record<string, any> | undefined, key: string): Record<string, any> | undefined;
|
|
14
|
+
//#endregion
|
|
15
|
+
//#region src/helpers/error.d.ts
|
|
16
|
+
declare function buildErrorMessageForAttribute(key: PropertyKey): string;
|
|
17
|
+
declare function buildErrorMessageForAttributes(input: PropertyKey[] | Record<string, any>): string;
|
|
18
|
+
//#endregion
|
|
19
|
+
//#region src/constants.d.ts
|
|
20
|
+
declare enum GroupKey {
|
|
21
|
+
WILDCARD = "*"
|
|
22
|
+
}
|
|
23
|
+
declare enum OptionalValue {
|
|
24
|
+
/**
|
|
25
|
+
* value: undefined
|
|
26
|
+
*/
|
|
27
|
+
UNDEFINED = "undefined",
|
|
28
|
+
/**
|
|
29
|
+
* value: null & undefined
|
|
30
|
+
*/
|
|
31
|
+
NULL = "null",
|
|
32
|
+
/**
|
|
33
|
+
* value: empty string, false, 0
|
|
34
|
+
*/
|
|
35
|
+
FALSY = "falsy"
|
|
36
|
+
}
|
|
37
|
+
//#endregion
|
|
38
|
+
//#region src/helpers/optional-value.d.ts
|
|
39
|
+
/**
|
|
40
|
+
* Check if input value (type of) satisfies optional value type constraint.
|
|
41
|
+
*
|
|
42
|
+
* @param value
|
|
43
|
+
* @param optionalValue
|
|
44
|
+
*/
|
|
45
|
+
declare function isOptionalValue(value: unknown, optionalValue?: `${OptionalValue}`): boolean;
|
|
46
|
+
//#endregion
|
|
47
|
+
//#region src/helpers/path-filter.d.ts
|
|
48
|
+
type PathFilterResolution = {
|
|
49
|
+
skip: boolean;
|
|
50
|
+
pathsToInclude: string[] | undefined;
|
|
51
|
+
pathsToExclude: string[] | undefined;
|
|
52
|
+
};
|
|
53
|
+
/**
|
|
54
|
+
* Resolve `pathsToInclude` / `pathsToExclude` against a single mounted item's
|
|
55
|
+
* (already-expanded) local `key`. Returns whether to skip the item, plus the
|
|
56
|
+
* filter sub-lists to forward into a child container — with the parent prefix
|
|
57
|
+
* stripped so the child can match purely against its own local keys.
|
|
58
|
+
*
|
|
59
|
+
* Semantics:
|
|
60
|
+
* - Un-keyed container mount (`key === ''`) shares the parent's namespace, so
|
|
61
|
+
* filters are forwarded verbatim.
|
|
62
|
+
* - Exact match in `pathsToInclude` / `pathsToExclude` matches the whole mount.
|
|
63
|
+
* - Prefix match (`<key>.…`) only descends into container mounts; leaf
|
|
64
|
+
* validators with deeper-target filters fall through (skipped for include,
|
|
65
|
+
* kept for exclude).
|
|
66
|
+
*/
|
|
67
|
+
declare function resolvePathFilter(pathsToInclude: string[] | undefined, pathsToExclude: string[] | undefined, key: string, isContainer: boolean): PathFilterResolution;
|
|
68
|
+
//#endregion
|
|
69
|
+
//#region src/helpers/path-stringify.d.ts
|
|
70
|
+
/**
|
|
71
|
+
* Stringify path array to string.
|
|
72
|
+
*
|
|
73
|
+
* @param input
|
|
74
|
+
*/
|
|
75
|
+
declare function stringifyPath(input: PropertyKey[]): string;
|
|
76
|
+
//#endregion
|
|
77
|
+
//#region src/issue/constants.d.ts
|
|
78
|
+
/**
|
|
79
|
+
* Registry of well-known issue codes. Third-party packages can extend the
|
|
80
|
+
* union via TypeScript declaration merging:
|
|
81
|
+
*
|
|
82
|
+
* ```ts
|
|
83
|
+
* declare module 'validup' {
|
|
84
|
+
* interface IssueCodeRegistry {
|
|
85
|
+
* email_taken: 'email_taken';
|
|
86
|
+
* }
|
|
87
|
+
* }
|
|
88
|
+
* ```
|
|
89
|
+
*
|
|
90
|
+
* After augmentation, `IssueCode` widens to include the new literal so
|
|
91
|
+
* `defineIssueItem({ code: 'email_taken', ... })` is type-checked.
|
|
92
|
+
*/
|
|
93
|
+
interface IssueCodeRegistry {
|
|
94
|
+
VALUE_INVALID: 'value_invalid';
|
|
95
|
+
ONE_OF_FAILED: 'one_of_failed';
|
|
96
|
+
}
|
|
97
|
+
type IssueCode = IssueCodeRegistry[keyof IssueCodeRegistry];
|
|
98
|
+
declare const IssueCode: {
|
|
99
|
+
readonly VALUE_INVALID: "value_invalid";
|
|
100
|
+
readonly ONE_OF_FAILED: "one_of_failed";
|
|
101
|
+
};
|
|
102
|
+
//#endregion
|
|
103
|
+
//#region src/issue/types.d.ts
|
|
104
|
+
interface IssueBase {
|
|
105
|
+
/**
|
|
106
|
+
* Context in which the issue occurred.
|
|
107
|
+
*/
|
|
108
|
+
meta?: Record<string, unknown>;
|
|
109
|
+
/**
|
|
110
|
+
* Issue path.
|
|
111
|
+
*/
|
|
112
|
+
path: PropertyKey[];
|
|
113
|
+
/**
|
|
114
|
+
* Default-rendered message (eager, English). Use `formatIssue` with a
|
|
115
|
+
* `code → template` map for localized re-rendering at the consumer side.
|
|
116
|
+
*/
|
|
117
|
+
message: string;
|
|
118
|
+
/**
|
|
119
|
+
* Structured parameters used by the default `message` rendering and
|
|
120
|
+
* available to consumer-side formatters (i18n, custom locales). For
|
|
121
|
+
* built-in issues created by the runtime, `params` is populated where
|
|
122
|
+
* the message references a non-trivial value (e.g. attribute name).
|
|
123
|
+
*/
|
|
124
|
+
params?: Record<string, unknown>;
|
|
125
|
+
}
|
|
126
|
+
interface IssueItem extends IssueBase {
|
|
127
|
+
/**
|
|
128
|
+
* Code identifying the issue. Known codes come from `IssueCodeRegistry`
|
|
129
|
+
* (extensible via declaration merging); the `string` widening leaves the
|
|
130
|
+
* door open for ad-hoc codes that don't need a registry entry.
|
|
131
|
+
*/
|
|
132
|
+
code: IssueCode | (string & {});
|
|
133
|
+
/**
|
|
134
|
+
* Issue Type
|
|
135
|
+
*/
|
|
136
|
+
type: 'item';
|
|
137
|
+
/**
|
|
138
|
+
* Received input value.
|
|
139
|
+
*/
|
|
140
|
+
received?: unknown;
|
|
141
|
+
/**
|
|
142
|
+
* Expected input value.
|
|
143
|
+
*/
|
|
144
|
+
expected?: unknown;
|
|
145
|
+
}
|
|
146
|
+
interface IssueGroup extends IssueBase {
|
|
147
|
+
/**
|
|
148
|
+
* Code identifying the issue. See `IssueItem.code` for the registry
|
|
149
|
+
* extension mechanism.
|
|
150
|
+
*/
|
|
151
|
+
code?: IssueCode | (string & {});
|
|
152
|
+
/**
|
|
153
|
+
* Issue Type
|
|
154
|
+
*/
|
|
155
|
+
type: 'group';
|
|
156
|
+
/**
|
|
157
|
+
* Child issues
|
|
158
|
+
*/
|
|
159
|
+
issues: Issue[];
|
|
160
|
+
}
|
|
161
|
+
type Issue = IssueGroup | IssueItem;
|
|
162
|
+
//#endregion
|
|
163
|
+
//#region src/issue/check.d.ts
|
|
164
|
+
declare function isIssueItem(input: unknown): input is IssueItem;
|
|
165
|
+
declare function isIssueGroup(input: unknown): input is IssueGroup;
|
|
166
|
+
declare function isIssue(input: unknown): input is Issue;
|
|
167
|
+
//#endregion
|
|
168
|
+
//#region src/issue/define.d.ts
|
|
169
|
+
type PartialProps<T extends Record<PropertyKey, any>, K extends keyof T> = Pick<T, Exclude<keyof T, K>> & Partial<Pick<T, K>>;
|
|
170
|
+
declare function defineIssueItem(data: Omit<PartialProps<IssueItem, 'code'>, 'type'>): IssueItem;
|
|
171
|
+
declare function defineIssueGroup(data: Omit<IssueGroup, 'type'>): IssueGroup;
|
|
172
|
+
//#endregion
|
|
173
|
+
//#region src/issue/flatten.d.ts
|
|
174
|
+
declare function flattenIssueItems(issues: Issue[]): IssueItem[];
|
|
175
|
+
declare function flattenIssueGroups(issues: Issue[]): IssueGroup[];
|
|
176
|
+
//#endregion
|
|
177
|
+
//#region src/issue/format.d.ts
|
|
178
|
+
/**
|
|
179
|
+
* Map of issue `code` → message template. Templates use `{name}` placeholders
|
|
180
|
+
* — the same syntax `@ebec/core`'s `interpolate` understands — and are
|
|
181
|
+
* resolved against `Issue.params` at format time.
|
|
182
|
+
*
|
|
183
|
+
* ```ts
|
|
184
|
+
* const de: IssueMessageTemplates = {
|
|
185
|
+
* value_invalid: 'Wert ist ungültig',
|
|
186
|
+
* one_of_failed: 'Keine der Varianten war erfolgreich',
|
|
187
|
+
* };
|
|
188
|
+
* ```
|
|
189
|
+
*/
|
|
190
|
+
type IssueMessageTemplates = Partial<Record<IssueCode, string>> & Record<string, string>;
|
|
191
|
+
/**
|
|
192
|
+
* Render an issue's user-facing message.
|
|
193
|
+
*
|
|
194
|
+
* Resolution order:
|
|
195
|
+
* 1. If `templates[code]` exists, return `interpolate(template, issue.params)`.
|
|
196
|
+
* 2. Else return `issue.message` (the default English rendering set at
|
|
197
|
+
* construction time).
|
|
198
|
+
* 3. Else return `fallback`.
|
|
199
|
+
*/
|
|
200
|
+
declare function formatIssue(issue: Issue, templates?: IssueMessageTemplates, fallback?: string): string;
|
|
201
|
+
//#endregion
|
|
202
|
+
//#region src/error/base.d.ts
|
|
203
|
+
declare class ValidupError extends BaseError {
|
|
204
|
+
readonly issues: Issue[];
|
|
205
|
+
constructor(issues?: Issue[]);
|
|
206
|
+
toJSON(): ReturnType<BaseError['toJSON']> & {
|
|
207
|
+
issues: Issue[];
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
//#endregion
|
|
211
|
+
//#region src/error/check.d.ts
|
|
212
|
+
declare function isError(input: unknown): input is Error & {
|
|
213
|
+
[key: string]: any;
|
|
214
|
+
};
|
|
215
|
+
declare function isValidupError(error: unknown): error is ValidupError;
|
|
216
|
+
//#endregion
|
|
217
|
+
//#region src/types.d.ts
|
|
218
|
+
type ObjectLiteral = Record<string | number, any>;
|
|
219
|
+
type ValidatorContext<C = unknown> = {
|
|
220
|
+
/**
|
|
221
|
+
* The expanded mount path in the current container.
|
|
222
|
+
*/
|
|
223
|
+
key: string;
|
|
224
|
+
/**
|
|
225
|
+
* The global mount path of the parent container.
|
|
226
|
+
*/
|
|
227
|
+
path: PropertyKey[];
|
|
228
|
+
/**
|
|
229
|
+
* The actual value, which should be validated.
|
|
230
|
+
*/
|
|
231
|
+
value: unknown;
|
|
232
|
+
/**
|
|
233
|
+
* The input data of the current container.
|
|
234
|
+
*/
|
|
235
|
+
data: Record<string, any>;
|
|
236
|
+
/**
|
|
237
|
+
* The group name for which the validator is executed.
|
|
238
|
+
*/
|
|
239
|
+
group?: string;
|
|
240
|
+
/**
|
|
241
|
+
* Caller-supplied context, forwarded unchanged through nested containers.
|
|
242
|
+
* Useful for request-scoped data (current user, locale, DB connection,
|
|
243
|
+
* request id) that validators need but isn't part of the validated value.
|
|
244
|
+
*/
|
|
245
|
+
context: C;
|
|
246
|
+
/**
|
|
247
|
+
* Cancellation signal forwarded from `ContainerRunOptions.signal`.
|
|
248
|
+
* Async validators (DB lookups, HTTP fetches) should pass it through to
|
|
249
|
+
* cancel I/O when the run is aborted. The container itself checks
|
|
250
|
+
* `signal.aborted` between mounts and rethrows the signal's `reason`.
|
|
251
|
+
*/
|
|
252
|
+
signal?: AbortSignal;
|
|
253
|
+
};
|
|
254
|
+
/**
|
|
255
|
+
* A `Validator` either returns the (optionally transformed) value or throws.
|
|
256
|
+
*
|
|
257
|
+
* @typeParam C - Caller-supplied context type, propagated from the parent
|
|
258
|
+
* `Container<T, C>` and surfaced on `ValidatorContext.context`.
|
|
259
|
+
* @typeParam Out - The validator's resolved output type. Defaults to `unknown`,
|
|
260
|
+
* so call sites that don't care about typed output compile
|
|
261
|
+
* unchanged. The builder API (`defineSchema`) uses this
|
|
262
|
+
* second generic to accumulate per-field types.
|
|
263
|
+
*/
|
|
264
|
+
type Validator<C = unknown, Out = unknown> = (ctx: ValidatorContext<C>) => Out | Promise<Out>;
|
|
265
|
+
//#endregion
|
|
266
|
+
//#region src/container/types.d.ts
|
|
267
|
+
type ContainerOptions<T> = {
|
|
268
|
+
/**
|
|
269
|
+
* Only one of the mounted container/validators must succeed.
|
|
270
|
+
*/
|
|
271
|
+
oneOf?: boolean;
|
|
272
|
+
/**
|
|
273
|
+
* Limit mounted paths on execution.
|
|
274
|
+
* By default, all mounted containers/validators will
|
|
275
|
+
* be considered for execution.
|
|
276
|
+
*/
|
|
277
|
+
pathsToInclude?: Path<T>[];
|
|
278
|
+
/**
|
|
279
|
+
* Exclude mounted paths on execution.
|
|
280
|
+
* By default, all mounted containers/validators will
|
|
281
|
+
* be considered for execution.
|
|
282
|
+
*/
|
|
283
|
+
pathsToExclude?: Path<T>[];
|
|
284
|
+
};
|
|
285
|
+
type ContainerRunOptions<T extends Record<string, any> = Record<string, any>, C = unknown> = {
|
|
286
|
+
/**
|
|
287
|
+
* Default values for the container output.
|
|
288
|
+
*/
|
|
289
|
+
defaults?: { [Key in Path<T>]: any };
|
|
290
|
+
/**
|
|
291
|
+
* Group to execute.
|
|
292
|
+
*/
|
|
293
|
+
group?: string;
|
|
294
|
+
/**
|
|
295
|
+
* Output flat object?
|
|
296
|
+
*/
|
|
297
|
+
flat?: boolean;
|
|
298
|
+
/**
|
|
299
|
+
* Passed path from the parent container.
|
|
300
|
+
*/
|
|
301
|
+
path?: PropertyKey[];
|
|
302
|
+
/**
|
|
303
|
+
* Limit mounted paths on execution.
|
|
304
|
+
* By default, all mounted containers/validators will
|
|
305
|
+
* be considered for execution.
|
|
306
|
+
*/
|
|
307
|
+
pathsToInclude?: Path<T>[];
|
|
308
|
+
/**
|
|
309
|
+
* Exclude mounted paths on execution.
|
|
310
|
+
* By default, all mounted containers/validators will
|
|
311
|
+
* be considered for execution.
|
|
312
|
+
*/
|
|
313
|
+
pathsToExclude?: Path<T>[];
|
|
314
|
+
/**
|
|
315
|
+
* Caller-supplied context surfaced on `ValidatorContext.context`. Forwarded
|
|
316
|
+
* unchanged into nested container `run()` calls so the entire tree shares
|
|
317
|
+
* the same value.
|
|
318
|
+
*/
|
|
319
|
+
context?: C;
|
|
320
|
+
/**
|
|
321
|
+
* Cancellation signal. The container checks `signal.aborted` between
|
|
322
|
+
* mounts and rethrows `signal.reason` when set. Forwarded unchanged into
|
|
323
|
+
* nested `run()` calls and surfaced on `ValidatorContext.signal` so async
|
|
324
|
+
* validators can pass it into `fetch`, DB drivers, etc.
|
|
325
|
+
*/
|
|
326
|
+
signal?: AbortSignal;
|
|
327
|
+
/**
|
|
328
|
+
* Run mounted validators / nested containers concurrently rather than
|
|
329
|
+
* sequentially. Useful when several independent fields hit slow async
|
|
330
|
+
* resources (DB lookups, HTTP calls). Forwarded unchanged into nested
|
|
331
|
+
* `run()` calls.
|
|
332
|
+
*
|
|
333
|
+
* Trade-off: in parallel mode each mount captures its `value` from the
|
|
334
|
+
* input `data` *before* any other mount runs, so a later mount can no
|
|
335
|
+
* longer read an earlier mount's transformed `output[key]`. Use
|
|
336
|
+
* sequential mode (the default) for chained-key sanitize-then-validate
|
|
337
|
+
* patterns.
|
|
338
|
+
*/
|
|
339
|
+
parallel?: boolean;
|
|
340
|
+
};
|
|
341
|
+
type MountOptions = {
|
|
342
|
+
/**
|
|
343
|
+
* Group(s) to execute.
|
|
344
|
+
*/
|
|
345
|
+
group?: string | string[];
|
|
346
|
+
/**
|
|
347
|
+
* Specify if an optional value is also acceptable for the mount key.
|
|
348
|
+
* An optional value won't be passed to the underlying container/validator.
|
|
349
|
+
*
|
|
350
|
+
* Pass a predicate `(value) => boolean` for fine-grained control beyond
|
|
351
|
+
* what `optionalValue` covers (e.g. "treat empty string as missing but
|
|
352
|
+
* keep `0`"). When a predicate is provided, `optionalValue` is ignored.
|
|
353
|
+
*
|
|
354
|
+
* default: false
|
|
355
|
+
*/
|
|
356
|
+
optional?: boolean | ((value: unknown) => boolean);
|
|
357
|
+
/**
|
|
358
|
+
* Which values are considered optional.
|
|
359
|
+
* An optional value won't be passed to the underlying container/validator.
|
|
360
|
+
*
|
|
361
|
+
* default: 'undefined'
|
|
362
|
+
*/
|
|
363
|
+
optionalValue?: `${OptionalValue}`;
|
|
364
|
+
/**
|
|
365
|
+
* Include optional value as property in output.
|
|
366
|
+
*
|
|
367
|
+
* default: false
|
|
368
|
+
*/
|
|
369
|
+
optionalInclude?: boolean;
|
|
370
|
+
};
|
|
371
|
+
type ResultSuccess<T> = {
|
|
372
|
+
success: true;
|
|
373
|
+
data: T;
|
|
374
|
+
error?: never;
|
|
375
|
+
};
|
|
376
|
+
type ResultFailure = {
|
|
377
|
+
success: false;
|
|
378
|
+
data?: never;
|
|
379
|
+
error: ValidupError;
|
|
380
|
+
};
|
|
381
|
+
type Result<T extends ObjectLiteral = ObjectLiteral> = ResultSuccess<T> | ResultFailure;
|
|
382
|
+
interface IContainer<T extends ObjectLiteral = ObjectLiteral, C = unknown> {
|
|
383
|
+
/**
|
|
384
|
+
* Run container with given input.
|
|
385
|
+
*
|
|
386
|
+
* @param input
|
|
387
|
+
* @param options
|
|
388
|
+
*/
|
|
389
|
+
run(input?: Record<string, any>, options?: ContainerRunOptions<T, C>): Promise<T>;
|
|
390
|
+
/**
|
|
391
|
+
* Safe run container with given input.
|
|
392
|
+
*
|
|
393
|
+
* @param input
|
|
394
|
+
* @param options
|
|
395
|
+
*/
|
|
396
|
+
safeRun(input?: Record<string, any>, options?: ContainerRunOptions<T, C>): Promise<Result<T>>;
|
|
397
|
+
/**
|
|
398
|
+
* Synchronous variant of `run()`. Optional — implementations can omit
|
|
399
|
+
* this when their validators are inherently async. The default
|
|
400
|
+
* `Container` provides it; nested containers without it cannot be
|
|
401
|
+
* traversed by a parent's `runSync`.
|
|
402
|
+
*/
|
|
403
|
+
runSync?(input?: Record<string, any>, options?: ContainerRunOptions<T, C>): T;
|
|
404
|
+
/**
|
|
405
|
+
* Synchronous variant of `safeRun()`. Optional — same caveat as `runSync`.
|
|
406
|
+
*/
|
|
407
|
+
safeRunSync?(input?: Record<string, any>, options?: ContainerRunOptions<T, C>): Result<T>;
|
|
408
|
+
}
|
|
409
|
+
type BaseMount = {
|
|
410
|
+
options: MountOptions;
|
|
411
|
+
/**
|
|
412
|
+
* Mount path expressed in dot/bracket notation (e.g. `foo.bar`,
|
|
413
|
+
* `items[0].name`). Path segments are joined and split on `.` throughout
|
|
414
|
+
* the runtime, so keys that contain a *literal* dot (e.g. an object keyed
|
|
415
|
+
* `data['user.email']`) are not addressable as a single mount segment and
|
|
416
|
+
* will produce ambiguous flat output when merged. Use bracketed/array
|
|
417
|
+
* shapes instead for inputs that contain literal-dot keys.
|
|
418
|
+
*/
|
|
419
|
+
path?: string;
|
|
420
|
+
};
|
|
421
|
+
type ContainerMount = BaseMount & {
|
|
422
|
+
type: 'container';
|
|
423
|
+
data: IContainer<any, any>;
|
|
424
|
+
};
|
|
425
|
+
type ValidatorMount<C = unknown> = BaseMount & {
|
|
426
|
+
type: 'validator';
|
|
427
|
+
data: Validator<C>;
|
|
428
|
+
};
|
|
429
|
+
type Mount<C = unknown> = ContainerMount | ValidatorMount<C>;
|
|
430
|
+
//#endregion
|
|
431
|
+
//#region src/container/check.d.ts
|
|
432
|
+
declare function isContainer(input: unknown): input is IContainer;
|
|
433
|
+
//#endregion
|
|
434
|
+
//#region src/container/module.d.ts
|
|
435
|
+
declare class Container<T extends Record<string, any> = Record<string, any>, C = unknown> implements IContainer<T, C> {
|
|
436
|
+
protected options: ContainerOptions<T>;
|
|
437
|
+
protected items: Mount<C>[];
|
|
438
|
+
constructor(options?: ContainerOptions<T>);
|
|
439
|
+
mount(container: IContainer<any, any>): void;
|
|
440
|
+
mount(options: MountOptions, container: IContainer<any, any>): void;
|
|
441
|
+
mount(key: Path<T> | (string & {}), data: IContainer<any, any> | Validator<C>): void;
|
|
442
|
+
mount(key: Path<T> | (string & {}), options: MountOptions, data: IContainer<any, any> | Validator<C>): void;
|
|
443
|
+
/**
|
|
444
|
+
* @throws ValidupError
|
|
445
|
+
*
|
|
446
|
+
* @param data
|
|
447
|
+
* @param options
|
|
448
|
+
*/
|
|
449
|
+
run(data?: Record<string, any>, options?: ContainerRunOptions<T, C>): Promise<T>;
|
|
450
|
+
/**
|
|
451
|
+
* Parallel-execution variant of `run()`. All mounts kick off their
|
|
452
|
+
* promises eagerly and the results are merged after `Promise.allSettled`.
|
|
453
|
+
* See `ContainerRunOptions.parallel` for the trade-off note.
|
|
454
|
+
*/
|
|
455
|
+
private runParallel;
|
|
456
|
+
safeRun(input?: Record<string, any>, options?: ContainerRunOptions<T, C>): Promise<Result<T>>;
|
|
457
|
+
/**
|
|
458
|
+
* @throws ValidupError
|
|
459
|
+
*
|
|
460
|
+
* Synchronous variant of `run()`. Throws synchronously if any validator
|
|
461
|
+
* returns a thenable, or if a nested container does not implement
|
|
462
|
+
* `runSync`. Use it for purely synchronous validator graphs where the
|
|
463
|
+
* microtask overhead of `await` per mount matters (e.g. driving a
|
|
464
|
+
* reactive UI without a `pending` flicker).
|
|
465
|
+
*/
|
|
466
|
+
runSync(data?: Record<string, any>, options?: ContainerRunOptions<T, C>): T;
|
|
467
|
+
safeRunSync(input?: Record<string, any>, options?: ContainerRunOptions<T, C>): Result<T>;
|
|
468
|
+
private resolveContainerFilters;
|
|
469
|
+
/**
|
|
470
|
+
* Prepend `keyParts` to a child issue's `path` and — when the child is
|
|
471
|
+
* an `IssueGroup` — recurse into its nested issues so every leaf carries
|
|
472
|
+
* the parent prefix. Without recursion, a child container that already
|
|
473
|
+
* wrapped its own multi-issue mount in a group (or a `oneOf` child) would
|
|
474
|
+
* surface inner items with paths missing the parent segment, breaking
|
|
475
|
+
* downstream consumers that rely on `flattenIssueItems` for per-field
|
|
476
|
+
* lookup (e.g. `@validup/vue`).
|
|
477
|
+
*/
|
|
478
|
+
private prefixIssuePath;
|
|
479
|
+
/**
|
|
480
|
+
* Translate a per-mount throw into accumulated `issues`. Re-throws when
|
|
481
|
+
* the run was aborted (signal-aware validators) so abort errors are not
|
|
482
|
+
* mangled into validation issues. `keyParts` is the current mount's
|
|
483
|
+
* expanded path (used to prepend to nested `ValidupError` issues).
|
|
484
|
+
*/
|
|
485
|
+
private recordMountError;
|
|
486
|
+
/**
|
|
487
|
+
* Apply post-loop semantics: oneOf aggregation, error throw, defaults
|
|
488
|
+
* fill, and flat-vs-nested output expansion.
|
|
489
|
+
*/
|
|
490
|
+
private finalizeOutput;
|
|
491
|
+
private wrapSafeRunError;
|
|
492
|
+
private isItemGroupIncluded;
|
|
493
|
+
/**
|
|
494
|
+
* Join flat-output keys with `.` separators, preserving any pre-existing
|
|
495
|
+
* leading dot on the right-hand side. Note: keys containing a *literal*
|
|
496
|
+
* dot collide with the dotted-path syntax used for nested-output
|
|
497
|
+
* expansion (see `setPathValue` in `pathtrace`) and will produce
|
|
498
|
+
* ambiguous output when re-expanded — avoid mounting/returning such keys.
|
|
499
|
+
*/
|
|
500
|
+
private mergePaths;
|
|
501
|
+
protected initialize(): void;
|
|
502
|
+
}
|
|
503
|
+
//#endregion
|
|
504
|
+
//#region src/builder/types.d.ts
|
|
505
|
+
/**
|
|
506
|
+
* Cosmetic helper — flattens an intersection so editors render
|
|
507
|
+
* `{ foo: string; bar: number }` instead of `{ foo: string } & { bar: number }`.
|
|
508
|
+
*/
|
|
509
|
+
type Spread<T> = { [K in keyof T]: T[K] };
|
|
510
|
+
/**
|
|
511
|
+
* Anything that can be mounted under a key on a `Builder`: a leaf validator,
|
|
512
|
+
* a nested builder (auto-`.build()`-ed), or an existing `Container`.
|
|
513
|
+
*/
|
|
514
|
+
type MountTarget<C> = Validator<C, any> | IBuilder<any, C> | IContainer<any, C>;
|
|
515
|
+
/**
|
|
516
|
+
* `true` when `O` declares `optional: true` or `optional: (value) => …`. The
|
|
517
|
+
* builder's `mount(key, options, target)` overload uses this to widen the
|
|
518
|
+
* accumulated key to `{ K?: V }`. Plain `boolean`-typed options stay required —
|
|
519
|
+
* the `const` modifier on the `mount` generic preserves literal `true` from
|
|
520
|
+
* inline option objects, which is the common case.
|
|
521
|
+
*/
|
|
522
|
+
type IsOptional<O> = O extends {
|
|
523
|
+
optional: true;
|
|
524
|
+
} ? true : O extends {
|
|
525
|
+
optional: (value: any) => any;
|
|
526
|
+
} ? true : false;
|
|
527
|
+
/**
|
|
528
|
+
* Resolved field shape produced by `mount(key, target, …)`.
|
|
529
|
+
*
|
|
530
|
+
* Builder / Container targets → `{ [K]: U }` where `U` is the child's shape.
|
|
531
|
+
* Validator targets → `{ [K]: Awaited<Out> }` based on the validator's return
|
|
532
|
+
* type. In all three cases, `IsOptional<O>` widens the property to
|
|
533
|
+
* `{ [K]?: … }` when `options.optional` is the literal `true` or a predicate —
|
|
534
|
+
* matching the runtime behavior that an optional mount may be skipped, leaving
|
|
535
|
+
* the key absent from the output.
|
|
536
|
+
*/
|
|
537
|
+
type Mounted<K extends string, V, O> = V extends IBuilder<infer U, any> ? IsOptional<O> extends true ? { [P in K]?: U } : { [P in K]: U } : V extends IContainer<infer U, any> ? IsOptional<O> extends true ? { [P in K]?: U } : { [P in K]: U } : V extends Validator<any, infer Out> ? IsOptional<O> extends true ? { [P in K]?: Awaited<Out> } : { [P in K]: Awaited<Out> } : never;
|
|
538
|
+
/**
|
|
539
|
+
* Compile-time-typed schema builder. `mount(...)` accumulates the resolved
|
|
540
|
+
* field type into `T`, so `build()` returns a `Container<T, C>` whose static
|
|
541
|
+
* return type from `run()` reflects exactly what was registered.
|
|
542
|
+
*
|
|
543
|
+
* Builders are immutable: every method returns a new `Builder` so chains
|
|
544
|
+
* may fork without leaking state. The shape of `mount` mirrors
|
|
545
|
+
* `Container.mount`'s keyed forms — `(key, target)` and
|
|
546
|
+
* `(key, options, target)` — so transferring patterns between the imperative
|
|
547
|
+
* and builder APIs is straightforward.
|
|
548
|
+
*/
|
|
549
|
+
interface IBuilder<T extends Record<string, any>, C = unknown> {
|
|
550
|
+
/**
|
|
551
|
+
* Mount a leaf validator, nested builder, or existing container at `key`.
|
|
552
|
+
* The accumulated shape gains the resolved type of `target`. Re-mounting
|
|
553
|
+
* the same key overrides the previous registration's type (matching the
|
|
554
|
+
* runtime "last write wins" behavior of `Container.mount`).
|
|
555
|
+
*/
|
|
556
|
+
mount<K extends string, V extends MountTarget<C>>(key: K, target: V): IBuilder<Spread<Omit<T, K> & Mounted<K, V, undefined>>, C>;
|
|
557
|
+
/**
|
|
558
|
+
* Mount with `MountOptions`. When `options.optional` is `true` (or a
|
|
559
|
+
* predicate function) the accumulated key widens to `{ K?: V }`.
|
|
560
|
+
*/
|
|
561
|
+
mount<K extends string, const O extends MountOptions, V extends MountTarget<C>>(key: K, options: O, target: V): IBuilder<Spread<Omit<T, K> & Mounted<K, V, O>>, C>;
|
|
562
|
+
/** Mark the resulting container as `oneOf` (only one branch must succeed). */
|
|
563
|
+
oneOf(): IBuilder<T, C>;
|
|
564
|
+
/** Restrict the resulting container's `pathsToInclude`. */
|
|
565
|
+
pathsToInclude(...paths: (keyof T & string)[]): IBuilder<T, C>;
|
|
566
|
+
/** Restrict the resulting container's `pathsToExclude`. */
|
|
567
|
+
pathsToExclude(...paths: (keyof T & string)[]): IBuilder<T, C>;
|
|
568
|
+
/** Materialize a `Container<T, C>` and replay every accumulated mount. */
|
|
569
|
+
build(): Container<T, C>;
|
|
570
|
+
}
|
|
571
|
+
//#endregion
|
|
572
|
+
//#region src/builder/module.d.ts
|
|
573
|
+
type ValidatorStep<C> = {
|
|
574
|
+
kind: 'validator';
|
|
575
|
+
key: string;
|
|
576
|
+
validator: Validator<C>;
|
|
577
|
+
options: MountOptions;
|
|
578
|
+
};
|
|
579
|
+
type NestStep<C> = {
|
|
580
|
+
kind: 'nest';
|
|
581
|
+
key: string;
|
|
582
|
+
child: IContainer<any, C>;
|
|
583
|
+
options: MountOptions;
|
|
584
|
+
};
|
|
585
|
+
type Step<C> = ValidatorStep<C> | NestStep<C>;
|
|
586
|
+
declare class Builder<T extends Record<string, any>, C> implements IBuilder<T, C> {
|
|
587
|
+
private readonly options;
|
|
588
|
+
private readonly steps;
|
|
589
|
+
constructor(options: ContainerOptions<any>, steps: ReadonlyArray<Step<C>>);
|
|
590
|
+
mount(key: string, ...rest: unknown[]): IBuilder<any, C>;
|
|
591
|
+
oneOf(): IBuilder<T, C>;
|
|
592
|
+
pathsToInclude(...paths: (keyof T & string)[]): IBuilder<T, C>;
|
|
593
|
+
pathsToExclude(...paths: (keyof T & string)[]): IBuilder<T, C>;
|
|
594
|
+
build(): Container<T, C>;
|
|
595
|
+
}
|
|
596
|
+
/**
|
|
597
|
+
* Entry point for the compile-time-typed schema builder. The accumulator
|
|
598
|
+
* starts at `{}` and grows as `.mount(...)` calls are chained;
|
|
599
|
+
* `.build()` returns a `Container<T, C>` whose `run()` reflects the
|
|
600
|
+
* accumulated shape.
|
|
601
|
+
*
|
|
602
|
+
* Pass the context type as the generic parameter to flow it through nested
|
|
603
|
+
* builders and validator factories: `defineSchema<MyContext>()`.
|
|
604
|
+
*/
|
|
605
|
+
declare function defineSchema<C = unknown>(): IBuilder<{}, C>;
|
|
606
|
+
//#endregion
|
|
607
|
+
//#region src/utils/has-own-property.d.ts
|
|
608
|
+
declare function hasOwnProperty<X extends {}, Y extends PropertyKey>(obj: X, prop: Y): obj is X & Record<Y, unknown>;
|
|
609
|
+
//#endregion
|
|
610
|
+
//#region src/utils/object.d.ts
|
|
611
|
+
declare function isObject(input: unknown): input is Record<string, any>;
|
|
612
|
+
//#endregion
|
|
613
|
+
export { BaseMount, Builder, Container, ContainerMount, ContainerOptions, ContainerRunOptions, GroupKey, IBuilder, IContainer, IsOptional, Issue, IssueBase, IssueCode, IssueCodeRegistry, IssueGroup, IssueItem, IssueMessageTemplates, Mount, MountOptions, MountTarget, Mounted, ObjectLiteral, OptionalValue, PathFilterResolution, Result, ResultFailure, ResultSuccess, Spread, Validator, ValidatorContext, ValidatorMount, ValidupError, buildErrorMessageForAttribute, buildErrorMessageForAttributes, defineIssueGroup, defineIssueItem, defineSchema, flattenIssueGroups, flattenIssueItems, formatIssue, hasOwnProperty, interpolate, isContainer, isError, isIssue, isIssueGroup, isIssueItem, isObject, isOptionalValue, isValidupError, resolveDefaults, resolvePathFilter, stringifyPath };
|
|
614
|
+
//# sourceMappingURL=index.d.mts.map
|