validup 1.0.0 → 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/dist/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { BaseError, interpolate, interpolate as interpolate$1 } from "@ebec/core";
1
+ import { BaseError, IssueCode, defineIssueGroup, defineIssueItem, isIssue, 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";
@@ -150,188 +150,6 @@ var ValidupError = class extends BaseError {
150
150
  }
151
151
  };
152
152
  //#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
153
  //#region src/error/check.ts
336
154
  function isError(input) {
337
155
  if (input instanceof Error) return true;
@@ -1050,9 +868,10 @@ var Container = class {
1050
868
  group: options.group
1051
869
  };
1052
870
  const cached = this.resolveCachedOutcome(item, key, snapshot, options.cache);
1053
- if (cached) if (cached.ok) output[key] = cached.value;
1054
- else throw cached.error;
1055
- else {
871
+ if (cached) {
872
+ if (cached.ok) output[key] = cached.value;
873
+ else throw cached.error;
874
+ } else {
1056
875
  const validator = item.data;
1057
876
  const ctx = this.buildValidatorContext(key, plan, value, data, options);
1058
877
  try {
@@ -1184,12 +1003,13 @@ var Container = class {
1184
1003
  const branchStart = issues.length;
1185
1004
  for (const [j, task] of tasks.entries()) {
1186
1005
  const result = settled[j];
1187
- if (result.status === "fulfilled") if (task.kind === "container") {
1188
- const tmp = result.value;
1189
- const tmpKeys = Object.keys(tmp);
1190
- for (const tmpKey of tmpKeys) output[this.mergePaths(task.key, tmpKey)] = tmp[tmpKey];
1191
- } else output[task.key] = result.value;
1192
- else {
1006
+ if (result.status === "fulfilled") {
1007
+ if (task.kind === "container") {
1008
+ const tmp = result.value;
1009
+ const tmpKeys = Object.keys(tmp);
1010
+ for (const tmpKey of tmpKeys) output[this.mergePaths(task.key, tmpKey)] = tmp[tmpKey];
1011
+ } else output[task.key] = result.value;
1012
+ } else {
1193
1013
  this.collectExecutionFailure({
1194
1014
  error: result.reason,
1195
1015
  item,
@@ -1576,23 +1396,6 @@ var Container = class {
1576
1396
  });
1577
1397
  }
1578
1398
  /**
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
1399
  * Translate a throw raised by one execution step (one validator or
1597
1400
  * nested-container invocation, from the surrounding `run` loop) into
1598
1401
  * accumulated issues and push them onto `context.issues`. Re-throws when
@@ -1634,7 +1437,7 @@ var Container = class {
1634
1437
  };
1635
1438
  const childIssues = [];
1636
1439
  if (isValidupError(error)) for (let i = 0; i < error.issues.length; i++) {
1637
- const prefixed = this.prefixIssuePath(error.issues[i], keyParts);
1440
+ const prefixed = prefixIssuePath(error.issues[i], keyParts);
1638
1441
  childIssues.push(item.type === "validator" ? markOptionalDeep(prefixed) : prefixed);
1639
1442
  }
1640
1443
  else if (isError(error)) childIssues.push(markOptional(defineIssueItem({
@@ -1645,14 +1448,15 @@ var Container = class {
1645
1448
  path: keyParts,
1646
1449
  message: typeof error === "string" && error.length > 0 ? error : `Non-Error throw: ${String(error)}`
1647
1450
  })));
1648
- if (pathRelative) if (item.type === "container" || childIssues.length > 1) issues.push(markOptional(defineIssueGroup({
1649
- message: buildErrorMessageForAttribute(String(pathRelative)),
1650
- data: { name: String(pathRelative) },
1651
- path: keyParts,
1652
- issues: childIssues
1653
- })));
1654
- else issues.push(...childIssues);
1655
- else issues.push(...childIssues);
1451
+ if (pathRelative) {
1452
+ if (item.type === "container" || childIssues.length > 1) issues.push(markOptional(defineIssueGroup({
1453
+ message: buildErrorMessageForAttribute(String(pathRelative)),
1454
+ data: { name: String(pathRelative) },
1455
+ path: keyParts,
1456
+ issues: childIssues
1457
+ })));
1458
+ else issues.push(...childIssues);
1459
+ } else issues.push(...childIssues);
1656
1460
  }
1657
1461
  /**
1658
1462
  * For `oneOf` containers, wrap the issues produced by a single branch
@@ -1885,6 +1689,6 @@ var ResultCache = class {
1885
1689
  }
1886
1690
  };
1887
1691
  //#endregion
1888
- export { Builder, Container, GroupKey, IssueCode, OptionalValue, PathsStrictViolationError, ResultCache, ValidupError, buildErrorMessageForAttribute, buildErrorMessageForAttributes, buildOneOfFailedGroup, compose, composeOneOf, createValidupError, defineIssueGroup, defineIssueItem, defineSchema, defineValidator, errorToIssues, flattenIssueGroups, flattenIssueItems, formatIssue, hasOwnProperty, interpolate, isContainer, isError, isIssue, isIssueGroup, isIssueItem, isObject, isOptionalValue, isPathsStrictViolation, isResultCache, isValidatorDescriptor, isValidupError, resolveDefaults, resolvePathFilter, stringifyPath };
1692
+ export { Builder, Container, GroupKey, OptionalValue, PathsStrictViolationError, ResultCache, ValidupError, buildErrorMessageForAttribute, buildErrorMessageForAttributes, buildOneOfFailedGroup, compose, composeOneOf, createValidupError, defineSchema, defineValidator, errorToIssues, hasOwnProperty, isContainer, isError, isObject, isOptionalValue, isPathsStrictViolation, isResultCache, isValidatorDescriptor, isValidupError, resolveDefaults, resolvePathFilter, stringifyPath };
1889
1693
 
1890
1694
  //# sourceMappingURL=index.mjs.map