@usearazzo/validator 1.0.1-alpha.32 → 1.0.1-alpha.33

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/CHANGELOG.md CHANGED
@@ -3,6 +3,10 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ ## [1.0.1-alpha.33](https://github.com/usearazzo/arazzo-toolkit/compare/v1.0.1-alpha.32...v1.0.1-alpha.33) (2026-11-16)
7
+
8
+ **Note:** Version bump only for package @usearazzo/validator
9
+
6
10
  ## [1.0.1-alpha.32](https://github.com/usearazzo/arazzo-toolkit/compare/v1.0.1-alpha.31...v1.0.1-alpha.32) (2026-11-13)
7
11
 
8
12
  **Note:** Version bump only for package @usearazzo/validator
@@ -183935,22 +183935,38 @@ const grammar = new _grammar_mjs__WEBPACK_IMPORTED_MODULE_1__["default"]();
183935
183935
  * In strict mode (default) the whole string must parse as an expression-string,
183936
183936
  * otherwise an empty array is returned. With `strict: false`, any `{` not followed
183937
183937
  * by `$` is treated as literal text and every valid `{$...}` span is extracted;
183938
- * invalid expression attempts are skipped.
183938
+ * invalid expression attempts are skipped. Each of them is reported to `onError`,
183939
+ * when given, with the same information `interpolate` passes to its `onError`;
183940
+ * its return value is ignored, as there is nothing to replace. `onError` is
183941
+ * ignored in strict mode.
183939
183942
  *
183940
183943
  * @example
183941
183944
  *
183942
183945
  * extract('{$url}'); // => ['$url']
183943
183946
  * extract('id={$inputs.id}&name={$inputs.name}'); // => ['$inputs.id', '$inputs.name']
183944
183947
  * extract('{"id": "{$inputs.id}"}', { strict: false }); // => ['$inputs.id']
183948
+ *
183949
+ * const errors = [];
183950
+ * extract('x={$inputs.}&y={$inputs.ok}', { strict: false, onError: (info) => errors.push(info) });
183951
+ * // => ['$inputs.ok'], errors[0].text === '{$inputs.}'
183945
183952
  */
