validup 1.0.0 → 2.0.1
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 +17 -8
- package/dist/index.d.mts +3 -427
- package/dist/index.mjs +27 -220
- package/dist/index.mjs.map +1 -1
- package/package.json +5 -2
package/dist/index.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { BaseError,
|
|
1
|
+
import { BaseError, IssueCode, defineIssueGroup, defineIssueItem, isIssue, markInstanceof, matchesInstanceof, prefixIssuePath } from "@ebec/core";
|
|
2
2
|
import { distinctArray } from "smob";
|
|
3
3
|
import { arrayToPath, expandPath, getPathValue, pathToArray, setPathValue } from "pathtrace";
|
|
4
4
|
import { op, runTwinAsync, runTwinSync } from "twinop";
|
|
@@ -135,12 +135,14 @@ function isStructuralThrow(error, signal) {
|
|
|
135
135
|
}
|
|
136
136
|
//#endregion
|
|
137
137
|
//#region src/error/base.ts
|
|
138
|
+
const VALIDUP_ERROR_INSTANCE = Symbol.for("validup/ValidupError");
|
|
138
139
|
var ValidupError = class extends BaseError {
|
|
139
140
|
issues;
|
|
140
141
|
constructor(issues = []) {
|
|
141
142
|
const paths = issues.map((issue) => stringifyPath(issue.path));
|
|
142
143
|
super({ message: buildErrorMessageForAttributes(paths) });
|
|
143
144
|
this.issues = issues;
|
|
145
|
+
markInstanceof(this, VALIDUP_ERROR_INSTANCE);
|
|
144
146
|
}
|
|
145
147
|
toJSON() {
|
|
146
148
|
return {
|
|
@@ -150,188 +152,6 @@ var ValidupError = class extends BaseError {
|
|
|
150
152
|
}
|
|
151
153
|
};
|
|
152
154
|
//#endregion
|
|
153
|
-
//#region src/issue/check.ts
|
|
154
|
-
function isIssuePath(input) {
|
|
155
|
-
if (!Array.isArray(input)) return false;
|
|
156
|
-
return input.every((el) => typeof el === "string" || typeof el === "number" || typeof el === "symbol");
|
|
157
|
-
}
|
|
158
|
-
function isBaseIssue(input) {
|
|
159
|
-
if (!isObject(input)) return false;
|
|
160
|
-
if (typeof input.message !== "string") return false;
|
|
161
|
-
if (!isIssuePath(input.path)) return false;
|
|
162
|
-
return typeof input.meta === "undefined" || isObject(input.meta);
|
|
163
|
-
}
|
|
164
|
-
function isIssueItem(input) {
|
|
165
|
-
if (!isBaseIssue(input)) return false;
|
|
166
|
-
if (input.type !== "item") return false;
|
|
167
|
-
return typeof input.code === "string";
|
|
168
|
-
}
|
|
169
|
-
function isIssueGroup(input) {
|
|
170
|
-
if (!isBaseIssue(input)) return false;
|
|
171
|
-
if (input.type !== "group") return false;
|
|
172
|
-
return Array.isArray(input.issues) && input.issues.every((issue) => isIssueItem(issue) || isIssueGroup(issue));
|
|
173
|
-
}
|
|
174
|
-
function isIssue(input) {
|
|
175
|
-
return isIssueGroup(input) || isIssueItem(input);
|
|
176
|
-
}
|
|
177
|
-
//#endregion
|
|
178
|
-
//#region src/issue/constants.ts
|
|
179
|
-
/**
|
|
180
|
-
* Vocabulary of well-known issue codes. Adapter packages map foreign
|
|
181
|
-
* validator codes onto these so consumer-side translation catalogs
|
|
182
|
-
* (e.g. `@ilingo/validup`) can ship one localized message per code
|
|
183
|
-
* instead of falling back to a generic "invalid value" fallback.
|
|
184
|
-
*
|
|
185
|
-
* Tracks the common ground between vuelidate, zod, joi, and yup. The
|
|
186
|
-
* core lifts from vuelidate's catalog (snake_cased to match validup's
|
|
187
|
-
* convention); modern format codes (`PATTERN`, `UUID`, `DATE`,
|
|
188
|
-
* `JSON`, `BASE64`, `STRONG_PASSWORD`, …) cover ground vuelidate
|
|
189
|
-
* predates. Every format code names the format positively (`UUID`,
|
|
190
|
-
* `EMAIL`, `URL`, …) rather than the failure mode (`NOT_UUID`,
|
|
191
|
-
* `INVALID_EMAIL`) so the registry reads consistently.
|
|
192
|
-
*
|
|
193
|
-
* Ad-hoc / project-specific codes still work — `IssueItem.code` is
|
|
194
|
-
* widened to `IssueCode | (string & {})`, so `defineIssueItem({ code:
|
|
195
|
-
* 'email_taken', ... })` is valid. If you want a typed const for your
|
|
196
|
-
* own codes, define one alongside this one:
|
|
197
|
-
*
|
|
198
|
-
* ```ts
|
|
199
|
-
* import { IssueCode } from 'validup';
|
|
200
|
-
*
|
|
201
|
-
* export const AppCode = {
|
|
202
|
-
* ...IssueCode,
|
|
203
|
-
* EMAIL_TAKEN: 'email_taken',
|
|
204
|
-
* } as const;
|
|
205
|
-
* ```
|
|
206
|
-
*
|
|
207
|
-
* Each entry's JSDoc documents the structured `data` adapters should
|
|
208
|
-
* attach when constructing the issue — templates can rely on those
|
|
209
|
-
* placeholders being present (`{{min}}`, `{{max}}`, `{{other}}`, etc.).
|
|
210
|
-
*/
|
|
211
|
-
const IssueCode = {
|
|
212
|
-
/** Generic fallback when no more-specific code applies. `data`: — */
|
|
213
|
-
VALUE_INVALID: "value_invalid",
|
|
214
|
-
/** Every branch of a `oneOf` container failed. `data`: — */
|
|
215
|
-
ONE_OF_FAILED: "one_of_failed",
|
|
216
|
-
/** Value is missing, `undefined`, `null`, or empty per the validator's semantics. `data`: — */
|
|
217
|
-
REQUIRED: "required",
|
|
218
|
-
/** Value contains characters outside the alphabetical set. `data`: — */
|
|
219
|
-
ALPHA: "alpha",
|
|
220
|
-
/** Value contains characters outside the alphanumeric set. `data`: — */
|
|
221
|
-
ALPHA_NUM: "alpha_num",
|
|
222
|
-
/** Value is not a number. `data`: — */
|
|
223
|
-
NUMERIC: "numeric",
|
|
224
|
-
/** Value is not an integer. `data`: — */
|
|
225
|
-
INTEGER: "integer",
|
|
226
|
-
/** Value is not a decimal number. `data`: — */
|
|
227
|
-
DECIMAL: "decimal",
|
|
228
|
-
/** Value is shorter than the configured minimum. `data`: `{ min: number }` */
|
|
229
|
-
MIN_LENGTH: "min_length",
|
|
230
|
-
/** Value is longer than the configured maximum. `data`: `{ max: number }` */
|
|
231
|
-
MAX_LENGTH: "max_length",
|
|
232
|
-
/** Numeric value is below the configured minimum. `data`: `{ min: number }` */
|
|
233
|
-
MIN_VALUE: "min_value",
|
|
234
|
-
/** Numeric value is above the configured maximum. `data`: `{ max: number }` */
|
|
235
|
-
MAX_VALUE: "max_value",
|
|
236
|
-
/** Numeric value falls outside the configured `[min, max]` range. `data`: `{ min: number, max: number }` */
|
|
237
|
-
BETWEEN: "between",
|
|
238
|
-
/** Value is not a valid email address. `data`: — */
|
|
239
|
-
EMAIL: "email",
|
|
240
|
-
/** Value is not a valid URL. `data`: — */
|
|
241
|
-
URL: "url",
|
|
242
|
-
/** Value is not a valid IP address. `data`: — */
|
|
243
|
-
IP_ADDRESS: "ip_address",
|
|
244
|
-
/** Value is not a valid MAC address. `data`: — */
|
|
245
|
-
MAC_ADDRESS: "mac_address",
|
|
246
|
-
/** Value is not a valid UUID. `data`: — */
|
|
247
|
-
UUID: "uuid",
|
|
248
|
-
/** Value is not a valid date / cannot be parsed as a date. `data`: — */
|
|
249
|
-
DATE: "date",
|
|
250
|
-
/**
|
|
251
|
-
* Value does not match the expected regex pattern. `data`:
|
|
252
|
-
* `{ pattern: string }` — source of the regex without flags so
|
|
253
|
-
* catalogs can quote it in human-readable form.
|
|
254
|
-
*/
|
|
255
|
-
PATTERN: "pattern",
|
|
256
|
-
/** Value is not valid JSON. `data`: — */
|
|
257
|
-
JSON: "json",
|
|
258
|
-
/** Value is not valid base64. `data`: — */
|
|
259
|
-
BASE64: "base64",
|
|
260
|
-
/**
|
|
261
|
-
* Value does not meet the configured strong-password rules. `data`:
|
|
262
|
-
* `{ minLength?: number, minLowercase?: number, minUppercase?: number,
|
|
263
|
-
* minNumbers?: number, minSymbols?: number }` — only the keys the
|
|
264
|
-
* adapter chose to surface; missing keys mean "the default for that
|
|
265
|
-
* setting wasn't configured."
|
|
266
|
-
*/
|
|
267
|
-
STRONG_PASSWORD: "strong_password",
|
|
268
|
-
/**
|
|
269
|
-
* Value must equal another named field's value (sibling-field assertion,
|
|
270
|
-
* e.g. password-confirm). `data`: `{ other: string }` — the name of
|
|
271
|
-
* the field being compared against.
|
|
272
|
-
*/
|
|
273
|
-
SAME_AS: "same_as"
|
|
274
|
-
};
|
|
275
|
-
//#endregion
|
|
276
|
-
//#region src/issue/define.ts
|
|
277
|
-
/**
|
|
278
|
-
* Build an `IssueItem`. TS uses the supplied `code` to pick the right
|
|
279
|
-
* `data` requirement at the call site — passing `MIN_LENGTH` without
|
|
280
|
-
* `data: { min }` is a compile error; passing `STRONG_PASSWORD` with
|
|
281
|
-
* `data: { pointsPerUnique: 5 }` is a compile error.
|
|
282
|
-
*
|
|
283
|
-
* `code` may be omitted (defaults to `IssueCode.VALUE_INVALID` — bare,
|
|
284
|
-
* no data).
|
|
285
|
-
*/
|
|
286
|
-
function defineIssueItem(data) {
|
|
287
|
-
return {
|
|
288
|
-
type: "item",
|
|
289
|
-
...data,
|
|
290
|
-
code: data.code || IssueCode.VALUE_INVALID
|
|
291
|
-
};
|
|
292
|
-
}
|
|
293
|
-
function defineIssueGroup(data) {
|
|
294
|
-
return {
|
|
295
|
-
type: "group",
|
|
296
|
-
...data
|
|
297
|
-
};
|
|
298
|
-
}
|
|
299
|
-
//#endregion
|
|
300
|
-
//#region src/issue/flatten.ts
|
|
301
|
-
function flattenIssueItems(issues) {
|
|
302
|
-
const output = [];
|
|
303
|
-
for (const issue of issues) if (isIssueItem(issue)) output.push(issue);
|
|
304
|
-
else output.push(...flattenIssueItems(issue.issues));
|
|
305
|
-
return output;
|
|
306
|
-
}
|
|
307
|
-
function flattenIssueGroups(issues) {
|
|
308
|
-
const output = [];
|
|
309
|
-
for (const issue of issues) if (isIssueGroup(issue)) {
|
|
310
|
-
output.push(issue);
|
|
311
|
-
output.push(...flattenIssueGroups(issue.issues));
|
|
312
|
-
}
|
|
313
|
-
return output;
|
|
314
|
-
}
|
|
315
|
-
//#endregion
|
|
316
|
-
//#region src/issue/format.ts
|
|
317
|
-
/**
|
|
318
|
-
* Render an issue's user-facing message.
|
|
319
|
-
*
|
|
320
|
-
* Resolution order:
|
|
321
|
-
* 1. If `templates[code]` exists, return `interpolate(template, issue.data)`.
|
|
322
|
-
* 2. Else return `issue.message` (the default English rendering set at
|
|
323
|
-
* construction time).
|
|
324
|
-
* 3. Else return `fallback`.
|
|
325
|
-
*/
|
|
326
|
-
function formatIssue(issue, templates, fallback = "") {
|
|
327
|
-
if (templates && issue.code) {
|
|
328
|
-
const template = templates[issue.code];
|
|
329
|
-
if (typeof template === "string") return interpolate$1(template, issue.data || {});
|
|
330
|
-
}
|
|
331
|
-
if (typeof issue.message === "string" && issue.message.length > 0) return issue.message;
|
|
332
|
-
return fallback;
|
|
333
|
-
}
|
|
334
|
-
//#endregion
|
|
335
155
|
//#region src/error/check.ts
|
|
336
156
|
function isError(input) {
|
|
337
157
|
if (input instanceof Error) return true;
|
|
@@ -341,8 +161,9 @@ function isError(input) {
|
|
|
341
161
|
function isValidupError(error) {
|
|
342
162
|
if (!isError(error)) return false;
|
|
343
163
|
if (error instanceof ValidupError) return true;
|
|
164
|
+
if (matchesInstanceof(error, VALIDUP_ERROR_INSTANCE)) return true;
|
|
344
165
|
if (!hasOwnProperty(error, "issues")) return false;
|
|
345
|
-
return Array.isArray(error.issues) && error.issues.every((issue) => isIssue(issue));
|
|
166
|
+
return Array.isArray(error.issues) && error.issues.every((issue) => isIssue(issue)) && (error.issues.length > 0 || error.code === "VALIDUP_ERROR");
|
|
346
167
|
}
|
|
347
168
|
//#endregion
|
|
348
169
|
//#region src/helpers/error-to-issues.ts
|
|
@@ -1050,9 +871,10 @@ var Container = class {
|
|
|
1050
871
|
group: options.group
|
|
1051
872
|
};
|
|
1052
873
|
const cached = this.resolveCachedOutcome(item, key, snapshot, options.cache);
|
|
1053
|
-
if (cached)
|
|
1054
|
-
|
|
1055
|
-
|
|
874
|
+
if (cached) {
|
|
875
|
+
if (cached.ok) output[key] = cached.value;
|
|
876
|
+
else throw cached.error;
|
|
877
|
+
} else {
|
|
1056
878
|
const validator = item.data;
|
|
1057
879
|
const ctx = this.buildValidatorContext(key, plan, value, data, options);
|
|
1058
880
|
try {
|
|
@@ -1184,12 +1006,13 @@ var Container = class {
|
|
|
1184
1006
|
const branchStart = issues.length;
|
|
1185
1007
|
for (const [j, task] of tasks.entries()) {
|
|
1186
1008
|
const result = settled[j];
|
|
1187
|
-
if (result.status === "fulfilled")
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1009
|
+
if (result.status === "fulfilled") {
|
|
1010
|
+
if (task.kind === "container") {
|
|
1011
|
+
const tmp = result.value;
|
|
1012
|
+
const tmpKeys = Object.keys(tmp);
|
|
1013
|
+
for (const tmpKey of tmpKeys) output[this.mergePaths(task.key, tmpKey)] = tmp[tmpKey];
|
|
1014
|
+
} else output[task.key] = result.value;
|
|
1015
|
+
} else {
|
|
1193
1016
|
this.collectExecutionFailure({
|
|
1194
1017
|
error: result.reason,
|
|
1195
1018
|
item,
|
|
@@ -1576,23 +1399,6 @@ var Container = class {
|
|
|
1576
1399
|
});
|
|
1577
1400
|
}
|
|
1578
1401
|
/**
|
|
1579
|
-
* Prepend `keyParts` to a child issue's `path` and — when the child is
|
|
1580
|
-
* an `IssueGroup` — recurse into its nested issues so every leaf carries
|
|
1581
|
-
* the parent prefix. Without recursion, a child container that already
|
|
1582
|
-
* wrapped its own multi-issue mount in a group (or a `oneOf` child) would
|
|
1583
|
-
* surface inner items with paths missing the parent segment, breaking
|
|
1584
|
-
* downstream consumers that rely on `flattenIssueItems` for per-field
|
|
1585
|
-
* lookup (e.g. `@validup/vue`).
|
|
1586
|
-
*/
|
|
1587
|
-
prefixIssuePath(issue, keyParts) {
|
|
1588
|
-
const prefixed = {
|
|
1589
|
-
...issue,
|
|
1590
|
-
path: [...keyParts, ...issue.path || []]
|
|
1591
|
-
};
|
|
1592
|
-
if (prefixed.type === "group") prefixed.issues = prefixed.issues.map((nested) => this.prefixIssuePath(nested, keyParts));
|
|
1593
|
-
return prefixed;
|
|
1594
|
-
}
|
|
1595
|
-
/**
|
|
1596
1402
|
* Translate a throw raised by one execution step (one validator or
|
|
1597
1403
|
* nested-container invocation, from the surrounding `run` loop) into
|
|
1598
1404
|
* accumulated issues and push them onto `context.issues`. Re-throws when
|
|
@@ -1634,7 +1440,7 @@ var Container = class {
|
|
|
1634
1440
|
};
|
|
1635
1441
|
const childIssues = [];
|
|
1636
1442
|
if (isValidupError(error)) for (let i = 0; i < error.issues.length; i++) {
|
|
1637
|
-
const prefixed =
|
|
1443
|
+
const prefixed = prefixIssuePath(error.issues[i], keyParts);
|
|
1638
1444
|
childIssues.push(item.type === "validator" ? markOptionalDeep(prefixed) : prefixed);
|
|
1639
1445
|
}
|
|
1640
1446
|
else if (isError(error)) childIssues.push(markOptional(defineIssueItem({
|
|
@@ -1645,14 +1451,15 @@ var Container = class {
|
|
|
1645
1451
|
path: keyParts,
|
|
1646
1452
|
message: typeof error === "string" && error.length > 0 ? error : `Non-Error throw: ${String(error)}`
|
|
1647
1453
|
})));
|
|
1648
|
-
if (pathRelative)
|
|
1649
|
-
|
|
1650
|
-
|
|
1651
|
-
|
|
1652
|
-
|
|
1653
|
-
|
|
1654
|
-
|
|
1655
|
-
|
|
1454
|
+
if (pathRelative) {
|
|
1455
|
+
if (item.type === "container" || childIssues.length > 1) issues.push(markOptional(defineIssueGroup({
|
|
1456
|
+
message: buildErrorMessageForAttribute(String(pathRelative)),
|
|
1457
|
+
data: { name: String(pathRelative) },
|
|
1458
|
+
path: keyParts,
|
|
1459
|
+
issues: childIssues
|
|
1460
|
+
})));
|
|
1461
|
+
else issues.push(...childIssues);
|
|
1462
|
+
} else issues.push(...childIssues);
|
|
1656
1463
|
}
|
|
1657
1464
|
/**
|
|
1658
1465
|
* For `oneOf` containers, wrap the issues produced by a single branch
|
|
@@ -1885,6 +1692,6 @@ var ResultCache = class {
|
|
|
1885
1692
|
}
|
|
1886
1693
|
};
|
|
1887
1694
|
//#endregion
|
|
1888
|
-
export { Builder, Container, GroupKey,
|
|
1695
|
+
export { Builder, Container, GroupKey, OptionalValue, PathsStrictViolationError, ResultCache, VALIDUP_ERROR_INSTANCE, ValidupError, buildErrorMessageForAttribute, buildErrorMessageForAttributes, buildOneOfFailedGroup, compose, composeOneOf, createValidupError, defineSchema, defineValidator, errorToIssues, hasOwnProperty, isContainer, isError, isObject, isOptionalValue, isPathsStrictViolation, isResultCache, isValidatorDescriptor, isValidupError, resolveDefaults, resolvePathFilter, stringifyPath };
|
|
1889
1696
|
|
|
1890
1697
|
//# sourceMappingURL=index.mjs.map
|