@niscorp/prism 0.3.0 → 0.3.2

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.cjs CHANGED
@@ -1,7 +1,7 @@
1
1
  'use strict';
2
2
 
3
- var chunkOLZQK2ZF_cjs = require('./chunk-OLZQK2ZF.cjs');
4
- var chunk66YSI4PX_cjs = require('./chunk-66YSI4PX.cjs');
3
+ var chunkROYHYYIL_cjs = require('./chunk-ROYHYYIL.cjs');
4
+ var chunkQUI2X5JB_cjs = require('./chunk-QUI2X5JB.cjs');
5
5
  var strata = require('@niscorp/strata');
6
6
  var dayjs = require('dayjs');
7
7
  var utc = require('dayjs/plugin/utc.js');
@@ -29,7 +29,31 @@ var explainOne = (issue, prefix) => {
29
29
  }
30
30
  return bestDepth <= here.length ? [{ path: here, message: "Not a Prism node: no op matches, and it is not a plain value." }] : best;
31
31
  };
32
- var explainIssues = (issues) => issues.flatMap((i) => explainOne(i, []));
32
+ var valueAt = (value, path) => path.reduce((at, key) => typeof at === "object" && at !== null ? Reflect.get(at, key) : void 0, value);
33
+ var opOf = (value, path) => {
34
+ const last = path[path.length - 1];
35
+ if (typeof last === "string" && last.startsWith("$") && chunkQUI2X5JB_cjs.formOf(last) === void 0) return void 0;
36
+ const here = valueAt(value, path);
37
+ if (typeof here === "object" && here !== null && !Array.isArray(here)) {
38
+ const ops = Object.keys(here).filter((key) => chunkQUI2X5JB_cjs.formOf(key) !== void 0);
39
+ if (ops.length === 1) return ops[0];
40
+ }
41
+ for (const back of [1, 2]) {
42
+ const i = path.length - back;
43
+ const key = path[i];
44
+ if (typeof key !== "string" || chunkQUI2X5JB_cjs.formOf(key) === void 0) continue;
45
+ const holder = valueAt(value, path.slice(0, i));
46
+ if (typeof holder === "object" && holder !== null && Object.keys(holder).length === 1) return key;
47
+ }
48
+ return void 0;
49
+ };
50
+ var withForm = (explained, value) => {
51
+ const op = opOf(value, explained.path);
52
+ const form = op === void 0 ? void 0 : chunkQUI2X5JB_cjs.formOf(op);
53
+ return form === void 0 ? explained : { ...explained, message: `${explained.message.replace(/\.$/, "")}. Form: ${form}` };
54
+ };
55
+ var explainIssues = (issues, value) => issues.flatMap((i) => explainOne(i, [])).map((explained) => withForm(explained, value));
56
+ var issuesText = (explained) => explained.map((i) => `${i.path.join(".") || "(root)"}: ${i.message}`).join("; ");
33
57
 
34
58
  // src/sugar/rewriters.ts
