@matatbread/matbot-tool-types 0.4.13 → 0.4.15

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/package.json +5 -5
  2. package/src/validator.ts +15 -1
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "matbotRuntime": [
4
4
  "node"
5
5
  ],
6
- "version": "0.4.13",
6
+ "version": "0.4.15",
7
7
  "engines": {
8
8
  "node": ">=24.0.0"
9
9
  },
@@ -36,14 +36,14 @@
36
36
  "typescript": "^6.0.3"
37
37
  },
38
38
  "peerDependencies": {
39
- "@matatbread/matbot-plugin-api": "^0.4.14",
40
- "@matatbread/matbot-core": "^0.4.14"
39
+ "@matatbread/matbot-plugin-api": "^0.4.15",
40
+ "@matatbread/matbot-core": "^0.4.15"
41
41
  },
42
42
  "devDependencies": {
43
43
  "@types/node": "^22.0.0",
44
44
  "typescript": "^6.0.3",
45
- "@matatbread/matbot-core": "^0.4.14",
46
- "@matatbread/matbot-plugin-api": "^0.4.14"
45
+ "@matatbread/matbot-plugin-api": "^0.4.15",
46
+ "@matatbread/matbot-core": "^0.4.15"
47
47
  },
48
48
  "scripts": {
49
49
  "typecheck": "tsc --noEmit"
package/src/validator.ts CHANGED
@@ -252,7 +252,21 @@ export function generateValidator(
252
252
  const c = check(a, 'v', 'p', `${where}|${i}`);
253
253
  return ` if (${c.reports ? c.expr.replace(/, e\)$/, ', scratch)') : c.expr}) return true;`;
254
254
  }).join('\n');
255
- return ` const scratch = [];\n${attempts}\n E(p, 'no union member matched', v, e);\n return false;`;
255
+ return `${excessAcrossArms(arms)} const scratch = [];\n${attempts}\n E(p, 'no union member matched', v, e);\n return false;`;
256
+ }
257
+
258
+ // A key no arm declares fails EVERY arm, so it can be reported by name before the arms are tried —
259
+ // exact, not a guess at which arm was meant. Without it, a call that invents a key (an `action` on a
260
+ // tool that has none) is reported as whatever else that arm lacks, which sends the caller to fix the
261
+ // arguments it did pass instead of the key it should not have.
262
+ function excessAcrossArms(arms: readonly TS.Type[]): string {
263
+ const objectLike = F.Object | F.Intersection;
264
+ if (!reject || !arms.every(a => (a.flags & objectLike) && !checker.getIndexInfoOfType(a, ts.IndexKind.String))) return '';
265
+ const known = [...new Set(arms.flatMap(a => checker.getPropertiesOfType(a).map(s => s.name)))];
266
+ return ` if (typeof v === 'object' && v !== null && !Array.isArray(v)) {\n`
267
+ + ` let excess = false;\n`
268
+ + ` for (const k of Object.keys(v)) if (!${lit(known)}.includes(k)) { E(K(p, k), 'unexpected property', v[k], e); excess = true; }\n`
269
+ + ` if (excess) return false;\n }\n`;
256
270
  }
257
271
 
258
272
  function bodyOf(t: TS.Type, where: string): string {