@gunshi/combinators 1.0.0-rc.7 → 1.0.0-rc.9

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.
Files changed (2) hide show
  1. package/lib/index.js +62 -13
  2. package/package.json +2 -2
package/lib/index.js CHANGED
@@ -8,7 +8,7 @@
8
8
  */
9
9
  "-".codePointAt(0);
10
10
  "=".codePointAt(0);
11
- // ../../node_modules/.pnpm/args-tokens@1.2.0/node_modules/args-tokens/lib/utils.js
11
+ // ../../node_modules/.pnpm/args-tokens@1.3.0/node_modules/args-tokens/lib/utils.js
12
12
  /**
13
13
  * @author kazuya kawaguchi (a.k.a. kazupon)
14
14
  * @license MIT
@@ -22,7 +22,7 @@
22
22
  function formatChoices(choices) {
23
23
  return choices.map((value) => JSON.stringify(value)).join(", ");
24
24
  }
25
- // ../../node_modules/.pnpm/args-tokens@1.2.0/node_modules/args-tokens/lib/resolver-DT0CiD5Y.js
25
+ // ../../node_modules/.pnpm/args-tokens@1.3.0/node_modules/args-tokens/lib/resolver-DWmHKGSL.js
26
26
  /**
27
27
  * Internal definitions shared by the modules of `args-tokens`.
28
28
  *
@@ -58,6 +58,11 @@ const PURE_PARSE = Symbol.for("args-tokens.pureParse");
58
58
  * Each code identifies a validation failure category and is also suitable as an
59
59
  * i18n resource key for localized rendering.
60
60
  *
61
+ * A value that breaks a constraint of the options of a combinator is reported with a code of its
62
+ * own: `tooSmall` and `tooLarge` for `min` and `max` of `number()`, `integer()` and `float()`,
63
+ * `tooShort` and `tooLong` for `minLength` and `maxLength` of `string()`, and `patternMismatch` for
64
+ * its `pattern`. What a `parse` function of your own throws is reported as `customParse`.
65
+ *
61
66
  * {@link resolveArgs} and `parse()` do not report `unknownOption` or `unexpectedPositional`: a caller
62
67
  * that knows what a command takes, such as a CLI framework, reports them. An option that is not in the
63
68
  * schema takes the argument after it as its value, unless that argument is an option, and the value
@@ -75,7 +80,12 @@ const ArgsValidationErrorKeys = {
75
80
  missingValue: "err:arg:missing-value",
76
81
  conflict: "err:arg:conflict",
77
82
  invalidDefault: "err:arg:invalid-default",
78
- unexpectedPositional: "err:arg:unexpected-positional"
83
+ unexpectedPositional: "err:arg:unexpected-positional",
84
+ tooSmall: "err:arg:too-small",
85
+ tooLarge: "err:arg:too-large",
86
+ tooShort: "err:arg:too-short",
87
+ tooLong: "err:arg:too-long",
88
+ patternMismatch: "err:arg:pattern-mismatch"
79
89
  };
80
90
  /**
81
91
  * Brand that marks {@link ArgsValidationError} instances.
@@ -127,7 +137,7 @@ var ArgsValidationError = class extends Error {
127
137
  });
128
138
  }
129
139
  };
130
- // ../../node_modules/.pnpm/args-tokens@1.2.0/node_modules/args-tokens/lib/combinators.js
140
+ // ../../node_modules/.pnpm/args-tokens@1.3.0/node_modules/args-tokens/lib/combinators.js
131
141
  /**
132
142
  * Parser combinator factory functions for composable argument schema construction.
133
143
  *
@@ -186,6 +196,12 @@ function createInvalidTypeError(message, expected, actual) {
186
196
  }
187
197
  });
188
198
  }
199
+ function createConstraintError(message, code, values) {
200
+ return new ArgsValidationError(message, {
201
+ code,
202
+ values
203
+ });
204
+ }
189
205
  function createInvalidChoiceError(message, choices, actual) {
190
206
  return new ArgsValidationError(message, {
191
207
  code: ArgsValidationErrorKeys.invalidChoice,
@@ -220,9 +236,18 @@ function string(opts) {
220
236
  ...opts?.short != null ? { short: opts.short } : {},
221
237
  ...opts?.required != null ? { required: opts.required } : {},
222
238
  parse(value) {
223
- if (opts?.minLength != null && value.length < opts.minLength) throw new RangeError(`String must be at least ${opts.minLength} characters`);
224
- if (opts?.maxLength != null && value.length > opts.maxLength) throw new RangeError(`String must be at most ${opts.maxLength} characters`);
225
- if (opts?.pattern != null && !opts.pattern.test(value)) throw new Error(`String must match pattern ${opts.pattern}`);
239
+ if (opts?.minLength != null && value.length < opts.minLength) throw createConstraintError(`String must be at least ${opts.minLength} characters`, ArgsValidationErrorKeys.tooShort, {
240
+ minLength: opts.minLength,
241
+ actual: value
242
+ });
243
+ if (opts?.maxLength != null && value.length > opts.maxLength) throw createConstraintError(`String must be at most ${opts.maxLength} characters`, ArgsValidationErrorKeys.tooLong, {
244
+ maxLength: opts.maxLength,
245
+ actual: value
246
+ });
247
+ if (opts?.pattern != null && !opts.pattern.test(value)) throw createConstraintError(`String must match pattern ${opts.pattern}`, ArgsValidationErrorKeys.patternMismatch, {
248
+ pattern: String(opts.pattern),
249
+ actual: value
250
+ });
226
251
  return value;
227
252
  }
228
253
  };
@@ -241,8 +266,16 @@ function number(opts) {
241
266
  parse(value) {
242
267
  const n = Number(value);
243
268
  if (value.trim() === "" || isNaN(n)) throw createInvalidTypeError(`Expected a number, got '${value}'`, "number", value);
244
- if (opts?.min != null && n < opts.min) throw new RangeError(`Number must be >= ${opts.min}, got ${n}`);
245
- if (opts?.max != null && n > opts.max) throw new RangeError(`Number must be <= ${opts.max}, got ${n}`);
269
+ if (opts?.min != null && n < opts.min) throw createConstraintError(`Number must be >= ${opts.min}, got ${n}`, ArgsValidationErrorKeys.tooSmall, {
270
+ expected: "number",
271
+ min: opts.min,
272
+ actual: value
273
+ });
274
+ if (opts?.max != null && n > opts.max) throw createConstraintError(`Number must be <= ${opts.max}, got ${n}`, ArgsValidationErrorKeys.tooLarge, {
275
+ expected: "number",
276
+ max: opts.max,
277
+ actual: value
278
+ });
246
279
  return n;
247
280
  }
248
281
  });
@@ -260,8 +293,16 @@ function integer(opts) {
260
293
  if (!/^-?\d+$/.test(value)) throw createInvalidTypeError(`Expected an integer, got '${value}'`, "integer", value);
261
294
  const n = Number.parseInt(value, 10);
262
295
  if (isNaN(n)) throw createInvalidTypeError(`Expected an integer, got '${value}'`, "integer", value);
263
- if (opts?.min != null && n < opts.min) throw new RangeError(`Integer must be >= ${opts.min}, got ${n}`);
264
- if (opts?.max != null && n > opts.max) throw new RangeError(`Integer must be <= ${opts.max}, got ${n}`);
296
+ if (opts?.min != null && n < opts.min) throw createConstraintError(`Integer must be >= ${opts.min}, got ${n}`, ArgsValidationErrorKeys.tooSmall, {
297
+ expected: "integer",
298
+ min: opts.min,
299
+ actual: value
300
+ });
301
+ if (opts?.max != null && n > opts.max) throw createConstraintError(`Integer must be <= ${opts.max}, got ${n}`, ArgsValidationErrorKeys.tooLarge, {
302
+ expected: "integer",
303
+ max: opts.max,
304
+ actual: value
305
+ });
265
306
  return n;
266
307
  }
267
308
  });
@@ -280,8 +321,16 @@ function float(opts) {
280
321
  if (!/^[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:e[+-]?\d+)?$/i.test(trimmed)) throw createInvalidTypeError(`Expected a finite float, got '${value}'`, "float", value);
281
322
  const n = Number.parseFloat(trimmed);
282
323
  if (isNaN(n) || !isFinite(n)) throw createInvalidTypeError(`Expected a finite float, got '${value}'`, "float", value);
283
- if (opts?.min != null && n < opts.min) throw new RangeError(`Float must be >= ${opts.min}, got ${n}`);
284
- if (opts?.max != null && n > opts.max) throw new RangeError(`Float must be <= ${opts.max}, got ${n}`);
324
+ if (opts?.min != null && n < opts.min) throw createConstraintError(`Float must be >= ${opts.min}, got ${n}`, ArgsValidationErrorKeys.tooSmall, {
325
+ expected: "float",
326
+ min: opts.min,
327
+ actual: value
328
+ });
329
+ if (opts?.max != null && n > opts.max) throw createConstraintError(`Float must be <= ${opts.max}, got ${n}`, ArgsValidationErrorKeys.tooLarge, {
330
+ expected: "float",
331
+ max: opts.max,
332
+ actual: value
333
+ });
285
334
  return n;
286
335
  }
287
336
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gunshi/combinators",
3
- "version": "1.0.0-rc.7",
3
+ "version": "1.0.0-rc.9",
4
4
  "description": "parser combinators for gunshi argument schema",
5
5
  "keywords": [
6
6
  "cli",
@@ -49,7 +49,7 @@
49
49
  "access": "public"
50
50
  },
51
51
  "devDependencies": {
52
- "gunshi": "1.0.0-rc.7",
52
+ "gunshi": "1.0.0-rc.9",
53
53
  "publint": "^0.3.20"
54
54
  },
55
55
  "engines": {