183946
183953
  const extract = (str, {
183947
- strict = true
183954
+ strict = true,
183955
+ onError
183948
183956
  } = {}) => {
183949
183957
  if (typeof str !== 'string') {
183950
183958
  return [];
183951
183959
  }
183952
183960
  if (strict === false) {
183953
- return (0,_scanner_mjs__WEBPACK_IMPORTED_MODULE_3__["default"])(str).filter(token => token.type === 'expression').map(token => token.expression);
183961
+ const expressions = [];
183962
+ for (const token of (0,_scanner_mjs__WEBPACK_IMPORTED_MODULE_3__["default"])(str)) {
183963
+ if (token.type === 'expression') {
183964
+ expressions.push(token.expression);
183965
+ } else if (token.type === 'invalid' && typeof onError === 'function') {
183966
+ onError((0,_scanner_mjs__WEBPACK_IMPORTED_MODULE_3__.invalidAttemptInfo)(token));
183967
+ }
183968
+ }
183969
+ return expressions;
183954
183970
  }
183955
183971
  const parser = new apg_lite__WEBPACK_IMPORTED_MODULE_0__.Parser();
183956
183972
  parser.ast = new _parse_translators_CSTTranslator_mjs__WEBPACK_IMPORTED_MODULE_2__["default"]();
@@ -185496,8 +185512,6 @@ __webpack_require__.r(__webpack_exports__);
185496
185512
  /* harmony import */ var _grammar_mjs__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(71652);
185497
185513
  /* harmony import */ var _parse_translators_CSTTranslator_mjs__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(35963);
185498
185514
  /* harmony import */ var _scanner_mjs__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(21425);
185499
- /* harmony import */ var _errors_ArazzoRuntimeExpressionParseError_mjs__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__(4089);
185500
-
185501
185515
 
185502
185516
 
185503
185517
 
@@ -185574,31 +185588,7 @@ const interpolate = (template, resolver, {
185574
185588
  if (token.type === 'expression') {
185575
185589
  output += stringify(resolver(token.expression));
185576
185590
  } else if (token.type === 'invalid') {
185577
- const {
185578
- text,
185579
- expression,
185580
- start,
185581
- length,
185582
- terminated
185583
- } = token;
185584
- let error;
185585
- const replacement = onError({
185586
- text,
185587
- start,
185588
- length,
185589
- // created lazily - capturing a stack trace for every invalid attempt is costly
185590
- get error() {
185591
- if (error === undefined) {
185592
- const message = terminated ? `Invalid runtime expression "${text}" at position ${start}` : `Unterminated runtime expression "${text}" at position ${start}`;
185593
- error = new _errors_ArazzoRuntimeExpressionParseError_mjs__WEBPACK_IMPORTED_MODULE_4__["default"](message, {
185594
- runtimeExpression: expression,
185595
- start,
185596
- length
185597
- });
185598
- }
185599
- return error;
185600
- }
185601
- });
185591
+ const replacement = onError((0,_scanner_mjs__WEBPACK_IMPORTED_MODULE_3__.invalidAttemptInfo)(token));
185602
185592
  if (typeof replacement !== 'string') {
185603
185593
  throw new TypeError('onError must return a string');
185604
185594
  }
@@ -185735,7 +185725,8 @@ const parse = (runtimeExpression, {
185735
185725
  __webpack_require__.r(__webpack_exports__);
185736
185726
  class Expectations extends Array {
185737
185727
  toString() {
185738
- return this.map(c => `"${String(c)}"`).join(', ');
185728
+ // JSON string syntax, so that entries such as '"' and '\\' stay unambiguous
185729
+ return this.map(c => JSON.stringify(String(c))).join(', ');
185739
185730
  }
185740
185731
  }
185741
185732
  /* harmony default export */ const __WEBPACK_DEFAULT_EXPORT__ = (Expectations);
@@ -185752,34 +185743,226 @@ class Expectations extends Array {
185752
185743
  "use strict";
185753
185744
  __webpack_require__.r(__webpack_exports__);
185754
185745
  /* harmony import */ var apg_lite__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(51751);
185755
- /* harmony import */ var _Expectations_mjs__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(3770);
185746
+ /* harmony import */ var _grammar_mjs__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(71652);
185747
+ /* harmony import */ var _Expectations_mjs__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(3770);
185756
185748
 
185757
185749
 
185758
- class Trace extends apg_lite__WEBPACK_IMPORTED_MODULE_0__.Trace {
185759
- inferExpectations() {
185760
- const lines = this.displayTrace().split('\n');
185761
- const expectations = new Set();
185762
- let lastMatchedIndex = -1;
185763
- for (let i = 0; i < lines.length; i++) {
185764
- const line = lines[i];
185750
+ // TLS (case-insensitive literal) opcodes store the string lowercased; the grammar source
185751
+ // keeps the spelling it is written with (e.g. "$statusCode"), so expectations use that.
185752
+ const literalSpellings = new Map();
185753
+ for (const [, literal] of String(new _grammar_mjs__WEBPACK_IMPORTED_MODULE_1__["default"]()).matchAll(/"([^"\n]*)"/g)) {
185754
+ const key = literal.toLowerCase();
185755
+ if (!literalSpellings.has(key)) literalSpellings.set(key, literal);
185756
+ }
185757
+ const hex = code => code.toString(16).toUpperCase().padStart(2, '0');
185758
+ const describeTerminal = op => {
185759
+ switch (op.type) {
185760
+ case apg_lite__WEBPACK_IMPORTED_MODULE_0__.identifiers.TLS:
185761
+ {
185762
+ const literal = String.fromCodePoint(...op.string);
185763
+ return literalSpellings.get(literal) ?? literal;
185764
+ }
185765
+ case apg_lite__WEBPACK_IMPORTED_MODULE_0__.identifiers.TBS:
185766
+ return String.fromCodePoint(...op.string);
185767
+ case apg_lite__WEBPACK_IMPORTED_MODULE_0__.identifiers.TRG:
185768
+ return op.min === op.max ? `%x${hex(op.min)}` : `%x${hex(op.min)}-${hex(op.max)}`;
185769
+ default:
185770
+ return undefined;
185771
+ }
185772
+ };
185765
185773
 
185766
- // capture the max match line (first one that ends in a single character match)
185767
- if (line.includes('M|')) {
185768
- const textMatch = line.match(/]'(.*)'$/);
185769
- if (textMatch && textMatch[1]) {
185770
- lastMatchedIndex = i;
185774
+ // FIRST set of an opcode: the terminals a match of it can start with, and whether it can
185775
+ // match the empty phrase (in which case what follows it can start the match as well)
185776
+ const first = (rules, opcodes, index, visiting = new Set()) => {
185777
+ const op = opcodes[index];
185778
+ switch (op.type) {
185779
+ case apg_lite__WEBPACK_IMPORTED_MODULE_0__.identifiers.ALT:
185780
+ {
185781
+ const terminals = [];
185782
+ let nullable = false;
185783
+ for (const child of op.children) {
185784
+ const result = first(rules, opcodes, child, visiting);
185785
+ terminals.push(...result.terminals);
185786
+ nullable = nullable || result.nullable;
185787
+ }
185788
+ return {
185789
+ terminals,
185790
+ nullable
185791
+ };
185792
+ }
185793
+ case apg_lite__WEBPACK_IMPORTED_MODULE_0__.identifiers.CAT:
185794
+ {
185795
+ const terminals = [];
185796
+ for (const child of op.children) {
185797
+ const result = first(rules, opcodes, child, visiting);
185798
+ terminals.push(...result.terminals);
185799
+ if (!result.nullable) return {
185800
+ terminals,
185801
+ nullable: false
185802
+ };
185771
185803
  }
185804
+ return {
185805
+ terminals,
185806
+ nullable: true
185807
+ };
185808
+ }
185809
+ case apg_lite__WEBPACK_IMPORTED_MODULE_0__.identifiers.REP:
185810
+ {
185811
+ const result = first(rules, opcodes, index + 1, visiting);
185812
+ return {
185813
+ terminals: result.terminals,
185814
+ nullable: op.min === 0 || result.nullable
185815
+ };
185772
185816
  }
185817
+ case apg_lite__WEBPACK_IMPORTED_MODULE_0__.identifiers.RNM:
185818
+ {
185819
+ // a left-recursive reference adds nothing the outer visit does not already add
185820
+ if (visiting.has(op.index)) return {
185821
+ terminals: [],
185822
+ nullable: false
185823
+ };
185824
+ visiting.add(op.index);
185825
+ const result = first(rules, rules[op.index].opcodes, 0, visiting);
185826
+ visiting.delete(op.index);
185827
+ return result;
185828
+ }
185829
+ case apg_lite__WEBPACK_IMPORTED_MODULE_0__.identifiers.TLS:
185830
+ case apg_lite__WEBPACK_IMPORTED_MODULE_0__.identifiers.TBS:
185831
+ return {
185832
+ terminals: op.string.length > 0 ? [op] : [],
185833
+ nullable: op.string.length === 0
185834
+ };
185835
+ case apg_lite__WEBPACK_IMPORTED_MODULE_0__.identifiers.TRG:
185836
+ return {
185837
+ terminals: [op],
185838
+ nullable: false
185839
+ };
185840
+ default:
185841
+ // AND, NOT (look-ahead) and UDT do not consume input that can be described as a terminal
185842
+ return {
185843
+ terminals: [],
185844
+ nullable: true
185845
+ };
185846
+ }
185847
+ };
185848
+ const stateSymbol = Symbol('trace state');
185849
+ class Trace extends apg_lite__WEBPACK_IMPORTED_MODULE_0__.Trace {
185850
+ constructor() {
185851
+ super();
185852
+ const baseInit = this.init;
185853
+ const baseDown = this.down;
185854
+ const baseUp = this.up;
185855
+ let rules = [];
185856
+ let inputLength = 0;
185857
+ let repetitions = new Map();
185858
+ let lookAhead = 0;
185859
+ // repetitions in progress: where each one has got to and how many times its child matched
185860
+ let progress = [];
185861
+ const state = {
185862
+ maxMatched: 0,
185863
+ failures: []
185864
+ };
185865
+ this[stateSymbol] = state;
185773
185866
 
185774
- // collect terminal failures after the deepest successful match
185775
- if (i > lastMatchedIndex) {
185776
- const terminalFailMatch = line.match(/N\|\[TLS\(([^)]+)\)]/);
185777
- if (terminalFailMatch) {
185778
- expectations.add(terminalFailMatch[1]);
185867
+ // only failures at the furthest offset are ever reported, so only those are kept
185868
+ const recordFailure = (op, offset) => {
185869
+ if (offset === state.maxMatched) state.failures.push({
185870
+ op,
185871
+ offset
185872
+ });
185873
+ };
185874
+ this.init = (r, u, c) => {
185875
+ rules = r;
185876
+ inputLength = c.length;
185877
+ // the opcodes of a repetition's rule, and its position there, to find its child
185878
+ repetitions = new Map();
185879
+ for (const rule of r) {
185880
+ rule.opcodes.forEach((op, index) => {
185881
+ if (op.type === apg_lite__WEBPACK_IMPORTED_MODULE_0__.identifiers.REP) repetitions.set(op, {
185882
+ opcodes: rule.opcodes,
185883
+ index
185884
+ });
185885
+ });
185886
+ }
185887
+ // a Trace instance may be reused for another parse
185888
+ lookAhead = 0;
185889
+ progress = [];
185890
+ state.maxMatched = 0;
185891
+ state.failures = [];
185892
+ baseInit(r, u, c);
185893
+ };
185894
+ this.down = (op, offset) => {
185895
+ if (op.type === apg_lite__WEBPACK_IMPORTED_MODULE_0__.identifiers.AND || op.type === apg_lite__WEBPACK_IMPORTED_MODULE_0__.identifiers.NOT) lookAhead += 1;
185896
+ if (op.type === apg_lite__WEBPACK_IMPORTED_MODULE_0__.identifiers.REP) {
185897
+ const {
185898
+ opcodes,
185899
+ index
185900
+ } = repetitions.get(op);
185901
+ progress.push({
185902
+ child: opcodes[index + 1],
185903
+ opcodes,
185904
+ index,
185905
+ position: offset,
185906
+ count: 0
185907
+ });
185908
+ }
185909
+ baseDown(op, offset);
185910
+ };
185911
+ this.up = (op, opState, offset, phraseLength) => {
185912
+ if (op.type === apg_lite__WEBPACK_IMPORTED_MODULE_0__.identifiers.AND || op.type === apg_lite__WEBPACK_IMPORTED_MODULE_0__.identifiers.NOT) lookAhead -= 1;
185913
+
185914
+ // look-ahead operators test the input without consuming it, so neither their
185915
+ // matches nor their failures say anything about what the input should contain
185916
+ if (lookAhead === 0) {
185917
+ if (opState === apg_lite__WEBPACK_IMPORTED_MODULE_0__.identifiers.MATCH && offset + phraseLength > state.maxMatched) {
185918
+ state.maxMatched = offset + phraseLength;
185919
+ state.failures = [];
185920
+ }
185921
+ if (opState === apg_lite__WEBPACK_IMPORTED_MODULE_0__.identifiers.NOMATCH && describeTerminal(op) !== undefined) {
185922
+ recordFailure(op, offset);
185923
+ }
185924
+ }
185925
+ const repetition = progress[progress.length - 1];
185926
+ if (repetition && op === repetition.child && opState === apg_lite__WEBPACK_IMPORTED_MODULE_0__.identifiers.MATCH) {
185927
+ repetition.position = offset + phraseLength;
185928
+ repetition.count += 1;
185929
+ }
185930
+ if (op.type === apg_lite__WEBPACK_IMPORTED_MODULE_0__.identifiers.REP) {
185931
+ progress.pop();
185932
+ // at the end of the input a repetition stops without trying its child again, so the
185933
+ // terminals that could have continued it are never tried; add them from its FIRST set
185934
+ if (lookAhead === 0 && repetition.position === inputLength && repetition.count < op.max) {
185935
+ const {
185936
+ opcodes,
185937
+ index
185938
+ } = repetition;
185939
+ for (const terminal of first(rules, opcodes, index + 1).terminals) {
185940
+ recordFailure(terminal, inputLength);
185941
+ }
185779
185942
  }
185780
185943
  }
185944
+ baseUp(op, opState, offset, phraseLength);
185945
+ };
185946
+ }
185947
+
185948
+ /**
185949
+ * Terminals the grammar could have accepted at the offset where parsing stopped
185950
+ * (`result.maxMatched`), in the order the parser tried them, unabbreviated.
185951
+ * Character ranges are given in ABNF notation (e.g. `%x30-39`).
185952
+ */
185953
+ inferExpectations() {
185954
+ const {
185955
+ maxMatched,
185956
+ failures
185957
+ } = this[stateSymbol];
185958
+ const expectations = new Set();
185959
+ for (const {
185960
+ op,
185961
+ offset
185962
+ } of failures) {
185963
+ if (offset === maxMatched) expectations.add(describeTerminal(op));
185781
185964
  }
185782
- return new _Expectations_mjs__WEBPACK_IMPORTED_MODULE_1__["default"](...expectations);
185965
+ return new _Expectations_mjs__WEBPACK_IMPORTED_MODULE_2__["default"](...expectations);
185783
185966
  }
185784
185967
  }
185785
185968
  /* harmony default export */ const __WEBPACK_DEFAULT_EXPORT__ = (Trace);
@@ -186183,6 +186366,8 @@ class XMLTranslator extends _CSTTranslator_mjs__WEBPACK_IMPORTED_MODULE_0__["def
186183
186366
  "use strict";
186184
186367
  __webpack_require__.r(__webpack_exports__);
186185
186368
  /* harmony import */ var _test_index_mjs__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(34976);
186369
+ /* harmony import */ var _errors_ArazzoRuntimeExpressionParseError_mjs__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(4089);
186370
+
186186
186371
 
186187
186372
  /**
186188
186373
  * Tolerantly scan a string containing embedded {expression} patterns.
@@ -186265,9 +186450,43 @@ const scan = str => {
186265
186450
  }
186266
186451
  return tokens;
186267
186452
  };
186453
+
186454
+ /**
186455
+ * Information about an invalid expression attempt, as passed to `onError`
186456
+ * of `interpolate` and `extract`: { text, start, length, error }.
186457
+ *
186458
+ * @private
186459
+ */
186460
+ const invalidAttemptInfo = ({
186461
+ text,
186462
+ expression,
186463
+ start,
186464
+ length,
186465
+ terminated
186466
+ }) => {
186467
+ let error;
186468
+ return {
186469
+ text,
186470
+ start,
186471
+ length,
186472
+ // created lazily - capturing a stack trace for every invalid attempt is costly
186473
+ get error() {
186474
+ if (error === undefined) {
186475
+ const message = terminated ? `Invalid runtime expression "${text}" at position ${start}` : `Unterminated runtime expression "${text}" at position ${start}`;
186476
+ error = new _errors_ArazzoRuntimeExpressionParseError_mjs__WEBPACK_IMPORTED_MODULE_1__["default"](message, {
186477
+ runtimeExpression: expression,
186478
+ start,
186479
+ length
186480
+ });
186481
+ }
186482
+ return error;
186483
+ }
186484
+ };
186485
+ };
186268
186486
  /* harmony default export */ const __WEBPACK_DEFAULT_EXPORT__ = (scan);
186269
186487
  /* harmony export */ __webpack_require__.d(__webpack_exports__, [
186270
- /* harmony export */ "default", 0, __WEBPACK_DEFAULT_EXPORT__
186488
+ /* harmony export */ "default", 0, __WEBPACK_DEFAULT_EXPORT__,
186489
+ /* harmony export */ "invalidAttemptInfo", 0, /* binding */ invalidAttemptInfo
186271
186490
  /* harmony export */ ]);
186272
186491
 
186273
186492