35
59
  var rewriteSum = (node, recurse) => ({
@@ -156,24 +180,24 @@ var desugar = (node) => {
156
180
  if (node === null || node === void 0) return node;
157
181
  if (typeof node !== "object") return node;
158
182
  if (Array.isArray(node)) return node.map(desugar);
159
- if (chunk66YSI4PX_cjs.isWithNode(node)) {
183
+ if (chunkQUI2X5JB_cjs.isWithNode(node)) {
160
184
  const bindings = {};
161
185
  for (const [name, expr] of Object.entries(node.$with.let)) bindings[name] = desugar(expr);
162
186
  return { $with: { let: bindings, value: desugar(node.$with.value) } };
163
187
  }
164
- if (chunk66YSI4PX_cjs.isRenameKeysNode(node)) return { $renameKeys: { ...node.$renameKeys, from: desugar(node.$renameKeys.from) } };
165
- if (chunk66YSI4PX_cjs.isConstNode(node)) return node;
166
- if (chunk66YSI4PX_cjs.isSumNode(node)) return rewriteSum(node, desugar);
167
- if (chunk66YSI4PX_cjs.isAvgNode(node)) return rewriteAvg(node, desugar);
168
- if (chunk66YSI4PX_cjs.isCountNode(node)) return rewriteCount(node, desugar);
169
- if (chunk66YSI4PX_cjs.isMinNode(node)) return rewriteMin(node, desugar);
170
- if (chunk66YSI4PX_cjs.isMaxNode(node)) return rewriteMax(node, desugar);
171
- if (chunk66YSI4PX_cjs.isPluckNode(node)) return rewritePluck(node, desugar);
172
- if (chunk66YSI4PX_cjs.isTakeNode(node)) return rewriteTake(node, desugar);
173
- if (chunk66YSI4PX_cjs.isDropNode(node)) return rewriteDrop(node, desugar);
174
- if (chunk66YSI4PX_cjs.isMatchNode(node)) return rewriteMatch(node, desugar);
175
- if (chunk66YSI4PX_cjs.isFlatMapNode(node)) return rewriteFlatMap(node, desugar);
176
- if (chunk66YSI4PX_cjs.isJsonObject(node)) {
188
+ if (chunkQUI2X5JB_cjs.isRenameKeysNode(node)) return { $renameKeys: { ...node.$renameKeys, from: desugar(node.$renameKeys.from) } };
189
+ if (chunkQUI2X5JB_cjs.isConstNode(node)) return node;
190
+ if (chunkQUI2X5JB_cjs.isSumNode(node)) return rewriteSum(node, desugar);
191
+ if (chunkQUI2X5JB_cjs.isAvgNode(node)) return rewriteAvg(node, desugar);
192
+ if (chunkQUI2X5JB_cjs.isCountNode(node)) return rewriteCount(node, desugar);
193
+ if (chunkQUI2X5JB_cjs.isMinNode(node)) return rewriteMin(node, desugar);
194
+ if (chunkQUI2X5JB_cjs.isMaxNode(node)) return rewriteMax(node, desugar);
195
+ if (chunkQUI2X5JB_cjs.isPluckNode(node)) return rewritePluck(node, desugar);
196
+ if (chunkQUI2X5JB_cjs.isTakeNode(node)) return rewriteTake(node, desugar);
197
+ if (chunkQUI2X5JB_cjs.isDropNode(node)) return rewriteDrop(node, desugar);
198
+ if (chunkQUI2X5JB_cjs.isMatchNode(node)) return rewriteMatch(node, desugar);
199
+ if (chunkQUI2X5JB_cjs.isFlatMapNode(node)) return rewriteFlatMap(node, desugar);
200
+ if (chunkQUI2X5JB_cjs.isJsonObject(node)) {
177
201
  const result = {};
178
202
  for (const [key, value] of Object.entries(node)) {
179
203
  result[key] = desugar(value);
@@ -186,12 +210,14 @@ var desugar = (node) => {
186
210
  // src/engine/budget.ts
187
211
  var DEFAULT_LIMITS = { maxSteps: 1e6, maxStringLength: 1e6, maxValues: 1e6 };
188
212
  var createBudget = (limits) => ({ limits: { ...DEFAULT_LIMITS, ...limits }, steps: 0, sizes: /* @__PURE__ */ new WeakMap() });
189
- var refuse = (what, limit) => {
190
- throw new chunk66YSI4PX_cjs.PrismError(`Evaluation stopped: ${what} (limit ${limit})`, chunk66YSI4PX_cjs.ErrorCode.BUDGET, { details: { limit } });
213
+ var refuse = (what, limit, node) => {
214
+ const message = `Evaluation stopped: ${what} (limit ${limit}).`;
215
+ if (typeof node === "object" && node !== null) return chunkQUI2X5JB_cjs.failAt(message, chunkQUI2X5JB_cjs.ErrorCode.BUDGET, { details: { limit } }, { node, form: false });
216
+ throw new chunkQUI2X5JB_cjs.PrismError(message, chunkQUI2X5JB_cjs.ErrorCode.BUDGET, { details: { limit } });
191
217
  };
192
- var spendStep = (budget) => {
218
+ var spendStep = (budget, node) => {
193
219
  budget.steps += 1;
194
- if (budget.steps > budget.limits.maxSteps) refuse("it evaluated too many nodes", budget.limits.maxSteps);
220
+ if (budget.steps > budget.limits.maxSteps) refuse("it evaluated too many nodes", budget.limits.maxSteps, node);
195
221
  };
196
222
  var sizeOf = (value, budget) => {
197
223
  if (value === null || typeof value !== "object") return 1;
@@ -205,14 +231,14 @@ var sizeOf = (value, budget) => {
205
231
  budget.sizes.set(value, size);
206
232
  return size;
207
233
  };
208
- var measureString = (value, budget) => {
209
- if (typeof value === "string" && value.length > budget.limits.maxStringLength) refuse(`a string of ${value.length} characters`, budget.limits.maxStringLength);
234
+ var measureString = (value, budget, node) => {
235
+ if (typeof value === "string" && value.length > budget.limits.maxStringLength) refuse(`a string of ${value.length} characters`, budget.limits.maxStringLength, node);
210
236
  };
211
- var measure = (value, budget) => {
237
+ var measure = (value, budget, node) => {
212
238
  if (budget === void 0) return;
213
- measureString(value, budget);
239
+ measureString(value, budget, node);
214
240
  if (value !== null && typeof value === "object" && sizeOf(value, budget) > budget.limits.maxValues) {
215
- refuse("a result holding too many values", budget.limits.maxValues);
241
+ refuse("a result with too many values", budget.limits.maxValues, node);
216
242
  }
217
243
  };
218
244
 
@@ -221,10 +247,17 @@ var SEGMENTS_KEY = "__segments";
221
247
  var isSegmentsArray = (value) => Array.isArray(value);
222
248
  var opRef = (node, context, _evaluate) => {
223
249
  const attached = Reflect.get(node, SEGMENTS_KEY);
224
- const segments = isSegmentsArray(attached) ? attached : chunk66YSI4PX_cjs.parseJsonPathCached(node.$ref);
225
- const resolved = chunk66YSI4PX_cjs.getByPath(context.source, segments);
226
- if (resolved === void 0)
227
- throw new chunk66YSI4PX_cjs.PrismError("Path not found", chunk66YSI4PX_cjs.ErrorCode.MISSING_PATH, { op: "$ref", path: node.$ref });
250
+ const segments = isSegmentsArray(attached) ? attached : chunkQUI2X5JB_cjs.parseJsonPathCached(node.$ref);
251
+ const resolved = chunkQUI2X5JB_cjs.getByPath(context.source, segments);
252
+ if (resolved === void 0) {
253
+ const steps = segments.map((s) => s.type === "key" ? s.key : s.index);
254
+ return chunkQUI2X5JB_cjs.failAt("Path not found.", chunkQUI2X5JB_cjs.ErrorCode.MISSING_PATH, { op: "$ref", path: node.$ref }, {
255
+ node,
256
+ op: "$ref",
257
+ form: false,
258
+ detail: () => `$ref ${JSON.stringify(node.$ref)} ${chunkQUI2X5JB_cjs.whereStopped(context.source, steps, (count) => chunkQUI2X5JB_cjs.jsonPathUpTo(segments, count))}`
259
+ });
260
+ }
228
261
  return resolved;
229
262
  };
230
263
  var copyOf = (value) => {
@@ -237,33 +270,50 @@ var copyOf = (value) => {
237
270
  var opConst = (node, context, _evaluate) => context.kept === true ? copyOf(node.$const) : node.$const;
238
271
  var opVar = (node, context, _evaluate) => {
239
272
  const value = context.vars[node.$var];
240
- if (value === void 0)
241
- throw new chunk66YSI4PX_cjs.PrismError(`Variable not found: ${node.$var}`, chunk66YSI4PX_cjs.ErrorCode.VAR_NOT_FOUND, { op: "$var", path: node.$var });
273
+ if (value === void 0) {
274
+ const names = Object.keys(context.vars).filter((name) => !name.startsWith("__"));
275
+ const scope = names.length === 0 ? "No variable is in scope here." : `In scope: ${names.join(", ")}.`;
276
+ return chunkQUI2X5JB_cjs.failAt(`Variable not found: ${node.$var}. ${scope}`, chunkQUI2X5JB_cjs.ErrorCode.VAR_NOT_FOUND, { op: "$var", path: node.$var }, { node, op: "$var", form: false });
277
+ }
242
278
  return value;
243
279
  };
280
+ var getStopped = (node, root, count, context, evaluate2) => {
281
+ const unbudgeted = { ...context, budget: void 0 };
282
+ const steps = node.$get.path.slice(0, count).map((segment) => {
283
+ const evaluated = typeof segment === "object" && segment !== null ? evaluate2(segment, unbudgeted) : segment;
284
+ return typeof evaluated === "number" ? evaluated : String(evaluated);
285
+ });
286
+ return `$get.path ${JSON.stringify(steps)} ${chunkQUI2X5JB_cjs.whereStopped(root, steps, (upTo) => upTo === 0 ? "from" : JSON.stringify(steps.slice(0, upTo)), true)}`;
287
+ };
244
288
  var opGet = (node, context, evaluate2) => {
245
289
  const { from, path, fallback } = node.$get;
246
- let current2 = evaluate2(from, context);
247
- for (const segment of path) {
290
+ const root = evaluate2(from, context);
291
+ let current2 = root;
292
+ for (let i = 0; i < path.length; i++) {
293
+ const segment = path[i];
248
294
  const evaluated = typeof segment === "object" && segment !== null ? evaluate2(segment, context) : segment;
249
295
  if (typeof evaluated === "number") {
250
- if (!chunk66YSI4PX_cjs.isJsonArray(current2)) {
296
+ if (!chunkQUI2X5JB_cjs.isJsonArray(current2)) {
251
297
  if (fallback !== void 0) return evaluate2(fallback, context);
252
- throw new chunk66YSI4PX_cjs.PrismError("Expected array for index access", chunk66YSI4PX_cjs.ErrorCode.TYPE, { op: "$get" });
298
+ return chunkQUI2X5JB_cjs.failAt(getStopped(node, root, i + 1, context, evaluate2), chunkQUI2X5JB_cjs.ErrorCode.TYPE, { op: "$get" }, { node, op: "$get", field: "path", form: true });
253
299
  }
254
300
  current2 = current2[evaluated];
255
301
  } else {
256
302
  const key = String(evaluated);
257
- if (!chunk66YSI4PX_cjs.isJsonObject(current2)) {
303
+ if (chunkQUI2X5JB_cjs.isJsonArray(current2) && chunkQUI2X5JB_cjs.INDEX_TEXT.test(key)) {
304
+ current2 = current2[Number(key)];
305
+ continue;
306
+ }
307
+ if (!chunkQUI2X5JB_cjs.isJsonObject(current2)) {
258
308
  if (fallback !== void 0) return evaluate2(fallback, context);
259
- throw new chunk66YSI4PX_cjs.PrismError("Expected object for key access", chunk66YSI4PX_cjs.ErrorCode.TYPE, { op: "$get" });
309
+ return chunkQUI2X5JB_cjs.failAt(getStopped(node, root, i + 1, context, evaluate2), chunkQUI2X5JB_cjs.ErrorCode.TYPE, { op: "$get" }, { node, op: "$get", field: "path", form: true });
260
310
  }
261
311
  current2 = current2[key];
262
312
  }
263
313
  }
264
314
  if (current2 === void 0) {
265
315
  if (fallback !== void 0) return evaluate2(fallback, context);
266
- throw new chunk66YSI4PX_cjs.PrismError("Path not found", chunk66YSI4PX_cjs.ErrorCode.MISSING_PATH, { op: "$get" });
316
+ return chunkQUI2X5JB_cjs.failAt("Path not found.", chunkQUI2X5JB_cjs.ErrorCode.MISSING_PATH, { op: "$get" }, { node, op: "$get", field: "path", form: true, detail: () => getStopped(node, root, path.length, context, evaluate2) });
267
317
  }
268
318
  return current2;
269
319
  };
@@ -272,7 +322,7 @@ var opWith = (node, context, evaluate2) => {
272
322
  const extendedVars = { ...context.vars };
273
323
  for (const [name, expr] of Object.entries(declarations)) {
274
324
  extendedVars[name] = evaluate2(expr, context);
275
- measure(extendedVars[name] ?? null, context.budget);
325
+ measure(extendedVars[name] ?? null, context.budget, node);
276
326
  }
277
327
  return evaluate2(value, { ...context, vars: extendedVars });
278
328
  };
@@ -295,15 +345,26 @@ var loopScope = (context) => {
295
345
  return { scope: { ...context, vars }, vars };
296
346
  };
297
347
 
348
+ // src/ops/refuse.ts
349
+ var writtenNote = (written) => chunkQUI2X5JB_cjs.isPlainObject(written) ? ` That object was written in the program as ${chunkQUI2X5JB_cjs.clip(JSON.stringify(written), 60)}, and an object without a "$" key builds an object; put the node itself there.` : "";
350
+ var TEXT_LIKE = "text, a number, true, false or null";
351
+ var isTextLike = (value) => value === null || typeof value !== "object";
352
+ var refuseType = ({ node, op, field, needs, got, written, contextOp }) => chunkQUI2X5JB_cjs.failAt(
353
+ `${field === void 0 ? op : `${op}.${field}`} needs ${needs}, but got ${chunkQUI2X5JB_cjs.describeValue(got)}.${writtenNote(written)}`,
354
+ chunkQUI2X5JB_cjs.ErrorCode.TYPE,
355
+ { op: contextOp ?? op },
356
+ { node, op, ...field === void 0 ? {} : { field }, form: true }
357
+ );
358
+
298
359
  // src/ops/array.ops.ts
299
- var requireArray = (value, op) => {
300
- if (!chunk66YSI4PX_cjs.isJsonArray(value))
301
- throw new chunk66YSI4PX_cjs.PrismError(`Expected array for ${op}`, chunk66YSI4PX_cjs.ErrorCode.TYPE, { op });
360
+ var requireArray = (value, node, op, field, written) => {
361
+ if (!chunkQUI2X5JB_cjs.isJsonArray(value))
362
+ return refuseType({ node, op, ...field === void 0 ? {} : { field }, needs: "a list", got: value, written, contextOp: field === void 0 ? op : `${op}.${field}` });
302
363
  return value;
303
364
  };
304
365
  var opMap = (node, context, evaluate2) => {
305
366
  const { over, as, body } = node.$map;
306
- const input = requireArray(evaluate2(over, context), "$map.over");
367
+ const input = requireArray(evaluate2(over, context), node, "$map", "over", over);
307
368
  const result = [];
308
369
  const { scope, vars } = loopScope(context);
309
370
  for (const item of input) {
@@ -314,7 +375,7 @@ var opMap = (node, context, evaluate2) => {
314
375
  };
315
376
  var opFilter = (node, context, evaluate2) => {
316
377
  const { over, as, when } = node.$filter;
317
- const input = requireArray(evaluate2(over, context), "$filter.over");
378
+ const input = requireArray(evaluate2(over, context), node, "$filter", "over", over);
318
379
  const result = [];
319
380
  const { scope, vars } = loopScope(context);
320
381
  for (const item of input) {
@@ -326,14 +387,14 @@ var opFilter = (node, context, evaluate2) => {
326
387
  };
327
388
  var opReduce = (node, context, evaluate2) => {
328
389
  const { over, as, acc: accName = "acc", init, body } = node.$reduce;
329
- const input = requireArray(evaluate2(over, context), "$reduce.over");
390
+ const input = requireArray(evaluate2(over, context), node, "$reduce", "over", over);
330
391
  let accumulator = evaluate2(init, context);
331
392
  const { scope, vars } = loopScope(context);
332
393
  for (const item of input) {
333
394
  vars[as] = item;
334
395
  vars[accName] = accumulator;
335
396
  accumulator = evaluate2(body, scope);
336
- measure(accumulator, context.budget);
397
+ measure(accumulator, context.budget, node);
337
398
  }
338
399
  return accumulator;
339
400
  };
@@ -341,20 +402,20 @@ var opSlice = (node, context, evaluate2) => {
341
402
  const { from, start, end } = node.$slice;
342
403
  const input = evaluate2(from, context);
343
404
  if (typeof input === "string") return input.slice(start, end);
344
- if (chunk66YSI4PX_cjs.isJsonArray(input)) return input.slice(start, end);
345
- throw new chunk66YSI4PX_cjs.PrismError("Expected array or string for $slice", chunk66YSI4PX_cjs.ErrorCode.TYPE, { op: "$slice" });
405
+ if (chunkQUI2X5JB_cjs.isJsonArray(input)) return input.slice(start, end);
406
+ return refuseType({ node, op: "$slice", field: "from", needs: "a list or text", got: input, written: from, contextOp: "$slice" });
346
407
  };
347
408
  var opFlatten = (node, context, evaluate2) => {
348
- const input = requireArray(evaluate2(node.$flatten, context), "$flatten");
409
+ const input = requireArray(evaluate2(node.$flatten, context), node, "$flatten", void 0, node.$flatten);
349
410
  const result = [];
350
411
  for (const item of input) {
351
- if (chunk66YSI4PX_cjs.isJsonArray(item)) result.push(...item);
412
+ if (chunkQUI2X5JB_cjs.isJsonArray(item)) result.push(...item);
352
413
  else result.push(item);
353
414
  }
354
415
  return result;
355
416
  };
356
417
  var opUnique = (node, context, evaluate2) => {
357
- const input = requireArray(evaluate2(node.$unique, context), "$unique");
418
+ const input = requireArray(evaluate2(node.$unique, context), node, "$unique", void 0, node.$unique);
358
419
  const seen = /* @__PURE__ */ new Set();
359
420
  const result = [];
360
421
  for (const item of input) {
@@ -366,12 +427,13 @@ var opUnique = (node, context, evaluate2) => {
366
427
  }
367
428
  return result;
368
429
  };
369
- var sortKey = (key) => {
430
+ var sortKey = (key, node) => {
370
431
  if (key !== null && typeof key === "object") {
371
- throw new chunk66YSI4PX_cjs.PrismError(
372
- `$sortBy.by answered ${Array.isArray(key) ? "a list" : "an object"}; a sort key is a string or a number. To sort by two keys, sort by the second and then by the first: the sort keeps the order of equal items.`,
373
- chunk66YSI4PX_cjs.ErrorCode.TYPE,
374
- { op: "$sortBy" }
432
+ return chunkQUI2X5JB_cjs.failAt(
433
+ `$sortBy.by answered ${chunkQUI2X5JB_cjs.describeValue(key)}; a sort key is a string or a number. To sort by two keys, sort by the second and then by the first: the sort keeps the order of equal items.`,
434
+ chunkQUI2X5JB_cjs.ErrorCode.TYPE,
435
+ { op: "$sortBy" },
436
+ { node, op: "$sortBy", field: "by", form: true }
375
437
  );
376
438
  }
377
439
  return key;
@@ -380,11 +442,11 @@ var NULL_KIND = 3;
380
442
  var kindOf = (key) => key === null ? NULL_KIND : typeof key === "number" ? 0 : typeof key === "string" ? 1 : 2;
381
443
  var opSortBy = (node, context, evaluate2) => {
382
444
  const { over, as, by, dir = "asc" } = node.$sortBy;
383
- const input = requireArray(evaluate2(over, context), "$sortBy.over");
445
+ const input = requireArray(evaluate2(over, context), node, "$sortBy", "over", over);
384
446
  const { scope, vars } = loopScope(context);
385
447
  const keyed = input.map((item) => {
386
448
  vars[as] = item;
387
- return { item, key: sortKey(evaluate2(by, scope)) };
449
+ return { item, key: sortKey(evaluate2(by, scope), node) };
388
450
  });
389
451
  keyed.sort((a, b) => {
390
452
  const kindA = kindOf(a.key);
@@ -397,33 +459,41 @@ var opSortBy = (node, context, evaluate2) => {
397
459
  };
398
460
 
399
461
  // src/ops/math.ops.ts
400
- var requireNumbers = (node, context, evaluate2, op) => {
401
- const a = evaluate2(node[0], context);
402
- const b = evaluate2(node[1], context);
403
- if (typeof a !== "number" || typeof b !== "number")
404
- throw new chunk66YSI4PX_cjs.PrismError(`Expected numbers for ${op}`, chunk66YSI4PX_cjs.ErrorCode.TYPE, { op });
462
+ var requireNumbers = (node, pair, context, evaluate2, op) => {
463
+ const a = evaluate2(pair[0], context);
464
+ const b = evaluate2(pair[1], context);
465
+ if (typeof a !== "number" || typeof b !== "number") {
466
+ const wrong = typeof a === "number" ? 1 : 0;
467
+ return chunkQUI2X5JB_cjs.failAt(
468
+ `${op} needs two numbers, but got ${chunkQUI2X5JB_cjs.describeValue(a)} and ${chunkQUI2X5JB_cjs.describeValue(b)}.${writtenNote(pair[wrong])}`,
469
+ chunkQUI2X5JB_cjs.ErrorCode.TYPE,
470
+ { op },
471
+ { node, op, field: String(wrong), form: true }
472
+ );
473
+ }
405
474
  return [a, b];
406
475
  };
476
+ var refuseZero = (node, op) => chunkQUI2X5JB_cjs.failAt(`Division by zero: the second value of ${op} is 0.`, chunkQUI2X5JB_cjs.ErrorCode.DIVISION_BY_ZERO, { op }, { node, op, field: "1", form: false });
407
477
  var opAdd = (node, context, evaluate2) => {
408
- const [a, b] = requireNumbers(node.$add, context, evaluate2, "$add");
478
+ const [a, b] = requireNumbers(node, node.$add, context, evaluate2, "$add");
409
479
  return a + b;
410
480
  };
411
481
  var opSub = (node, context, evaluate2) => {
412
- const [a, b] = requireNumbers(node.$sub, context, evaluate2, "$sub");
482
+ const [a, b] = requireNumbers(node, node.$sub, context, evaluate2, "$sub");
413
483
  return a - b;
414
484
  };
415
485
  var opMul = (node, context, evaluate2) => {
416
- const [a, b] = requireNumbers(node.$mul, context, evaluate2, "$mul");
486
+ const [a, b] = requireNumbers(node, node.$mul, context, evaluate2, "$mul");
417
487
  return a * b;
418
488
  };
419
489
  var opDiv = (node, context, evaluate2) => {
420
- const [a, b] = requireNumbers(node.$div, context, evaluate2, "$div");
421
- if (b === 0) throw new chunk66YSI4PX_cjs.PrismError("Division by zero", chunk66YSI4PX_cjs.ErrorCode.DIVISION_BY_ZERO, { op: "$div" });
490
+ const [a, b] = requireNumbers(node, node.$div, context, evaluate2, "$div");
491
+ if (b === 0) return refuseZero(node, "$div");
422
492
  return a / b;
423
493
  };
424
494
  var opMod = (node, context, evaluate2) => {
425
- const [a, b] = requireNumbers(node.$mod, context, evaluate2, "$mod");
426
- if (b === 0) throw new chunk66YSI4PX_cjs.PrismError("Division by zero", chunk66YSI4PX_cjs.ErrorCode.DIVISION_BY_ZERO, { op: "$mod" });
495
+ const [a, b] = requireNumbers(node, node.$mod, context, evaluate2, "$mod");
496
+ if (b === 0) return refuseZero(node, "$mod");
427
497
  const remainder = a % b;
428
498
  if (remainder === 0) return 0;
429
499
  return remainder < 0 !== b < 0 ? remainder + b : remainder;
@@ -431,19 +501,12 @@ var opMod = (node, context, evaluate2) => {
431
501
  var ROUNDING = { nearest: Math.round, floor: Math.floor, ceil: Math.ceil };
432
502
  var opRound = (node, context, evaluate2) => {
433
503
  const value = evaluate2(node.$round.value, context);
434
- if (typeof value !== "number")
435
- throw new chunk66YSI4PX_cjs.PrismError("Expected number for $round", chunk66YSI4PX_cjs.ErrorCode.TYPE, { op: "$round" });
504
+ if (typeof value !== "number") return refuseType({ node, op: "$round", field: "value", needs: "a number", got: value, written: node.$round.value });
436
505
  const digits = node.$round.digits ?? 0;
437
506
  const factor = Math.pow(10, digits);
438
507
  return ROUNDING[node.$round.mode ?? "nearest"](value * factor) / factor;
439
508
  };
440
509
  var NUMERIC_TEXT = /^[+-]?(\d+\.?\d*|\.\d+)([eE][+-]?\d+)?$/;
441
- var whatIs = (value) => {
442
- if (typeof value === "string") return JSON.stringify(value.slice(0, 40));
443
- if (value === null) return "null";
444
- if (Array.isArray(value)) return "a list";
445
- return typeof value === "object" ? "an object" : String(value);
446
- };
447
510
  var opToNumber = (node, context, evaluate2) => {
448
511
  const value = evaluate2(node.$toNumber.value, context);
449
512
  if (typeof value === "number") return value;
@@ -453,14 +516,22 @@ var opToNumber = (node, context, evaluate2) => {
453
516
  if (Number.isFinite(parsed)) return parsed;
454
517
  }
455
518
  if (node.$toNumber.fallback !== void 0) return evaluate2(node.$toNumber.fallback, context);
456
- throw new chunk66YSI4PX_cjs.PrismError(`Expected a number or numeric text for $toNumber, got ${whatIs(value)}`, chunk66YSI4PX_cjs.ErrorCode.TYPE, { op: "$toNumber" });
519
+ return refuseType({ node, op: "$toNumber", field: "value", needs: "a number or numeric text", got: value, written: node.$toNumber.value, contextOp: "$toNumber" });
457
520
  };
458
521
 
459
522
  // src/ops/string.ops.ts
460
523
  var opJoin = (node, context, evaluate2) => {
461
524
  const { parts, sep = "" } = node.$join;
462
525
  const list = evaluate2(parts, context);
463
- if (!Array.isArray(list)) throw new chunk66YSI4PX_cjs.PrismError("Expected $join.parts to evaluate to an array", chunk66YSI4PX_cjs.ErrorCode.TYPE, { op: "$join" });
526
+ if (!Array.isArray(list)) return refuseType({ node, op: "$join", field: "parts", needs: "a list", got: list, written: parts });
527
+ const wrong = list.findIndex((part) => !isTextLike(part));
528
+ if (wrong !== -1)
529
+ return chunkQUI2X5JB_cjs.failAt(
530
+ `$join.parts needs items that are ${TEXT_LIKE}, but item ${wrong} is ${chunkQUI2X5JB_cjs.describeValue(list[wrong])}.${Array.isArray(parts) ? writtenNote(parts[wrong]) : ""}`,
531
+ chunkQUI2X5JB_cjs.ErrorCode.TYPE,
532
+ { op: "$join" },
533
+ { node, op: "$join", field: "parts", form: true }
534
+ );
464
535
  return list.map((p) => String(p ?? "")).join(sep);
465
536
  };
466
537
  var opToString = (node, context, evaluate2) => {
@@ -472,17 +543,23 @@ var opToString = (node, context, evaluate2) => {
472
543
  var opInterpolate = (node, context, evaluate2) => {
473
544
  const { template, values: valuesNode } = node.$interpolate;
474
545
  const values = evaluate2(valuesNode, context);
475
- if (!chunk66YSI4PX_cjs.isJsonObject(values))
476
- throw new chunk66YSI4PX_cjs.PrismError("Expected object for $interpolate.values", chunk66YSI4PX_cjs.ErrorCode.TYPE, { op: "$interpolate" });
477
- return template.replace(
478
- /\{\{(\w+)\}\}/g,
479
- (_match, key) => String(values[key] ?? "")
480
- );
546
+ if (!chunkQUI2X5JB_cjs.isJsonObject(values)) return refuseType({ node, op: "$interpolate", field: "values", needs: "an object", got: values, written: valuesNode });
547
+ return template.replace(/\{\{(\w+)\}\}/g, (_match, key) => {
548
+ const value = values[key] ?? "";
549
+ if (!isTextLike(value))
550
+ return chunkQUI2X5JB_cjs.failAt(
551
+ `$interpolate.values needs ${TEXT_LIKE} for {{${key}}}, but "${key}" is ${chunkQUI2X5JB_cjs.describeValue(value)}.`,
552
+ chunkQUI2X5JB_cjs.ErrorCode.TYPE,
553
+ { op: "$interpolate" },
554
+ { node, op: "$interpolate", field: "values", form: true }
555
+ );
556
+ return String(value);
557
+ });
481
558
  };
482
559
  var fillOne = (value) => {
483
560
  if (value === null || typeof value !== "object" || Array.isArray(value)) return value;
484
561
  const { phrase, slots } = value;
485
- if (typeof phrase !== "string" || !chunk66YSI4PX_cjs.isJsonObject(slots)) return value;
562
+ if (typeof phrase !== "string" || !chunkQUI2X5JB_cjs.isJsonObject(slots)) return value;
486
563
  return phrase.replace(/\{([A-Za-z_][A-Za-z0-9_]*)\}/g, (hole, name) => {
487
564
  const slot = slots[name];
488
565
  if (slot === void 0 || slot === null) return hole;
@@ -493,32 +570,27 @@ var fillOne = (value) => {
493
570
  var opFill = (node, context, evaluate2) => fillOne(evaluate2(node.$fill, context));
494
571
  var opTrim = (node, context, evaluate2) => {
495
572
  const value = evaluate2(node.$trim, context);
496
- if (typeof value !== "string")
497
- throw new chunk66YSI4PX_cjs.PrismError("Expected string for $trim", chunk66YSI4PX_cjs.ErrorCode.TYPE, { op: "$trim" });
573
+ if (typeof value !== "string") return refuseType({ node, op: "$trim", needs: "text", got: value, written: node.$trim });
498
574
  return value.trim();
499
575
  };
500
576
  var opLower = (node, context, evaluate2) => {
501
577
  const value = evaluate2(node.$lower, context);
502
- if (typeof value !== "string")
503
- throw new chunk66YSI4PX_cjs.PrismError("Expected string for $lower", chunk66YSI4PX_cjs.ErrorCode.TYPE, { op: "$lower" });
578
+ if (typeof value !== "string") return refuseType({ node, op: "$lower", needs: "text", got: value, written: node.$lower });
504
579
  return value.toLowerCase();
505
580
  };
506
581
  var opUpper = (node, context, evaluate2) => {
507
582
  const value = evaluate2(node.$upper, context);
508
- if (typeof value !== "string")
509
- throw new chunk66YSI4PX_cjs.PrismError("Expected string for $upper", chunk66YSI4PX_cjs.ErrorCode.TYPE, { op: "$upper" });
583
+ if (typeof value !== "string") return refuseType({ node, op: "$upper", needs: "text", got: value, written: node.$upper });
510
584
  return value.toUpperCase();
511
585
  };
512
586
  var opSplit = (node, context, evaluate2) => {
513
587
  const value = evaluate2(node.$split.value, context);
514
- if (typeof value !== "string")
515
- throw new chunk66YSI4PX_cjs.PrismError("Expected string for $split", chunk66YSI4PX_cjs.ErrorCode.TYPE, { op: "$split" });
588
+ if (typeof value !== "string") return refuseType({ node, op: "$split", field: "value", needs: "text", got: value, written: node.$split.value });
516
589
  return value.split(node.$split.sep);
517
590
  };
518
591
  var opReplace = (node, context, evaluate2) => {
519
592
  const value = evaluate2(node.$replace.value, context);
520
- if (typeof value !== "string")
521
- throw new chunk66YSI4PX_cjs.PrismError("Expected string for $replace", chunk66YSI4PX_cjs.ErrorCode.TYPE, { op: "$replace" });
593
+ if (typeof value !== "string") return refuseType({ node, op: "$replace", field: "value", needs: "text", got: value, written: node.$replace.value });
522
594
  const { search, replacement, all } = node.$replace;
523
595
  return all === true ? value.replaceAll(search, replacement) : value.replace(search, replacement);
524
596
  };
@@ -534,45 +606,52 @@ var opNeq = (node, context, evaluate2) => {
534
606
  const b = evaluate2(node.$neq[1], context);
535
607
  return !jsonEqual(a, b);
536
608
  };
537
- var compareOp = (pair, context, evaluate2, op, test) => {
609
+ var compareOp = (node, pair, context, evaluate2, op, test) => {
538
610
  const a = evaluate2(pair[0], context);
539
611
  const b = evaluate2(pair[1], context);
540
612
  const cmp = compare(a, b);
541
- if (cmp === void 0)
542
- throw new chunk66YSI4PX_cjs.PrismError(`Cannot compare values for ${op}`, chunk66YSI4PX_cjs.ErrorCode.TYPE, { op });
613
+ if (cmp === void 0) {
614
+ const wrong = typeof a === "number" || typeof a === "string" ? 1 : 0;
615
+ return chunkQUI2X5JB_cjs.failAt(
616
+ `${op} needs two numbers or two texts, but got ${chunkQUI2X5JB_cjs.describeValue(a)} and ${chunkQUI2X5JB_cjs.describeValue(b)}.${writtenNote(pair[wrong])}`,
617
+ chunkQUI2X5JB_cjs.ErrorCode.TYPE,
618
+ { op },
619
+ { node, op, field: String(wrong), form: true }
620
+ );
621
+ }
543
622
  return test(cmp);
544
623
  };
545
- var opGt = (node, ctx, ev) => compareOp(node.$gt, ctx, ev, "$gt", (c) => c > 0);
546
- var opGte = (node, ctx, ev) => compareOp(node.$gte, ctx, ev, "$gte", (c) => c >= 0);
547
- var opLt = (node, ctx, ev) => compareOp(node.$lt, ctx, ev, "$lt", (c) => c < 0);
548
- var opLte = (node, ctx, ev) => compareOp(node.$lte, ctx, ev, "$lte", (c) => c <= 0);
624
+ var opGt = (node, ctx, ev) => compareOp(node, node.$gt, ctx, ev, "$gt", (c) => c > 0);
625
+ var opGte = (node, ctx, ev) => compareOp(node, node.$gte, ctx, ev, "$gte", (c) => c >= 0);
626
+ var opLt = (node, ctx, ev) => compareOp(node, node.$lt, ctx, ev, "$lt", (c) => c < 0);
627
+ var opLte = (node, ctx, ev) => compareOp(node, node.$lte, ctx, ev, "$lte", (c) => c <= 0);
549
628
  var opEmpty = (node, context, evaluate2) => {
550
629
  const value = evaluate2(node.$empty, context);
551
630
  if (value === null || value === void 0) return true;
552
631
  if (typeof value === "string") return value.length === 0;
553
- if (chunk66YSI4PX_cjs.isJsonArray(value)) return value.length === 0;
554
- if (chunk66YSI4PX_cjs.isJsonObject(value)) return Object.keys(value).length === 0;
632
+ if (chunkQUI2X5JB_cjs.isJsonArray(value)) return value.length === 0;
633
+ if (chunkQUI2X5JB_cjs.isJsonObject(value)) return Object.keys(value).length === 0;
555
634
  return false;
556
635
  };
557
636
  var opStartsWith = (node, context, evaluate2) => {
558
637
  const value = evaluate2(node.$startsWith.value, context);
559
638
  const prefix = evaluate2(node.$startsWith.prefix, context);
560
- if (typeof value !== "string" || typeof prefix !== "string")
561
- throw new chunk66YSI4PX_cjs.PrismError("Expected strings for $startsWith", chunk66YSI4PX_cjs.ErrorCode.TYPE, { op: "$startsWith" });
639
+ if (typeof value !== "string") return refuseType({ node, op: "$startsWith", field: "value", needs: "text", got: value, written: node.$startsWith.value });
640
+ if (typeof prefix !== "string") return refuseType({ node, op: "$startsWith", field: "prefix", needs: "text", got: prefix, written: node.$startsWith.prefix });
562
641
  return value.startsWith(prefix);
563
642
  };
564
643
  var opEndsWith = (node, context, evaluate2) => {
565
644
  const value = evaluate2(node.$endsWith.value, context);
566
645
  const suffix = evaluate2(node.$endsWith.suffix, context);
567
- if (typeof value !== "string" || typeof suffix !== "string")
568
- throw new chunk66YSI4PX_cjs.PrismError("Expected strings for $endsWith", chunk66YSI4PX_cjs.ErrorCode.TYPE, { op: "$endsWith" });
646
+ if (typeof value !== "string") return refuseType({ node, op: "$endsWith", field: "value", needs: "text", got: value, written: node.$endsWith.value });
647
+ if (typeof suffix !== "string") return refuseType({ node, op: "$endsWith", field: "suffix", needs: "text", got: suffix, written: node.$endsWith.suffix });
569
648
  return value.endsWith(suffix);
570
649
  };
571
650
  var opContains = (node, context, evaluate2) => {
572
651
  const value = evaluate2(node.$contains.value, context);
573
652
  const search = evaluate2(node.$contains.search, context);
574
- if (typeof value !== "string" || typeof search !== "string")
575
- throw new chunk66YSI4PX_cjs.PrismError("Expected strings for $contains", chunk66YSI4PX_cjs.ErrorCode.TYPE, { op: "$contains" });
653
+ if (typeof value !== "string") return refuseType({ node, op: "$contains", field: "value", needs: "text", got: value, written: node.$contains.value });
654
+ if (typeof search !== "string") return refuseType({ node, op: "$contains", field: "search", needs: "text", got: search, written: node.$contains.search });
576
655
  return value.includes(search);
577
656
  };
578
657
 
@@ -598,10 +677,10 @@ var opOr = (node, context, evaluate2) => {
598
677
  // src/ops/structure.ops.ts
599
678
  var opMerge = (node, context, evaluate2) => {
600
679
  const result = {};
601
- for (const part of node.$merge) {
680
+ for (let index = 0; index < node.$merge.length; index++) {
681
+ const part = node.$merge[index];
602
682
  const evaluated = evaluate2(part, context);
603
- if (!chunk66YSI4PX_cjs.isJsonObject(evaluated))
604
- throw new chunk66YSI4PX_cjs.PrismError("Expected object for $merge element", chunk66YSI4PX_cjs.ErrorCode.TYPE, { op: "$merge" });
683
+ if (!chunkQUI2X5JB_cjs.isJsonObject(evaluated)) return refuseType({ node, op: "$merge", field: String(index), needs: "an object", got: evaluated, written: part, contextOp: "$merge" });
605
684
  Object.assign(result, evaluated);
606
685
  }
607
686
  return result;
@@ -623,20 +702,19 @@ var opCase = (node, context, evaluate2) => {
623
702
  };
624
703
  var opEntriesOf = (node, context, evaluate2) => {
625
704
  const value = evaluate2(node.$entriesOf, context);
626
- if (!chunk66YSI4PX_cjs.isJsonObject(value))
627
- throw new chunk66YSI4PX_cjs.PrismError("Expected object for $entriesOf", chunk66YSI4PX_cjs.ErrorCode.TYPE, { op: "$entriesOf" });
705
+ if (!chunkQUI2X5JB_cjs.isJsonObject(value)) return refuseType({ node, op: "$entriesOf", needs: "an object", got: value, written: node.$entriesOf });
628
706
  return Object.entries(value).map(([k, v]) => [k, v]);
629
707
  };
630
708
  var opKeyBy = (node, context, evaluate2) => {
631
709
  const { over, as, key: keyExpr } = node.$keyBy;
632
710
  const input = evaluate2(over, context);
633
- if (!chunk66YSI4PX_cjs.isJsonArray(input))
634
- throw new chunk66YSI4PX_cjs.PrismError("Expected array for $keyBy.over", chunk66YSI4PX_cjs.ErrorCode.TYPE, { op: "$keyBy" });
711
+ if (!chunkQUI2X5JB_cjs.isJsonArray(input)) return refuseType({ node, op: "$keyBy", field: "over", needs: "a list", got: input, written: over, contextOp: "$keyBy" });
635
712
  const result = {};
636
713
  const { scope, vars } = loopScope(context);
637
714
  for (const item of input) {
638
715
  vars[as] = item;
639
716
  const key = evaluate2(keyExpr, scope);
717
+ if (!isTextLike(key)) return refuseType({ node, op: "$keyBy", field: "key", needs: TEXT_LIKE, got: key, written: keyExpr, contextOp: "$keyBy" });
640
718
  result[String(key)] = item;
641
719
  }
642
720
  return result;
@@ -644,13 +722,14 @@ var opKeyBy = (node, context, evaluate2) => {
644
722
  var opGroupBy = (node, context, evaluate2) => {
645
723
  const { over, as, key: keyExpr } = node.$groupBy;
646
724
  const input = evaluate2(over, context);
647
- if (!chunk66YSI4PX_cjs.isJsonArray(input))
648
- throw new chunk66YSI4PX_cjs.PrismError("Expected array for $groupBy.over", chunk66YSI4PX_cjs.ErrorCode.TYPE, { op: "$groupBy" });
725
+ if (!chunkQUI2X5JB_cjs.isJsonArray(input)) return refuseType({ node, op: "$groupBy", field: "over", needs: "a list", got: input, written: over, contextOp: "$groupBy" });
649
726
  const result = {};
650
727
  const { scope, vars } = loopScope(context);
651
728
  for (const item of input) {
652
729
  vars[as] = item;
653
- const key = String(evaluate2(keyExpr, scope));
730
+ const value = evaluate2(keyExpr, scope);
731
+ if (!isTextLike(value)) return refuseType({ node, op: "$groupBy", field: "key", needs: TEXT_LIKE, got: value, written: keyExpr, contextOp: "$groupBy" });
732
+ const key = String(value);
654
733
  if (!result[key]) result[key] = [];
655
734
  result[key].push(item);
656
735
  }
@@ -660,32 +739,29 @@ var opGroupBy = (node, context, evaluate2) => {
660
739
  // src/ops/object.ops.ts
661
740
  var opKeys = (node, context, evaluate2) => {
662
741
  const value = evaluate2(node.$keys, context);
663
- if (!chunk66YSI4PX_cjs.isJsonObject(value))
664
- throw new chunk66YSI4PX_cjs.PrismError("Expected object for $keys", chunk66YSI4PX_cjs.ErrorCode.TYPE, { op: "$keys" });
742
+ if (!chunkQUI2X5JB_cjs.isJsonObject(value)) return refuseType({ node, op: "$keys", needs: "an object", got: value, written: node.$keys });
665
743
  return Object.keys(value);
666
744
  };
667
745
  var opValues = (node, context, evaluate2) => {
668
746
  const value = evaluate2(node.$values, context);
669
- if (!chunk66YSI4PX_cjs.isJsonObject(value))
670
- throw new chunk66YSI4PX_cjs.PrismError("Expected object for $values", chunk66YSI4PX_cjs.ErrorCode.TYPE, { op: "$values" });
747
+ if (!chunkQUI2X5JB_cjs.isJsonObject(value)) return refuseType({ node, op: "$values", needs: "an object", got: value, written: node.$values });
671
748
  return Object.values(value);
672
749
  };
673
750
  var opFromEntries = (node, context, evaluate2) => {
674
751
  const value = evaluate2(node.$fromEntries, context);
675
- if (!chunk66YSI4PX_cjs.isJsonArray(value))
676
- throw new chunk66YSI4PX_cjs.PrismError("Expected array for $fromEntries", chunk66YSI4PX_cjs.ErrorCode.TYPE, { op: "$fromEntries" });
752
+ if (!chunkQUI2X5JB_cjs.isJsonArray(value)) return refuseType({ node, op: "$fromEntries", needs: "a list of [key, value] pairs", got: value, written: node.$fromEntries });
677
753
  const result = {};
678
- for (const entry of value) {
679
- if (!chunk66YSI4PX_cjs.isJsonArray(entry) || entry.length < 2)
680
- throw new chunk66YSI4PX_cjs.PrismError("Expected [key, value] pairs for $fromEntries", chunk66YSI4PX_cjs.ErrorCode.TYPE, { op: "$fromEntries" });
754
+ for (let index = 0; index < value.length; index++) {
755
+ const entry = value[index];
756
+ if (!chunkQUI2X5JB_cjs.isJsonArray(entry) || entry.length < 2)
757
+ return chunkQUI2X5JB_cjs.failAt(`$fromEntries needs a list of [key, value] pairs, but item ${index} is ${chunkQUI2X5JB_cjs.describeValue(entry)}.`, chunkQUI2X5JB_cjs.ErrorCode.TYPE, { op: "$fromEntries" }, { node, op: "$fromEntries", form: true });
681
758
  result[String(entry[0])] = entry[1];
682
759
  }
683
760
  return result;
684
761
  };
685
762
  var opPick = (node, context, evaluate2) => {
686
763
  const value = evaluate2(node.$pick.from, context);
687
- if (!chunk66YSI4PX_cjs.isJsonObject(value))
688
- throw new chunk66YSI4PX_cjs.PrismError("Expected object for $pick", chunk66YSI4PX_cjs.ErrorCode.TYPE, { op: "$pick" });
764
+ if (!chunkQUI2X5JB_cjs.isJsonObject(value)) return refuseType({ node, op: "$pick", field: "from", needs: "an object", got: value, written: node.$pick.from, contextOp: "$pick" });
689
765
  const result = {};
690
766
  for (const key of node.$pick.keys) {
691
767
  if (key in value) result[key] = value[key];
@@ -694,8 +770,7 @@ var opPick = (node, context, evaluate2) => {
694
770
  };
695
771
  var opOmit = (node, context, evaluate2) => {
696
772
  const value = evaluate2(node.$omit.from, context);
697
- if (!chunk66YSI4PX_cjs.isJsonObject(value))
698
- throw new chunk66YSI4PX_cjs.PrismError("Expected object for $omit", chunk66YSI4PX_cjs.ErrorCode.TYPE, { op: "$omit" });
773
+ if (!chunkQUI2X5JB_cjs.isJsonObject(value)) return refuseType({ node, op: "$omit", field: "from", needs: "an object", got: value, written: node.$omit.from, contextOp: "$omit" });
699
774
  const keysToOmit = new Set(node.$omit.keys);
700
775
  const result = {};
701
776
  for (const [k, v] of Object.entries(value)) {
@@ -706,14 +781,14 @@ var opOmit = (node, context, evaluate2) => {
706
781
  var opType = (node, context, evaluate2) => {
707
782
  const value = evaluate2(node.$type, context);
708
783
  if (value === null) return "null";
709
- if (chunk66YSI4PX_cjs.isJsonArray(value)) return "array";
784
+ if (chunkQUI2X5JB_cjs.isJsonArray(value)) return "array";
710
785
  return typeof value;
711
786
  };
712
787
  var opLength = (node, context, evaluate2) => {
713
788
  const value = evaluate2(node.$length, context);
714
789
  if (typeof value === "string") return value.length;
715
- if (chunk66YSI4PX_cjs.isJsonArray(value)) return value.length;
716
- throw new chunk66YSI4PX_cjs.PrismError("Expected array or string for $length", chunk66YSI4PX_cjs.ErrorCode.TYPE, { op: "$length" });
790
+ if (chunkQUI2X5JB_cjs.isJsonArray(value)) return value.length;
791
+ return refuseType({ node, op: "$length", needs: "a list or text", got: value, written: node.$length });
717
792
  };
718
793
 
719
794
  // src/ops/transform.ops.ts
@@ -721,10 +796,10 @@ var has = (value, path) => {
721
796
  let at = value;
722
797
  for (const segment of path) {
723
798
  if (typeof segment === "number") {
724
- if (!chunk66YSI4PX_cjs.isJsonArray(at) || segment >= at.length) return false;
799
+ if (!chunkQUI2X5JB_cjs.isJsonArray(at) || segment >= at.length) return false;
725
800
  at = at[segment] ?? null;
726
801
  } else {
727
- if (!chunk66YSI4PX_cjs.isJsonObject(at) || !Object.prototype.hasOwnProperty.call(at, segment)) return false;
802
+ if (!chunkQUI2X5JB_cjs.isJsonObject(at) || !Object.prototype.hasOwnProperty.call(at, segment)) return false;
728
803
  at = at[segment] ?? null;
729
804
  }
730
805
  }
@@ -733,7 +808,7 @@ var has = (value, path) => {
733
808
  var opHas = (node, context, evaluate2) => has(evaluate2(node.$has.from, context), node.$has.path);
734
809
  var opRenameKeys = (node, context, evaluate2) => {
735
810
  const value = evaluate2(node.$renameKeys.from, context);
736
- if (!chunk66YSI4PX_cjs.isJsonObject(value)) throw new chunk66YSI4PX_cjs.PrismError("Expected object for $renameKeys", chunk66YSI4PX_cjs.ErrorCode.TYPE, { op: "$renameKeys" });
811
+ if (!chunkQUI2X5JB_cjs.isJsonObject(value)) return refuseType({ node, op: "$renameKeys", field: "from", needs: "an object", got: value, written: node.$renameKeys.from, contextOp: "$renameKeys" });
737
812
  const { map } = node.$renameKeys;
738
813
  const renamedTo = new Set(Object.entries(map).filter(([from]) => Object.prototype.hasOwnProperty.call(value, from)).map(([, to]) => to));
739
814
  const result = {};
@@ -747,31 +822,31 @@ var opRenameKeys = (node, context, evaluate2) => {
747
822
  var current = (value, path) => {
748
823
  let at = value;
749
824
  for (const segment of path) {
750
- if (typeof segment === "number") at = chunk66YSI4PX_cjs.isJsonArray(at) ? at[segment] ?? null : null;
751
- else at = chunk66YSI4PX_cjs.isJsonObject(at) ? at[segment] ?? null : null;
825
+ if (typeof segment === "number") at = chunkQUI2X5JB_cjs.isJsonArray(at) ? at[segment] ?? null : null;
826
+ else at = chunkQUI2X5JB_cjs.isJsonObject(at) ? at[segment] ?? null : null;
752
827
  }
753
828
  return at;
754
829
  };
755
- var setIn = (value, path, next, full) => {
830
+ var setIn = (value, path, next, full, node) => {
756
831
  const [head, ...rest] = path;
757
832
  if (head === void 0) return next;
758
833
  if (typeof head === "number") {
759
- if (!chunk66YSI4PX_cjs.isJsonArray(value) || head >= value.length) {
760
- throw new chunk66YSI4PX_cjs.PrismError(`No array item at ${full.join(".")}`, chunk66YSI4PX_cjs.ErrorCode.MISSING_PATH, { op: "$update", path: full.join(".") });
834
+ if (!chunkQUI2X5JB_cjs.isJsonArray(value) || head >= value.length) {
835
+ return chunkQUI2X5JB_cjs.failAt(`No array item at ${full.join(".")}: the value there is ${chunkQUI2X5JB_cjs.describeValue(value)}.`, chunkQUI2X5JB_cjs.ErrorCode.MISSING_PATH, { op: "$update", path: full.join(".") }, { node, op: "$update", field: "path", form: false });
761
836
  }
762
- return value.map((item, i) => i === head ? setIn(item, rest, next, full) : item);
837
+ return value.map((item, i) => i === head ? setIn(item, rest, next, full, node) : item);
763
838
  }
764
- const base = chunk66YSI4PX_cjs.isJsonObject(value) ? value : {};
765
- return { ...base, [head]: setIn(base[head] ?? null, rest, next, full) };
839
+ const base = chunkQUI2X5JB_cjs.isJsonObject(value) ? value : {};
840
+ return { ...base, [head]: setIn(base[head] ?? null, rest, next, full, node) };
766
841
  };
767
842
  var opUpdate = (node, context, evaluate2) => {
768
843
  const { from, path, value, as } = node.$update;
769
844
  const source = evaluate2(from, context);
770
845
  const next = evaluate2(value, { ...context, vars: { ...context.vars, [as ?? "current"]: current(source, path) } });
771
- return setIn(source, path, next, path);
846
+ return setIn(source, path, next, path, node);
772
847
  };
773
848
  var opAssert = (node, context, evaluate2) => {
774
- if (!evaluate2(node.$assert.when, context)) throw new chunk66YSI4PX_cjs.PrismError(node.$assert.message, chunk66YSI4PX_cjs.ErrorCode.ASSERT, { op: "$assert" });
849
+ if (!evaluate2(node.$assert.when, context)) throw new chunkQUI2X5JB_cjs.PrismError(node.$assert.message, chunkQUI2X5JB_cjs.ErrorCode.ASSERT, { op: "$assert" });
775
850
  return evaluate2(node.$assert.value, context);
776
851
  };
777
852
  var opWalk = (node, context, evaluate2) => {
@@ -784,8 +859,8 @@ var opWalk = (node, context, evaluate2) => {
784
859
  return { matched: false, value };
785
860
  };
786
861
  const children = (value, visit) => {
787
- if (chunk66YSI4PX_cjs.isJsonArray(value)) return value.map(visit);
788
- if (chunk66YSI4PX_cjs.isJsonObject(value)) return Object.fromEntries(Object.entries(value).map(([k, v]) => [k, visit(v)]));
862
+ if (chunkQUI2X5JB_cjs.isJsonArray(value)) return value.map(visit);
863
+ if (chunkQUI2X5JB_cjs.isJsonObject(value)) return Object.fromEntries(Object.entries(value).map(([k, v]) => [k, visit(v)]));
789
864
  return value;
790
865
  };
791
866
  const post = (value) => rewrite(children(value, post)).value;
@@ -795,32 +870,32 @@ var opWalk = (node, context, evaluate2) => {
795
870
  dayjs__default.default.extend(utc__default.default);
796
871
  var DATE_ONLY = /^\d{4}-\d{2}-\d{2}$/;
797
872
  var isDateOnly = (value) => typeof value === "string" && DATE_ONLY.test(value);
798
- var parseDate = (value, op) => {
873
+ var parseDate = (value, node, op, field, written, contextOp) => {
799
874
  if (typeof value !== "string" && typeof value !== "number")
800
- throw new chunk66YSI4PX_cjs.PrismError(`Expected string or number for ${op}`, chunk66YSI4PX_cjs.ErrorCode.TYPE, { op });
875
+ return refuseType({ node, op, field, needs: "a date, as text or a number", got: value, written, contextOp });
801
876
  const d = isDateOnly(value) ? dayjs__default.default.utc(value) : dayjs__default.default(value);
802
877
  if (!d.isValid())
803
- throw new chunk66YSI4PX_cjs.PrismError(`Invalid date for ${op}: ${String(value)}`, chunk66YSI4PX_cjs.ErrorCode.DATE_INVALID, { op });
878
+ return chunkQUI2X5JB_cjs.failAt(`Invalid date for ${contextOp}: ${chunkQUI2X5JB_cjs.clip(String(value), 60)}`, chunkQUI2X5JB_cjs.ErrorCode.DATE_INVALID, { op: contextOp }, { node, op, field, form: false });
804
879
  return d;
805
880
  };
806
881
  var opDate = (node, context, evaluate2) => {
807
882
  const value = evaluate2(node.$date.value, context);
808
- const d = parseDate(value, "$date");
883
+ const d = parseDate(value, node, "$date", "value", node.$date.value, "$date");
809
884
  const instance = node.$date.utc ? d.utc() : d;
810
885
  if (node.$date.format) return instance.format(node.$date.format);
811
886
  return instance.toISOString();
812
887
  };
813
888
  var opDateAdd = (node, context, evaluate2) => {
814
889
  const value = evaluate2(node.$dateAdd.date, context);
815
- const d = parseDate(value, "$dateAdd");
890
+ const d = parseDate(value, node, "$dateAdd", "date", node.$dateAdd.date, "$dateAdd");
816
891
  const moved = d.add(node.$dateAdd.amount, node.$dateAdd.unit);
817
892
  return isDateOnly(value) ? moved.format("YYYY-MM-DD") : moved.toISOString();
818
893
  };
819
894
  var opDateDiff = (node, context, evaluate2) => {
820
895
  const from = evaluate2(node.$dateDiff.from, context);
821
896
  const to = evaluate2(node.$dateDiff.to, context);
822
- const fromDate = parseDate(from, "$dateDiff.from");
823
- const toDate = parseDate(to, "$dateDiff.to");
897
+ const fromDate = parseDate(from, node, "$dateDiff", "from", node.$dateDiff.from, "$dateDiff.from");
898
+ const toDate = parseDate(to, node, "$dateDiff", "to", node.$dateDiff.to, "$dateDiff.to");
824
899
  return toDate.diff(fromDate, node.$dateDiff.unit);
825
900
  };
826
901
 
@@ -828,9 +903,9 @@ var opDateDiff = (node, context, evaluate2) => {
828
903
  var DATE_FORMATTERS = /* @__PURE__ */ new Map();
829
904
  var NUMBER_FORMATTERS = /* @__PURE__ */ new Map();
830
905
  var DATE_ONLY2 = /^\d{4}-\d{2}-\d{2}$/;
831
- var localeOf = (value, op) => {
906
+ var localeOf = (value, node, op, written) => {
832
907
  if (typeof value !== "string" || value.trim() === "")
833
- throw new chunk66YSI4PX_cjs.PrismError(`${op} needs a locale tag (e.g. "de-AT")`, chunk66YSI4PX_cjs.ErrorCode.TYPE, { op });
908
+ return refuseType({ node, op, field: "locale", needs: 'a locale tag such as "de-AT"', got: value, written, contextOp: op });
834
909
  return value;
835
910
  };
836
911
  var isBlank = (value) => value === null || value === void 0 || value === "";
@@ -839,16 +914,16 @@ var fallbackOf = (raw, context, evaluate2) => {
839
914
  const resolved = evaluate2(raw, context);
840
915
  return resolved === null || resolved === void 0 ? "" : resolved;
841
916
  };
842
- var numberOf = (value, op) => {
917
+ var numberOf = (value, node, op, written) => {
843
918
  const parsed = typeof value === "number" ? value : Number(value);
844
- if (!Number.isFinite(parsed)) throw new chunk66YSI4PX_cjs.PrismError(`${op} expected a number, got ${JSON.stringify(value)}`, chunk66YSI4PX_cjs.ErrorCode.TYPE, { op });
919
+ if (!Number.isFinite(parsed)) return refuseType({ node, op, field: "value", needs: "a number or numeric text", got: value, written, contextOp: op });
845
920
  return parsed;
846
921
  };
847
922
  var opLocaleDate = (node, context, evaluate2) => {
848
923
  const spec = node.$localeDate;
849
924
  const value = evaluate2(spec.value, context);
850
925
  if (isBlank(value)) return fallbackOf(spec.fallback, context, evaluate2);
851
- const locale = localeOf(evaluate2(spec.locale, context), "$localeDate");
926
+ const locale = localeOf(evaluate2(spec.locale, context), node, "$localeDate", spec.locale);
852
927
  const authored = spec.options ?? { dateStyle: "medium" };
853
928
  const dateOnly = typeof value === "string" && DATE_ONLY2.test(value);
854
929
  const options = {
@@ -857,7 +932,7 @@ var opLocaleDate = (node, context, evaluate2) => {
857
932
  };
858
933
  const parsed = typeof value === "number" ? new Date(value) : new Date(String(value));
859
934
  if (Number.isNaN(parsed.getTime()))
860
- throw new chunk66YSI4PX_cjs.PrismError(`Invalid date for $localeDate: ${String(value)}`, chunk66YSI4PX_cjs.ErrorCode.DATE_INVALID, { op: "$localeDate" });
935
+ return chunkQUI2X5JB_cjs.failAt(`Invalid date for $localeDate: ${chunkQUI2X5JB_cjs.clip(String(value), 60)}`, chunkQUI2X5JB_cjs.ErrorCode.DATE_INVALID, { op: "$localeDate" }, { node, op: "$localeDate", field: "value", form: false });
861
936
  const key = `${locale}\0${JSON.stringify(options)}`;
862
937
  let formatter = DATE_FORMATTERS.get(key);
863
938
  if (formatter === void 0) {
@@ -883,12 +958,12 @@ var opLocaleMoney = (node, context, evaluate2) => {
883
958
  const spec = node.$localeMoney;
884
959
  const value = evaluate2(spec.value, context);
885
960
  if (isBlank(value)) return fallbackOf(spec.fallback, context, evaluate2);
886
- const locale = localeOf(evaluate2(spec.locale, context), "$localeMoney");
961
+ const locale = localeOf(evaluate2(spec.locale, context), node, "$localeMoney", spec.locale);
887
962
  const rawCurrency = evaluate2(spec.currency, context);
888
963
  if (typeof rawCurrency !== "string" || rawCurrency.trim() === "")
889
- throw new chunk66YSI4PX_cjs.PrismError("$localeMoney needs an ISO-4217 currency code", chunk66YSI4PX_cjs.ErrorCode.TYPE, { op: "$localeMoney" });
964
+ return refuseType({ node, op: "$localeMoney", field: "currency", needs: 'an ISO-4217 currency code such as "EUR"', got: rawCurrency, written: spec.currency, contextOp: "$localeMoney" });
890
965
  const currency = rawCurrency.toUpperCase();
891
- const amount = numberOf(value, "$localeMoney");
966
+ const amount = numberOf(value, node, "$localeMoney", spec.value);
892
967
  const major = spec.minorUnits === false ? amount : amount / minorUnitDivisor(locale, currency);
893
968
  const options = {
894
969
  style: "currency",
@@ -911,7 +986,7 @@ var opLocaleNumber = (node, context, evaluate2) => {
911
986
  const spec = node.$localeNumber;
912
987
  const value = evaluate2(spec.value, context);
913
988
  if (isBlank(value)) return fallbackOf(spec.fallback, context, evaluate2);
914
- const locale = localeOf(evaluate2(spec.locale, context), "$localeNumber");
989
+ const locale = localeOf(evaluate2(spec.locale, context), node, "$localeNumber", spec.locale);
915
990
  const options = {
916
991
  style: spec.style ?? "decimal",
917
992
  ...spec.digits === void 0 ? {} : { maximumFractionDigits: spec.digits },
@@ -924,7 +999,7 @@ var opLocaleNumber = (node, context, evaluate2) => {
924
999
  formatter = new Intl.NumberFormat(locale, options);
925
1000
  NUMBER_FORMATTERS.set(key, formatter);
926
1001
  }
927
- return formatter.format(numberOf(value, "$localeNumber"));
1002
+ return formatter.format(numberOf(value, node, "$localeNumber", spec.value));
928
1003
  };
929
1004
 
930
1005
  // src/engine/optimize.ts
@@ -939,78 +1014,77 @@ var attachNonEnumerable = (target, key, value) => {
939
1014
  configurable: true
940
1015
  });
941
1016
  };
942
- var isPlainObject2 = (value) => chunk66YSI4PX_cjs.isJsonObject(value);
943
1017
  var freezeConstant = (value) => {
944
1018
  if (value === null || typeof value !== "object" || Object.isFrozen(value)) return;
945
1019
  Object.freeze(value);
946
1020
  for (const inner of Object.values(value)) freezeConstant(inner);
947
1021
  };
948
1022
  var resolveHandler = (node) => {
949
- if (chunk66YSI4PX_cjs.isRefNode(node)) return eraseOp(opRef);
950
- if (chunk66YSI4PX_cjs.isConstNode(node)) return eraseOp(opConst);
951
- if (chunk66YSI4PX_cjs.isVarNode(node)) return eraseOp(opVar);
952
- if (chunk66YSI4PX_cjs.isGetNode(node)) return eraseOp(opGet);
953
- if (chunk66YSI4PX_cjs.isWithNode(node)) return eraseOp(opWith);
954
- if (chunk66YSI4PX_cjs.isMapNode(node)) return eraseOp(opMap);
955
- if (chunk66YSI4PX_cjs.isFilterNode(node)) return eraseOp(opFilter);
956
- if (chunk66YSI4PX_cjs.isReduceNode(node)) return eraseOp(opReduce);
957
- if (chunk66YSI4PX_cjs.isSliceNode(node)) return eraseOp(opSlice);
958
- if (chunk66YSI4PX_cjs.isFlattenNode(node)) return eraseOp(opFlatten);
959
- if (chunk66YSI4PX_cjs.isUniqueNode(node)) return eraseOp(opUnique);
960
- if (chunk66YSI4PX_cjs.isSortByNode(node)) return eraseOp(opSortBy);
961
- if (chunk66YSI4PX_cjs.isAddNode(node)) return eraseOp(opAdd);
962
- if (chunk66YSI4PX_cjs.isSubNode(node)) return eraseOp(opSub);
963
- if (chunk66YSI4PX_cjs.isMulNode(node)) return eraseOp(opMul);
964
- if (chunk66YSI4PX_cjs.isDivNode(node)) return eraseOp(opDiv);
965
- if (chunk66YSI4PX_cjs.isRoundNode(node)) return eraseOp(opRound);
966
- if (chunk66YSI4PX_cjs.isModNode(node)) return eraseOp(opMod);
967
- if (chunk66YSI4PX_cjs.isToNumberNode(node)) return eraseOp(opToNumber);
968
- if (chunk66YSI4PX_cjs.isJoinNode(node)) return eraseOp(opJoin);
969
- if (chunk66YSI4PX_cjs.isToStringNode(node)) return eraseOp(opToString);
970
- if (chunk66YSI4PX_cjs.isInterpolateNode(node)) return eraseOp(opInterpolate);
971
- if (chunk66YSI4PX_cjs.isFillNode(node)) return eraseOp(opFill);
972
- if (chunk66YSI4PX_cjs.isTrimNode(node)) return eraseOp(opTrim);
973
- if (chunk66YSI4PX_cjs.isLowerNode(node)) return eraseOp(opLower);
974
- if (chunk66YSI4PX_cjs.isUpperNode(node)) return eraseOp(opUpper);
975
- if (chunk66YSI4PX_cjs.isSplitNode(node)) return eraseOp(opSplit);
976
- if (chunk66YSI4PX_cjs.isReplaceNode(node)) return eraseOp(opReplace);
977
- if (chunk66YSI4PX_cjs.isEqNode(node)) return eraseOp(opEq);
978
- if (chunk66YSI4PX_cjs.isNeqNode(node)) return eraseOp(opNeq);
979
- if (chunk66YSI4PX_cjs.isGtNode(node)) return eraseOp(opGt);
980
- if (chunk66YSI4PX_cjs.isGteNode(node)) return eraseOp(opGte);
981
- if (chunk66YSI4PX_cjs.isLtNode(node)) return eraseOp(opLt);
982
- if (chunk66YSI4PX_cjs.isLteNode(node)) return eraseOp(opLte);
983
- if (chunk66YSI4PX_cjs.isEmptyNode(node)) return eraseOp(opEmpty);
984
- if (chunk66YSI4PX_cjs.isStartsWithNode(node)) return eraseOp(opStartsWith);
985
- if (chunk66YSI4PX_cjs.isEndsWithNode(node)) return eraseOp(opEndsWith);
986
- if (chunk66YSI4PX_cjs.isContainsNode(node)) return eraseOp(opContains);
987
- if (chunk66YSI4PX_cjs.isNotNode(node)) return eraseOp(opNot);
988
- if (chunk66YSI4PX_cjs.isAndNode(node)) return eraseOp(opAnd);
989
- if (chunk66YSI4PX_cjs.isOrNode(node)) return eraseOp(opOr);
990
- if (chunk66YSI4PX_cjs.isMergeNode(node)) return eraseOp(opMerge);
991
- if (chunk66YSI4PX_cjs.isCoalesceNode(node)) return eraseOp(opCoalesce);
992
- if (chunk66YSI4PX_cjs.isCaseNode(node)) return eraseOp(opCase);
993
- if (chunk66YSI4PX_cjs.isEntriesOfNode(node)) return eraseOp(opEntriesOf);
994
- if (chunk66YSI4PX_cjs.isKeyByNode(node)) return eraseOp(opKeyBy);
995
- if (chunk66YSI4PX_cjs.isGroupByNode(node)) return eraseOp(opGroupBy);
996
- if (chunk66YSI4PX_cjs.isKeysNode(node)) return eraseOp(opKeys);
997
- if (chunk66YSI4PX_cjs.isValuesNode(node)) return eraseOp(opValues);
998
- if (chunk66YSI4PX_cjs.isFromEntriesNode(node)) return eraseOp(opFromEntries);
999
- if (chunk66YSI4PX_cjs.isPickNode(node)) return eraseOp(opPick);
1000
- if (chunk66YSI4PX_cjs.isOmitNode(node)) return eraseOp(opOmit);
1001
- if (chunk66YSI4PX_cjs.isTypeNode(node)) return eraseOp(opType);
1002
- if (chunk66YSI4PX_cjs.isLengthNode(node)) return eraseOp(opLength);
1003
- if (chunk66YSI4PX_cjs.isHasNode(node)) return eraseOp(opHas);
1004
- if (chunk66YSI4PX_cjs.isRenameKeysNode(node)) return eraseOp(opRenameKeys);
1005
- if (chunk66YSI4PX_cjs.isUpdateNode(node)) return eraseOp(opUpdate);
1006
- if (chunk66YSI4PX_cjs.isAssertNode(node)) return eraseOp(opAssert);
1007
- if (chunk66YSI4PX_cjs.isWalkNode(node)) return eraseOp(opWalk);
1008
- if (chunk66YSI4PX_cjs.isDateNode(node)) return eraseOp(opDate);
1009
- if (chunk66YSI4PX_cjs.isDateAddNode(node)) return eraseOp(opDateAdd);
1010
- if (chunk66YSI4PX_cjs.isDateDiffNode(node)) return eraseOp(opDateDiff);
1011
- if (chunk66YSI4PX_cjs.isLocaleDateNode(node)) return eraseOp(opLocaleDate);
1012
- if (chunk66YSI4PX_cjs.isLocaleMoneyNode(node)) return eraseOp(opLocaleMoney);
1013
- if (chunk66YSI4PX_cjs.isLocaleNumberNode(node)) return eraseOp(opLocaleNumber);
1023
+ if (chunkQUI2X5JB_cjs.isRefNode(node)) return eraseOp(opRef);
1024
+ if (chunkQUI2X5JB_cjs.isConstNode(node)) return eraseOp(opConst);
1025
+ if (chunkQUI2X5JB_cjs.isVarNode(node)) return eraseOp(opVar);
1026
+ if (chunkQUI2X5JB_cjs.isGetNode(node)) return eraseOp(opGet);
1027
+ if (chunkQUI2X5JB_cjs.isWithNode(node)) return eraseOp(opWith);
1028
+ if (chunkQUI2X5JB_cjs.isMapNode(node)) return eraseOp(opMap);
1029
+ if (chunkQUI2X5JB_cjs.isFilterNode(node)) return eraseOp(opFilter);
1030
+ if (chunkQUI2X5JB_cjs.isReduceNode(node)) return eraseOp(opReduce);
1031
+ if (chunkQUI2X5JB_cjs.isSliceNode(node)) return eraseOp(opSlice);
1032
+ if (chunkQUI2X5JB_cjs.isFlattenNode(node)) return eraseOp(opFlatten);
1033
+ if (chunkQUI2X5JB_cjs.isUniqueNode(node)) return eraseOp(opUnique);
1034
+ if (chunkQUI2X5JB_cjs.isSortByNode(node)) return eraseOp(opSortBy);
1035
+ if (chunkQUI2X5JB_cjs.isAddNode(node)) return eraseOp(opAdd);
1036
+ if (chunkQUI2X5JB_cjs.isSubNode(node)) return eraseOp(opSub);
1037
+ if (chunkQUI2X5JB_cjs.isMulNode(node)) return eraseOp(opMul);
1038
+ if (chunkQUI2X5JB_cjs.isDivNode(node)) return eraseOp(opDiv);
1039
+ if (chunkQUI2X5JB_cjs.isRoundNode(node)) return eraseOp(opRound);
1040
+ if (chunkQUI2X5JB_cjs.isModNode(node)) return eraseOp(opMod);
1041
+ if (chunkQUI2X5JB_cjs.isToNumberNode(node)) return eraseOp(opToNumber);
1042
+ if (chunkQUI2X5JB_cjs.isJoinNode(node)) return eraseOp(opJoin);
1043
+ if (chunkQUI2X5JB_cjs.isToStringNode(node)) return eraseOp(opToString);
1044
+ if (chunkQUI2X5JB_cjs.isInterpolateNode(node)) return eraseOp(opInterpolate);
1045
+ if (chunkQUI2X5JB_cjs.isFillNode(node)) return eraseOp(opFill);
1046
+ if (chunkQUI2X5JB_cjs.isTrimNode(node)) return eraseOp(opTrim);
1047
+ if (chunkQUI2X5JB_cjs.isLowerNode(node)) return eraseOp(opLower);
1048
+ if (chunkQUI2X5JB_cjs.isUpperNode(node)) return eraseOp(opUpper);
1049
+ if (chunkQUI2X5JB_cjs.isSplitNode(node)) return eraseOp(opSplit);
1050
+ if (chunkQUI2X5JB_cjs.isReplaceNode(node)) return eraseOp(opReplace);
1051
+ if (chunkQUI2X5JB_cjs.isEqNode(node)) return eraseOp(opEq);
1052
+ if (chunkQUI2X5JB_cjs.isNeqNode(node)) return eraseOp(opNeq);
1053
+ if (chunkQUI2X5JB_cjs.isGtNode(node)) return eraseOp(opGt);
1054
+ if (chunkQUI2X5JB_cjs.isGteNode(node)) return eraseOp(opGte);
1055
+ if (chunkQUI2X5JB_cjs.isLtNode(node)) return eraseOp(opLt);
1056
+ if (chunkQUI2X5JB_cjs.isLteNode(node)) return eraseOp(opLte);
1057
+ if (chunkQUI2X5JB_cjs.isEmptyNode(node)) return eraseOp(opEmpty);
1058
+ if (chunkQUI2X5JB_cjs.isStartsWithNode(node)) return eraseOp(opStartsWith);
1059
+ if (chunkQUI2X5JB_cjs.isEndsWithNode(node)) return eraseOp(opEndsWith);
1060
+ if (chunkQUI2X5JB_cjs.isContainsNode(node)) return eraseOp(opContains);
1061
+ if (chunkQUI2X5JB_cjs.isNotNode(node)) return eraseOp(opNot);
1062
+ if (chunkQUI2X5JB_cjs.isAndNode(node)) return eraseOp(opAnd);
1063
+ if (chunkQUI2X5JB_cjs.isOrNode(node)) return eraseOp(opOr);
1064
+ if (chunkQUI2X5JB_cjs.isMergeNode(node)) return eraseOp(opMerge);
1065
+ if (chunkQUI2X5JB_cjs.isCoalesceNode(node)) return eraseOp(opCoalesce);
1066
+ if (chunkQUI2X5JB_cjs.isCaseNode(node)) return eraseOp(opCase);
1067
+ if (chunkQUI2X5JB_cjs.isEntriesOfNode(node)) return eraseOp(opEntriesOf);
1068
+ if (chunkQUI2X5JB_cjs.isKeyByNode(node)) return eraseOp(opKeyBy);
1069
+ if (chunkQUI2X5JB_cjs.isGroupByNode(node)) return eraseOp(opGroupBy);
1070
+ if (chunkQUI2X5JB_cjs.isKeysNode(node)) return eraseOp(opKeys);
1071
+ if (chunkQUI2X5JB_cjs.isValuesNode(node)) return eraseOp(opValues);
1072
+ if (chunkQUI2X5JB_cjs.isFromEntriesNode(node)) return eraseOp(opFromEntries);
1073
+ if (chunkQUI2X5JB_cjs.isPickNode(node)) return eraseOp(opPick);
1074
+ if (chunkQUI2X5JB_cjs.isOmitNode(node)) return eraseOp(opOmit);
1075
+ if (chunkQUI2X5JB_cjs.isTypeNode(node)) return eraseOp(opType);
1076
+ if (chunkQUI2X5JB_cjs.isLengthNode(node)) return eraseOp(opLength);
1077
+ if (chunkQUI2X5JB_cjs.isHasNode(node)) return eraseOp(opHas);
1078
+ if (chunkQUI2X5JB_cjs.isRenameKeysNode(node)) return eraseOp(opRenameKeys);
1079
+ if (chunkQUI2X5JB_cjs.isUpdateNode(node)) return eraseOp(opUpdate);
1080
+ if (chunkQUI2X5JB_cjs.isAssertNode(node)) return eraseOp(opAssert);
1081
+ if (chunkQUI2X5JB_cjs.isWalkNode(node)) return eraseOp(opWalk);
1082
+ if (chunkQUI2X5JB_cjs.isDateNode(node)) return eraseOp(opDate);
1083
+ if (chunkQUI2X5JB_cjs.isDateAddNode(node)) return eraseOp(opDateAdd);
1084
+ if (chunkQUI2X5JB_cjs.isDateDiffNode(node)) return eraseOp(opDateDiff);
1085
+ if (chunkQUI2X5JB_cjs.isLocaleDateNode(node)) return eraseOp(opLocaleDate);
1086
+ if (chunkQUI2X5JB_cjs.isLocaleMoneyNode(node)) return eraseOp(opLocaleMoney);
1087
+ if (chunkQUI2X5JB_cjs.isLocaleNumberNode(node)) return eraseOp(opLocaleNumber);
1014
1088
  return void 0;
1015
1089
  };
1016
1090
  var isFoldableLiteral = (node) => {
@@ -1018,8 +1092,8 @@ var isFoldableLiteral = (node) => {
1018
1092
  const t = typeof node;
1019
1093
  if (t === "string" || t === "number" || t === "boolean") return true;
1020
1094
  if (Array.isArray(node)) return node.every(isFoldableLiteral);
1021
- if (!isPlainObject2(node)) return false;
1022
- if (chunk66YSI4PX_cjs.isConstNode(node)) return isFoldableLiteral(node.$const);
1095
+ if (!chunkQUI2X5JB_cjs.isJsonObject(node)) return false;
1096
+ if (chunkQUI2X5JB_cjs.isConstNode(node)) return isFoldableLiteral(node.$const);
1023
1097
  if (Object.keys(node).some((k) => k.startsWith("$"))) return false;
1024
1098
  return Object.values(node).every(isFoldableLiteral);
1025
1099
  };
@@ -1031,8 +1105,8 @@ var optimizeNode = (node, stats, evaluate2) => {
1031
1105
  for (const child of node) out.push(optimizeNode(child, stats, evaluate2));
1032
1106
  return out;
1033
1107
  }
1034
- if (!isPlainObject2(node)) return node;
1035
- if (chunk66YSI4PX_cjs.isConstNode(node)) {
1108
+ if (!chunkQUI2X5JB_cjs.isJsonObject(node)) return node;
1109
+ if (chunkQUI2X5JB_cjs.isConstNode(node)) {
1036
1110
  const literal = { ...node };
1037
1111
  freezeConstant(literal["$const"]);
1038
1112
  attachNonEnumerable(literal, HANDLER_KEY, eraseOp(opConst));
@@ -1045,15 +1119,15 @@ var optimizeNode = (node, stats, evaluate2) => {
1045
1119
  return out;
1046
1120
  };
1047
1121
  let optimized;
1048
- if (chunk66YSI4PX_cjs.isWithNode(node)) {
1122
+ if (chunkQUI2X5JB_cjs.isWithNode(node)) {
1049
1123
  optimized = { $with: { let: optimizeValues(node.$with.let), value: optimizeNode(node.$with.value, stats, evaluate2) } };
1050
- } else if (chunk66YSI4PX_cjs.isRenameKeysNode(node)) {
1124
+ } else if (chunkQUI2X5JB_cjs.isRenameKeysNode(node)) {
1051
1125
  optimized = { $renameKeys: { ...node.$renameKeys, from: optimizeNode(node.$renameKeys.from, stats, evaluate2) } };
1052
1126
  } else {
1053
1127
  optimized = optimizeValues(node);
1054
1128
  }
1055
- if (chunk66YSI4PX_cjs.isRefNode(optimized)) {
1056
- const segments = chunk66YSI4PX_cjs.parseJsonPath(optimized.$ref);
1129
+ if (chunkQUI2X5JB_cjs.isRefNode(optimized)) {
1130
+ const segments = chunkQUI2X5JB_cjs.parseJsonPath(optimized.$ref);
1057
1131
  if (segments.length > 0) {
1058
1132
  attachNonEnumerable(optimized, SEGMENTS_KEY2, segments);
1059
1133
  stats.refsInlined += 1;
@@ -1064,7 +1138,7 @@ var optimizeNode = (node, stats, evaluate2) => {
1064
1138
  attachNonEnumerable(optimized, HANDLER_KEY, handler);
1065
1139
  stats.handlersAttached += 1;
1066
1140
  }
1067
- if (handler !== void 0 && !chunk66YSI4PX_cjs.isConstNode(optimized) && !chunk66YSI4PX_cjs.isRefNode(optimized) && !chunk66YSI4PX_cjs.isVarNode(optimized)) {
1141
+ if (handler !== void 0 && !chunkQUI2X5JB_cjs.isConstNode(optimized) && !chunkQUI2X5JB_cjs.isRefNode(optimized) && !chunkQUI2X5JB_cjs.isVarNode(optimized)) {
1068
1142
  const allFoldable = Object.entries(optimized).every(([key, value]) => {
1069
1143
  if (key === HANDLER_KEY || key === SEGMENTS_KEY2) return true;
1070
1144
  return isFoldableLiteral(value);
@@ -1094,19 +1168,19 @@ var annotateNode = (node) => {
1094
1168
  for (const child of node) annotateNode(child);
1095
1169
  return;
1096
1170
  }
1097
- if (!isPlainObject2(node)) return;
1098
- if (chunk66YSI4PX_cjs.isWithNode(node)) {
1171
+ if (!chunkQUI2X5JB_cjs.isJsonObject(node)) return;
1172
+ if (chunkQUI2X5JB_cjs.isWithNode(node)) {
1099
1173
  for (const value of Object.values(node.$with.let)) annotateNode(value);
1100
1174
  annotateNode(node.$with.value);
1101
- } else if (chunk66YSI4PX_cjs.isRenameKeysNode(node)) {
1175
+ } else if (chunkQUI2X5JB_cjs.isRenameKeysNode(node)) {
1102
1176
  annotateNode(node.$renameKeys.from);
1103
- } else if (!chunk66YSI4PX_cjs.isConstNode(node)) {
1177
+ } else if (!chunkQUI2X5JB_cjs.isConstNode(node)) {
1104
1178
  for (const value of Object.values(node)) annotateNode(value);
1105
1179
  } else {
1106
1180
  freezeConstant(node.$const);
1107
1181
  }
1108
- if (chunk66YSI4PX_cjs.isRefNode(node)) {
1109
- const segments = chunk66YSI4PX_cjs.parseJsonPath(node.$ref);
1182
+ if (chunkQUI2X5JB_cjs.isRefNode(node)) {
1183
+ const segments = chunkQUI2X5JB_cjs.parseJsonPath(node.$ref);
1110
1184
  if (segments.length > 0) attachNonEnumerable(node, SEGMENTS_KEY2, segments);
1111
1185
  }
1112
1186
  const handler = resolveHandler(node);
@@ -1114,15 +1188,88 @@ var annotateNode = (node) => {
1114
1188
  };
1115
1189
  var rehydrate = (core) => annotateNode(core);
1116
1190
 
1191
+ // src/engine/place.ts
1192
+ var findPath = (tree, matches) => {
1193
+ const at = [];
1194
+ const walk2 = (value) => {
1195
+ if (matches(value)) return true;
1196
+ if (value === null || typeof value !== "object") return false;
1197
+ for (const [key, child] of Object.entries(value)) {
1198
+ at.push(key);
1199
+ if (walk2(child)) return true;
1200
+ at.pop();
1201
+ }
1202
+ return false;
1203
+ };
1204
+ return walk2(tree) ? at : void 0;
1205
+ };
1206
+ var soleOpOf = (node) => {
1207
+ if (!chunkQUI2X5JB_cjs.isJsonObject(node)) return void 0;
1208
+ const keys = Object.keys(node);
1209
+ return keys.length === 1 && keys[0] !== void 0 && keys[0].startsWith("$") ? keys[0] : void 0;
1210
+ };
1211
+ var MARK = "\0prism-field:";
1212
+ var authorPosition = (authored, treePath) => {
1213
+ const path = [];
1214
+ let node = authored;
1215
+ let rest = treePath;
1216
+ let sugar;
1217
+ while (rest.length > 0) {
1218
+ const [key] = rest;
1219
+ if (key !== void 0 && typeof node === "object" && node !== null && Object.prototype.hasOwnProperty.call(node, key)) {
1220
+ path.push(key);
1221
+ node = Reflect.get(node, key);
1222
+ rest = rest.slice(1);
1223
+ sugar = void 0;
1224
+ continue;
1225
+ }
1226
+ const op = soleOpOf(node);
1227
+ if (op === void 0 || !chunkQUI2X5JB_cjs.isJsonObject(node)) return void 0;
1228
+ const fields = node[op];
1229
+ const names = chunkQUI2X5JB_cjs.isJsonObject(fields) ? Object.keys(fields) : [];
1230
+ const rewritten = desugar({ [op]: Object.fromEntries(names.map((name) => [name, `${MARK}${name}`])) });
1231
+ const landed = names.map((name) => ({ name, at: findPath(rewritten, (value) => value === `${MARK}${name}`) })).find(({ at }) => at !== void 0 && at.every((segment, i) => rest[i] === segment));
1232
+ if (landed?.at === void 0 || !chunkQUI2X5JB_cjs.isJsonObject(fields)) return { path: [...path, op], sugar: op };
1233
+ path.push(op, landed.name);
1234
+ node = fields[landed.name];
1235
+ rest = rest.slice(landed.at.length);
1236
+ sugar = op;
1237
+ }
1238
+ return sugar === void 0 ? { path } : { path, sugar };
1239
+ };
1240
+ var pathText = (path) => path.length === 0 ? "(root)" : path.join(".");
1241
+ var placeError = (error, tree, authored) => {
1242
+ if (!(error instanceof chunkQUI2X5JB_cjs.PrismError)) return;
1243
+ const origin = chunkQUI2X5JB_cjs.takeOrigin(error);
1244
+ if (origin === void 0) return;
1245
+ const parts = [error.message];
1246
+ if (origin.detail !== void 0) parts.push(origin.detail());
1247
+ const tail = [origin.op, origin.field].filter((part) => part !== void 0);
1248
+ let formOp = origin.op;
1249
+ const nodePath = findPath(tree, (value) => value === origin.node);
1250
+ if (nodePath !== void 0) {
1251
+ const treePath = [...nodePath, ...tail];
1252
+ const position = authored === void 0 ? { path: treePath } : authorPosition(authored, treePath) ?? { path: treePath };
1253
+ if (position.sugar === void 0) parts.push(`At ${pathText(position.path)}.`);
1254
+ else {
1255
+ parts.push(`At ${pathText(position.path)}, in the ${origin.op ?? "node"} that ${position.sugar} runs as.`);
1256
+ formOp = position.sugar;
1257
+ }
1258
+ }
1259
+ const form = origin.form && formOp !== void 0 ? chunkQUI2X5JB_cjs.formOf(formOp) : void 0;
1260
+ if (form !== void 0) parts.push(`Form: ${form}`);
1261
+ error.message = parts.join(" ");
1262
+ };
1263
+
1117
1264
  // src/engine/evaluate.ts
1118
1265
  var HANDLER_KEY2 = "__op";
1119
1266
  var isAttachedFn = (value) => typeof value === "function";
1120
1267
  var evaluateNode = (node, context) => {
1121
1268
  const { budget } = context;
1122
1269
  if (budget === void 0) return evaluateUnbudgeted(node, context);
1123
- spendStep(budget);
1270
+ spendStep(budget, node);
1124
1271
  const out = evaluateUnbudgeted(node, context);
1125
- measureString(out, budget);
1272
+ measureString(out, budget, node);
1126
1273
  return out;
1127
1274
  };
1128
1275
  var evaluateUnbudgeted = (node, context) => {
@@ -1133,130 +1280,155 @@ var evaluateUnbudgeted = (node, context) => {
1133
1280
  const obj = node;
1134
1281
  const attached = obj[HANDLER_KEY2];
1135
1282
  if (isAttachedFn(attached)) return attached(obj, context, evaluateNode);
1136
- if (chunk66YSI4PX_cjs.isPlainObject(obj)) {
1283
+ if (chunkQUI2X5JB_cjs.isPlainObject(obj)) {
1137
1284
  const result = {};
1138
- if (!(chunk66YSI4PX_cjs.OPTIONAL_FIELDS_KEY in obj)) {
1285
+ if (!(chunkQUI2X5JB_cjs.OPTIONAL_FIELDS_KEY in obj)) {
1139
1286
  for (const key of Object.keys(obj)) result[key] = evaluateNode(obj[key], context);
1140
1287
  return result;
1141
1288
  }
1142
1289
  const optionalFields = new Set(
1143
- Array.isArray(obj[chunk66YSI4PX_cjs.OPTIONAL_FIELDS_KEY]) ? obj[chunk66YSI4PX_cjs.OPTIONAL_FIELDS_KEY] : []
1290
+ Array.isArray(obj[chunkQUI2X5JB_cjs.OPTIONAL_FIELDS_KEY]) ? obj[chunkQUI2X5JB_cjs.OPTIONAL_FIELDS_KEY] : []
1144
1291
  );
1145
1292
  for (const [key, value] of Object.entries(obj)) {
1146
- if (key === chunk66YSI4PX_cjs.OPTIONAL_FIELDS_KEY) continue;
1293
+ if (key === chunkQUI2X5JB_cjs.OPTIONAL_FIELDS_KEY) continue;
1147
1294
  const isOptional = optionalFields.has(key);
1148
1295
  try {
1149
1296
  const evaluated = evaluateNode(value, context);
1150
1297
  if (isOptional && (evaluated === null || evaluated === void 0)) continue;
1151
1298
  result[key] = evaluated;
1152
1299
  } catch (error) {
1153
- if (isOptional && error instanceof chunk66YSI4PX_cjs.PrismError && error.code === chunk66YSI4PX_cjs.ErrorCode.MISSING_PATH) continue;
1300
+ if (isOptional && error instanceof chunkQUI2X5JB_cjs.PrismError && error.code === chunkQUI2X5JB_cjs.ErrorCode.MISSING_PATH) continue;
1154
1301
  throw error;
1155
1302
  }
1156
1303
  }
1157
1304
  return result;
1158
1305
  }
1159
- if (chunk66YSI4PX_cjs.isRefNode(obj)) return opRef(obj, context);
1160
- if (chunk66YSI4PX_cjs.isConstNode(obj)) return opConst(obj, context);
1161
- if (chunk66YSI4PX_cjs.isVarNode(obj)) return opVar(obj, context);
1162
- if (chunk66YSI4PX_cjs.isGetNode(obj)) return opGet(obj, context, evaluateNode);
1163
- if (chunk66YSI4PX_cjs.isWithNode(obj)) return opWith(obj, context, evaluateNode);
1164
- if (chunk66YSI4PX_cjs.isMapNode(obj)) return opMap(obj, context, evaluateNode);
1165
- if (chunk66YSI4PX_cjs.isFilterNode(obj)) return opFilter(obj, context, evaluateNode);
1166
- if (chunk66YSI4PX_cjs.isReduceNode(obj)) return opReduce(obj, context, evaluateNode);
1167
- if (chunk66YSI4PX_cjs.isSliceNode(obj)) return opSlice(obj, context, evaluateNode);
1168
- if (chunk66YSI4PX_cjs.isFlattenNode(obj)) return opFlatten(obj, context, evaluateNode);
1169
- if (chunk66YSI4PX_cjs.isUniqueNode(obj)) return opUnique(obj, context, evaluateNode);
1170
- if (chunk66YSI4PX_cjs.isSortByNode(obj)) return opSortBy(obj, context, evaluateNode);
1171
- if (chunk66YSI4PX_cjs.isAddNode(obj)) return opAdd(obj, context, evaluateNode);
1172
- if (chunk66YSI4PX_cjs.isSubNode(obj)) return opSub(obj, context, evaluateNode);
1173
- if (chunk66YSI4PX_cjs.isMulNode(obj)) return opMul(obj, context, evaluateNode);
1174
- if (chunk66YSI4PX_cjs.isDivNode(obj)) return opDiv(obj, context, evaluateNode);
1175
- if (chunk66YSI4PX_cjs.isRoundNode(obj)) return opRound(obj, context, evaluateNode);
1176
- if (chunk66YSI4PX_cjs.isModNode(obj)) return opMod(obj, context, evaluateNode);
1177
- if (chunk66YSI4PX_cjs.isToNumberNode(obj)) return opToNumber(obj, context, evaluateNode);
1178
- if (chunk66YSI4PX_cjs.isJoinNode(obj)) return opJoin(obj, context, evaluateNode);
1179
- if (chunk66YSI4PX_cjs.isToStringNode(obj)) return opToString(obj, context, evaluateNode);
1180
- if (chunk66YSI4PX_cjs.isInterpolateNode(obj)) return opInterpolate(obj, context, evaluateNode);
1181
- if (chunk66YSI4PX_cjs.isFillNode(obj)) return opFill(obj, context, evaluateNode);
1182
- if (chunk66YSI4PX_cjs.isTrimNode(obj)) return opTrim(obj, context, evaluateNode);
1183
- if (chunk66YSI4PX_cjs.isLowerNode(obj)) return opLower(obj, context, evaluateNode);
1184
- if (chunk66YSI4PX_cjs.isUpperNode(obj)) return opUpper(obj, context, evaluateNode);
1185
- if (chunk66YSI4PX_cjs.isSplitNode(obj)) return opSplit(obj, context, evaluateNode);
1186
- if (chunk66YSI4PX_cjs.isReplaceNode(obj)) return opReplace(obj, context, evaluateNode);
1187
- if (chunk66YSI4PX_cjs.isEqNode(obj)) return opEq(obj, context, evaluateNode);
1188
- if (chunk66YSI4PX_cjs.isNeqNode(obj)) return opNeq(obj, context, evaluateNode);
1189
- if (chunk66YSI4PX_cjs.isGtNode(obj)) return opGt(obj, context, evaluateNode);
1190
- if (chunk66YSI4PX_cjs.isGteNode(obj)) return opGte(obj, context, evaluateNode);
1191
- if (chunk66YSI4PX_cjs.isLtNode(obj)) return opLt(obj, context, evaluateNode);
1192
- if (chunk66YSI4PX_cjs.isLteNode(obj)) return opLte(obj, context, evaluateNode);
1193
- if (chunk66YSI4PX_cjs.isEmptyNode(obj)) return opEmpty(obj, context, evaluateNode);
1194
- if (chunk66YSI4PX_cjs.isStartsWithNode(obj)) return opStartsWith(obj, context, evaluateNode);
1195
- if (chunk66YSI4PX_cjs.isEndsWithNode(obj)) return opEndsWith(obj, context, evaluateNode);
1196
- if (chunk66YSI4PX_cjs.isContainsNode(obj)) return opContains(obj, context, evaluateNode);
1197
- if (chunk66YSI4PX_cjs.isNotNode(obj)) return opNot(obj, context, evaluateNode);
1198
- if (chunk66YSI4PX_cjs.isAndNode(obj)) return opAnd(obj, context, evaluateNode);
1199
- if (chunk66YSI4PX_cjs.isOrNode(obj)) return opOr(obj, context, evaluateNode);
1200
- if (chunk66YSI4PX_cjs.isMergeNode(obj)) return opMerge(obj, context, evaluateNode);
1201
- if (chunk66YSI4PX_cjs.isCoalesceNode(obj)) return opCoalesce(obj, context, evaluateNode);
1202
- if (chunk66YSI4PX_cjs.isCaseNode(obj)) return opCase(obj, context, evaluateNode);
1203
- if (chunk66YSI4PX_cjs.isEntriesOfNode(obj)) return opEntriesOf(obj, context, evaluateNode);
1204
- if (chunk66YSI4PX_cjs.isKeyByNode(obj)) return opKeyBy(obj, context, evaluateNode);
1205
- if (chunk66YSI4PX_cjs.isGroupByNode(obj)) return opGroupBy(obj, context, evaluateNode);
1206
- if (chunk66YSI4PX_cjs.isKeysNode(obj)) return opKeys(obj, context, evaluateNode);
1207
- if (chunk66YSI4PX_cjs.isValuesNode(obj)) return opValues(obj, context, evaluateNode);
1208
- if (chunk66YSI4PX_cjs.isFromEntriesNode(obj)) return opFromEntries(obj, context, evaluateNode);
1209
- if (chunk66YSI4PX_cjs.isPickNode(obj)) return opPick(obj, context, evaluateNode);
1210
- if (chunk66YSI4PX_cjs.isOmitNode(obj)) return opOmit(obj, context, evaluateNode);
1211
- if (chunk66YSI4PX_cjs.isTypeNode(obj)) return opType(obj, context, evaluateNode);
1212
- if (chunk66YSI4PX_cjs.isLengthNode(obj)) return opLength(obj, context, evaluateNode);
1213
- if (chunk66YSI4PX_cjs.isHasNode(obj)) return opHas(obj, context, evaluateNode);
1214
- if (chunk66YSI4PX_cjs.isRenameKeysNode(obj)) return opRenameKeys(obj, context, evaluateNode);
1215
- if (chunk66YSI4PX_cjs.isUpdateNode(obj)) return opUpdate(obj, context, evaluateNode);
1216
- if (chunk66YSI4PX_cjs.isAssertNode(obj)) return opAssert(obj, context, evaluateNode);
1217
- if (chunk66YSI4PX_cjs.isWalkNode(obj)) return opWalk(obj, context, evaluateNode);
1218
- if (chunk66YSI4PX_cjs.isDateNode(obj)) return opDate(obj, context, evaluateNode);
1219
- if (chunk66YSI4PX_cjs.isDateAddNode(obj)) return opDateAdd(obj, context, evaluateNode);
1220
- if (chunk66YSI4PX_cjs.isDateDiffNode(obj)) return opDateDiff(obj, context, evaluateNode);
1221
- if (chunk66YSI4PX_cjs.isLocaleDateNode(obj)) return opLocaleDate(obj, context, evaluateNode);
1222
- if (chunk66YSI4PX_cjs.isLocaleMoneyNode(obj)) return opLocaleMoney(obj, context, evaluateNode);
1223
- if (chunk66YSI4PX_cjs.isLocaleNumberNode(obj)) return opLocaleNumber(obj, context, evaluateNode);
1306
+ if (chunkQUI2X5JB_cjs.isRefNode(obj)) return opRef(obj, context);
1307
+ if (chunkQUI2X5JB_cjs.isConstNode(obj)) return opConst(obj, context);
1308
+ if (chunkQUI2X5JB_cjs.isVarNode(obj)) return opVar(obj, context);
1309
+ if (chunkQUI2X5JB_cjs.isGetNode(obj)) return opGet(obj, context, evaluateNode);
1310
+ if (chunkQUI2X5JB_cjs.isWithNode(obj)) return opWith(obj, context, evaluateNode);
1311
+ if (chunkQUI2X5JB_cjs.isMapNode(obj)) return opMap(obj, context, evaluateNode);
1312
+ if (chunkQUI2X5JB_cjs.isFilterNode(obj)) return opFilter(obj, context, evaluateNode);
1313
+ if (chunkQUI2X5JB_cjs.isReduceNode(obj)) return opReduce(obj, context, evaluateNode);
1314
+ if (chunkQUI2X5JB_cjs.isSliceNode(obj)) return opSlice(obj, context, evaluateNode);
1315
+ if (chunkQUI2X5JB_cjs.isFlattenNode(obj)) return opFlatten(obj, context, evaluateNode);
1316
+ if (chunkQUI2X5JB_cjs.isUniqueNode(obj)) return opUnique(obj, context, evaluateNode);
1317
+ if (chunkQUI2X5JB_cjs.isSortByNode(obj)) return opSortBy(obj, context, evaluateNode);
1318
+ if (chunkQUI2X5JB_cjs.isAddNode(obj)) return opAdd(obj, context, evaluateNode);
1319
+ if (chunkQUI2X5JB_cjs.isSubNode(obj)) return opSub(obj, context, evaluateNode);
1320
+ if (chunkQUI2X5JB_cjs.isMulNode(obj)) return opMul(obj, context, evaluateNode);
1321
+ if (chunkQUI2X5JB_cjs.isDivNode(obj)) return opDiv(obj, context, evaluateNode);
1322
+ if (chunkQUI2X5JB_cjs.isRoundNode(obj)) return opRound(obj, context, evaluateNode);
1323
+ if (chunkQUI2X5JB_cjs.isModNode(obj)) return opMod(obj, context, evaluateNode);
1324
+ if (chunkQUI2X5JB_cjs.isToNumberNode(obj)) return opToNumber(obj, context, evaluateNode);
1325
+ if (chunkQUI2X5JB_cjs.isJoinNode(obj)) return opJoin(obj, context, evaluateNode);
1326
+ if (chunkQUI2X5JB_cjs.isToStringNode(obj)) return opToString(obj, context, evaluateNode);
1327
+ if (chunkQUI2X5JB_cjs.isInterpolateNode(obj)) return opInterpolate(obj, context, evaluateNode);
1328
+ if (chunkQUI2X5JB_cjs.isFillNode(obj)) return opFill(obj, context, evaluateNode);
1329
+ if (chunkQUI2X5JB_cjs.isTrimNode(obj)) return opTrim(obj, context, evaluateNode);
1330
+ if (chunkQUI2X5JB_cjs.isLowerNode(obj)) return opLower(obj, context, evaluateNode);
1331
+ if (chunkQUI2X5JB_cjs.isUpperNode(obj)) return opUpper(obj, context, evaluateNode);
1332
+ if (chunkQUI2X5JB_cjs.isSplitNode(obj)) return opSplit(obj, context, evaluateNode);
1333
+ if (chunkQUI2X5JB_cjs.isReplaceNode(obj)) return opReplace(obj, context, evaluateNode);
1334
+ if (chunkQUI2X5JB_cjs.isEqNode(obj)) return opEq(obj, context, evaluateNode);
1335
+ if (chunkQUI2X5JB_cjs.isNeqNode(obj)) return opNeq(obj, context, evaluateNode);
1336
+ if (chunkQUI2X5JB_cjs.isGtNode(obj)) return opGt(obj, context, evaluateNode);
1337
+ if (chunkQUI2X5JB_cjs.isGteNode(obj)) return opGte(obj, context, evaluateNode);
1338
+ if (chunkQUI2X5JB_cjs.isLtNode(obj)) return opLt(obj, context, evaluateNode);
1339
+ if (chunkQUI2X5JB_cjs.isLteNode(obj)) return opLte(obj, context, evaluateNode);
1340
+ if (chunkQUI2X5JB_cjs.isEmptyNode(obj)) return opEmpty(obj, context, evaluateNode);
1341
+ if (chunkQUI2X5JB_cjs.isStartsWithNode(obj)) return opStartsWith(obj, context, evaluateNode);
1342
+ if (chunkQUI2X5JB_cjs.isEndsWithNode(obj)) return opEndsWith(obj, context, evaluateNode);
1343
+ if (chunkQUI2X5JB_cjs.isContainsNode(obj)) return opContains(obj, context, evaluateNode);
1344
+ if (chunkQUI2X5JB_cjs.isNotNode(obj)) return opNot(obj, context, evaluateNode);
1345
+ if (chunkQUI2X5JB_cjs.isAndNode(obj)) return opAnd(obj, context, evaluateNode);
1346
+ if (chunkQUI2X5JB_cjs.isOrNode(obj)) return opOr(obj, context, evaluateNode);
1347
+ if (chunkQUI2X5JB_cjs.isMergeNode(obj)) return opMerge(obj, context, evaluateNode);
1348
+ if (chunkQUI2X5JB_cjs.isCoalesceNode(obj)) return opCoalesce(obj, context, evaluateNode);
1349
+ if (chunkQUI2X5JB_cjs.isCaseNode(obj)) return opCase(obj, context, evaluateNode);
1350
+ if (chunkQUI2X5JB_cjs.isEntriesOfNode(obj)) return opEntriesOf(obj, context, evaluateNode);
1351
+ if (chunkQUI2X5JB_cjs.isKeyByNode(obj)) return opKeyBy(obj, context, evaluateNode);
1352
+ if (chunkQUI2X5JB_cjs.isGroupByNode(obj)) return opGroupBy(obj, context, evaluateNode);
1353
+ if (chunkQUI2X5JB_cjs.isKeysNode(obj)) return opKeys(obj, context, evaluateNode);
1354
+ if (chunkQUI2X5JB_cjs.isValuesNode(obj)) return opValues(obj, context, evaluateNode);
1355
+ if (chunkQUI2X5JB_cjs.isFromEntriesNode(obj)) return opFromEntries(obj, context, evaluateNode);
1356
+ if (chunkQUI2X5JB_cjs.isPickNode(obj)) return opPick(obj, context, evaluateNode);
1357
+ if (chunkQUI2X5JB_cjs.isOmitNode(obj)) return opOmit(obj, context, evaluateNode);
1358
+ if (chunkQUI2X5JB_cjs.isTypeNode(obj)) return opType(obj, context, evaluateNode);
1359
+ if (chunkQUI2X5JB_cjs.isLengthNode(obj)) return opLength(obj, context, evaluateNode);
1360
+ if (chunkQUI2X5JB_cjs.isHasNode(obj)) return opHas(obj, context, evaluateNode);
1361
+ if (chunkQUI2X5JB_cjs.isRenameKeysNode(obj)) return opRenameKeys(obj, context, evaluateNode);
1362
+ if (chunkQUI2X5JB_cjs.isUpdateNode(obj)) return opUpdate(obj, context, evaluateNode);
1363
+ if (chunkQUI2X5JB_cjs.isAssertNode(obj)) return opAssert(obj, context, evaluateNode);
1364
+ if (chunkQUI2X5JB_cjs.isWalkNode(obj)) return opWalk(obj, context, evaluateNode);
1365
+ if (chunkQUI2X5JB_cjs.isDateNode(obj)) return opDate(obj, context, evaluateNode);
1366
+ if (chunkQUI2X5JB_cjs.isDateAddNode(obj)) return opDateAdd(obj, context, evaluateNode);
1367
+ if (chunkQUI2X5JB_cjs.isDateDiffNode(obj)) return opDateDiff(obj, context, evaluateNode);
1368
+ if (chunkQUI2X5JB_cjs.isLocaleDateNode(obj)) return opLocaleDate(obj, context, evaluateNode);
1369
+ if (chunkQUI2X5JB_cjs.isLocaleMoneyNode(obj)) return opLocaleMoney(obj, context, evaluateNode);
1370
+ if (chunkQUI2X5JB_cjs.isLocaleNumberNode(obj)) return opLocaleNumber(obj, context, evaluateNode);
1224
1371
  const unknownKeys = Object.keys(obj).filter((k) => k.startsWith("$"));
1225
- throw new chunk66YSI4PX_cjs.PrismError("Unsupported node shape", chunk66YSI4PX_cjs.ErrorCode.NODE_SHAPE, {
1226
- details: { keys: unknownKeys, preview: JSON.stringify(obj).slice(0, 100) }
1227
- });
1372
+ const preview = JSON.stringify(obj).slice(0, 100);
1373
+ return chunkQUI2X5JB_cjs.failAt(
1374
+ `No op answers this node: ${chunkQUI2X5JB_cjs.clip(JSON.stringify(obj), 100)} (keys: ${unknownKeys.join(", ")}). The tree did not go through the schema.`,
1375
+ chunkQUI2X5JB_cjs.ErrorCode.NODE_SHAPE,
1376
+ { details: { keys: unknownKeys, preview } },
1377
+ { node: obj, form: false }
1378
+ );
1228
1379
  };
1229
- var isJsonValue = (value) => {
1230
- if (value === null || typeof value === "string" || typeof value === "boolean") return true;
1231
- if (typeof value === "number") return Number.isFinite(value);
1232
- if (Array.isArray(value)) return value.every(isJsonValue);
1233
- if (typeof value !== "object") return false;
1380
+ var notJson = (value, at) => {
1381
+ if (value === null || typeof value === "string" || typeof value === "boolean") return void 0;
1382
+ if (typeof value === "number") return Number.isFinite(value) ? void 0 : `${at} is ${value}`;
1383
+ if (Array.isArray(value)) {
1384
+ for (const index of Object.keys(value)) {
1385
+ const found = notJson(value[Number(index)], `${at}[${index}]`);
1386
+ if (found !== void 0) return found;
1387
+ }
1388
+ return void 0;
1389
+ }
1390
+ if (typeof value !== "object") return `${at} is ${value === void 0 ? "undefined" : `a ${typeof value}`}`;
1234
1391
  const proto = Object.getPrototypeOf(value);
1235
- return (proto === Object.prototype || proto === null) && Object.values(value).every(isJsonValue);
1392
+ if (proto !== Object.prototype && proto !== null) {
1393
+ const tag = Object.prototype.toString.call(value).slice(8, -1);
1394
+ return `${at} is ${tag === "Object" ? "an instance of a class" : `a ${tag}`}`;
1395
+ }
1396
+ for (const [key, inner] of Object.entries(value)) {
1397
+ const found = notJson(inner, `${at}.${key}`);
1398
+ if (found !== void 0) return found;
1399
+ }
1400
+ return void 0;
1236
1401
  };
1237
1402
  var checked = (config) => {
1238
- if (strata.exceedsDepth(config)) throw new chunk66YSI4PX_cjs.PrismError("Invalid config", chunk66YSI4PX_cjs.ErrorCode.SCHEMA, { details: { issues: [{ path: "root", message: strata.depthRefusal() }] } });
1239
- const parsed = chunk66YSI4PX_cjs.ConfigSchema.safeParse(config);
1403
+ if (strata.exceedsDepth(config)) throw new chunkQUI2X5JB_cjs.PrismError(`Invalid config: ${strata.depthRefusal()}`, chunkQUI2X5JB_cjs.ErrorCode.SCHEMA, { details: { issues: [{ path: "root", message: strata.depthRefusal() }] } });
1404
+ const parsed = chunkQUI2X5JB_cjs.ConfigSchema.safeParse(config);
1240
1405
  if (!parsed.success) {
1241
- const issues = explainIssues(parsed.error.issues).map((i) => ({ path: i.path.join(".") || "root", message: i.message }));
1242
- throw new chunk66YSI4PX_cjs.PrismError("Invalid config", chunk66YSI4PX_cjs.ErrorCode.SCHEMA, { details: { issues } });
1406
+ const explained = explainIssues(parsed.error.issues, config);
1407
+ const issues = explained.map((i) => ({ path: i.path.join(".") || "root", message: i.message }));
1408
+ throw new chunkQUI2X5JB_cjs.PrismError(`Invalid config: ${issuesText(explained)}`, chunkQUI2X5JB_cjs.ErrorCode.SCHEMA, { details: { issues } });
1243
1409
  }
1244
1410
  return desugar(parsed.data);
1245
1411
  };
1246
- var run = (tree, source, limits, kept) => {
1412
+ var run = (tree, source, limits, kept, authored) => {
1247
1413
  const budget = createBudget(limits);
1248
- const result = evaluateNode(tree, kept ? { source, vars: {}, budget, kept } : { source, vars: {}, budget });
1249
- measure(result, budget);
1250
- return result;
1414
+ try {
1415
+ const result = evaluateNode(tree, kept ? { source, vars: {}, budget, kept } : { source, vars: {}, budget });
1416
+ measure(result, budget);
1417
+ return result;
1418
+ } catch (error) {
1419
+ placeError(error, tree, authored);
1420
+ throw error;
1421
+ }
1251
1422
  };
1252
1423
  var keptTrees = /* @__PURE__ */ new WeakMap();
1253
1424
  var evaluate = (config, source, options) => {
1254
1425
  if (options?.check === "always") {
1255
- if (!isJsonValue(source)) throw new chunk66YSI4PX_cjs.PrismError("The source must be plain JSON.", chunk66YSI4PX_cjs.ErrorCode.TYPE);
1256
- return run(checked(config), source, options.limits, false);
1426
+ const wrong = notJson(source, "source");
1427
+ if (wrong !== void 0) throw new chunkQUI2X5JB_cjs.PrismError(`The source must be plain JSON: ${wrong}.`, chunkQUI2X5JB_cjs.ErrorCode.TYPE);
1428
+ return run(checked(config), source, options.limits, false, config);
1257
1429
  }
1258
1430
  const input = source;
1259
- if (typeof config !== "object" || config === null) return run(checked(config), input, options?.limits, false);
1431
+ if (typeof config !== "object" || config === null) return run(checked(config), input, options?.limits, false, config);
1260
1432
  let kept = keptTrees.get(config);
1261
1433
  if (kept === void 0) {
1262
1434
  kept = { tree: checked(config), optimized: false };
@@ -1265,7 +1437,7 @@ var evaluate = (config, source, options) => {
1265
1437
  kept.tree = optimize(kept.tree, evaluateNode).node;
1266
1438
  kept.optimized = true;
1267
1439
  }
1268
- return run(kept.tree, input, options?.limits, true);
1440
+ return run(kept.tree, input, options?.limits, true, config);
1269
1441
  };
1270
1442
  var evaluateSafe = (config, source, options) => {
1271
1443
  try {
@@ -1287,7 +1459,7 @@ var walk = (node, stats, depth) => {
1287
1459
  for (const child of node) walk(child, stats, depth + 1);
1288
1460
  return;
1289
1461
  }
1290
- if (!chunk66YSI4PX_cjs.isJsonObject(node)) return;
1462
+ if (!chunkQUI2X5JB_cjs.isJsonObject(node)) return;
1291
1463
  for (const key of Object.keys(node)) {
1292
1464
  if (key.startsWith("$")) {
1293
1465
  stats.opCount[key] = (stats.opCount[key] ?? 0) + 1;
@@ -1307,11 +1479,10 @@ var hashSha256 = async (data) => {
1307
1479
  return Array.from(new Uint8Array(buffer)).map((b) => b.toString(16).padStart(2, "0")).join("");
1308
1480
  };
1309
1481
  var compile = async (config, options) => {
1310
- if (strata.exceedsDepth(config)) throw new chunk66YSI4PX_cjs.PrismError(`Invalid config: ${strata.depthRefusal()}`, chunk66YSI4PX_cjs.ErrorCode.SCHEMA);
1311
- const parsed = chunk66YSI4PX_cjs.ConfigSchema.safeParse(config);
1482
+ if (strata.exceedsDepth(config)) throw new chunkQUI2X5JB_cjs.PrismError(`Invalid config: ${strata.depthRefusal()}`, chunkQUI2X5JB_cjs.ErrorCode.SCHEMA);
1483
+ const parsed = chunkQUI2X5JB_cjs.ConfigSchema.safeParse(config);
1312
1484
  if (!parsed.success) {
1313
- const issues = explainIssues(parsed.error.issues).map((i) => `${i.path.join(".") || "(root)"}: ${i.message}`).join("; ");
1314
- throw new chunk66YSI4PX_cjs.PrismError(`Invalid config: ${issues}`, chunk66YSI4PX_cjs.ErrorCode.SCHEMA);
1485
+ throw new chunkQUI2X5JB_cjs.PrismError(`Invalid config: ${issuesText(explainIssues(parsed.error.issues, config))}`, chunkQUI2X5JB_cjs.ErrorCode.SCHEMA);
1315
1486
  }
1316
1487
  const desugared = desugar(parsed.data);
1317
1488
  const { node: core, stats: optimizations } = optimize(desugared, evaluateNode);
@@ -1358,19 +1529,22 @@ var execute = (ir, source, limits) => {
1358
1529
  hydrated.add(core);
1359
1530
  }
1360
1531
  const budget = createBudget(limits);
1361
- const result = evaluateNode(core, { source, vars: {}, budget });
1362
- measure(result, budget);
1363
- return result;
1532
+ try {
1533
+ const result = evaluateNode(core, { source, vars: {}, budget });
1534
+ measure(result, budget);
1535
+ return result;
1536
+ } catch (error) {
1537
+ placeError(error, core);
1538
+ throw error;
1539
+ }
1364
1540
  };
1365
1541
  var validate = (config) => {
1366
1542
  if (strata.exceedsDepth(config)) return { ok: false, issues: [{ path: [], message: strata.depthRefusal() }] };
1367
- const parsed = chunk66YSI4PX_cjs.ConfigSchema.safeParse(config);
1543
+ const parsed = chunkQUI2X5JB_cjs.ConfigSchema.safeParse(config);
1368
1544
  if (!parsed.success) {
1369
1545
  return {
1370
1546
  ok: false,
1371
- // The branch the config meant, with its full path — not the root union's
1372
- // "Invalid input" (see utils/issues).
1373
- issues: explainIssues(parsed.error.issues)
1547
+ issues: explainIssues(parsed.error.issues, config)
1374
1548
  };
1375
1549
  }
1376
1550
  return { ok: true, data: parsed.data };
@@ -1378,39 +1552,39 @@ var validate = (config) => {
1378
1552
 
1379
1553
  Object.defineProperty(exports, "MAPPING_OPS", {
1380
1554
  enumerable: true,
1381
- get: function () { return chunkOLZQK2ZF_cjs.MAPPING_OPS; }
1555
+ get: function () { return chunkROYHYYIL_cjs.MAPPING_OPS; }
1382
1556
  });
1383
1557
  Object.defineProperty(exports, "getConfigJsonSchema", {
1384
1558
  enumerable: true,
1385
- get: function () { return chunkOLZQK2ZF_cjs.getConfigJsonSchema; }
1559
+ get: function () { return chunkROYHYYIL_cjs.getConfigJsonSchema; }
1386
1560
  });
1387
1561
  Object.defineProperty(exports, "getNodeJsonSchema", {
1388
1562
  enumerable: true,
1389
- get: function () { return chunkOLZQK2ZF_cjs.getNodeJsonSchema; }
1563
+ get: function () { return chunkROYHYYIL_cjs.getNodeJsonSchema; }
1390
1564
  });
1391
1565
  Object.defineProperty(exports, "getProfileJsonSchema", {
1392
1566
  enumerable: true,
1393
- get: function () { return chunkOLZQK2ZF_cjs.getProfileJsonSchema; }
1567
+ get: function () { return chunkROYHYYIL_cjs.getProfileJsonSchema; }
1394
1568
  });
1395
1569
  Object.defineProperty(exports, "ConfigSchema", {
1396
1570
  enumerable: true,
1397
- get: function () { return chunk66YSI4PX_cjs.ConfigSchema; }
1571
+ get: function () { return chunkQUI2X5JB_cjs.ConfigSchema; }
1398
1572
  });
1399
1573
  Object.defineProperty(exports, "ErrorCode", {
1400
1574
  enumerable: true,
1401
- get: function () { return chunk66YSI4PX_cjs.ErrorCode; }
1575
+ get: function () { return chunkQUI2X5JB_cjs.ErrorCode; }
1402
1576
  });
1403
1577
  Object.defineProperty(exports, "NodeSchema", {
1404
1578
  enumerable: true,
1405
- get: function () { return chunk66YSI4PX_cjs.NodeSchema; }
1579
+ get: function () { return chunkQUI2X5JB_cjs.NodeSchema; }
1406
1580
  });
1407
1581
  Object.defineProperty(exports, "OP_KEYS", {
1408
1582
  enumerable: true,
1409
- get: function () { return chunk66YSI4PX_cjs.OP_KEYS; }
1583
+ get: function () { return chunkQUI2X5JB_cjs.OP_KEYS; }
1410
1584
  });
1411
1585
  Object.defineProperty(exports, "PrismError", {
1412
1586
  enumerable: true,
1413
- get: function () { return chunk66YSI4PX_cjs.PrismError; }
1587
+ get: function () { return chunkQUI2X5JB_cjs.PrismError; }
1414
1588
  });
1415
1589
  exports.DEFAULT_LIMITS = DEFAULT_LIMITS;
1416
1590
  exports.compile = compile;