@turingcode/cli 0.2.9 → 0.2.11

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/README.md CHANGED
@@ -126,4 +126,8 @@ is a value or an operator object. As in MongoDB:
126
126
 
127
127
  Other operators (`$where`, `$exists`, ...) are rejected by `contract check`. Each top-level operator is
128
128
  checked and reported on its own, with a specific code (`$ref` → `UNKNOWN_REF`, `$regex` → `PATTERN`,
129
- `$gte` → `OUT_OF_RANGE`, `{"$ne": null}` → `REQUIRED_IF`, ...); a failing `$or` reports `MATCH`.
129
+ `$gte` → `OUT_OF_RANGE`, `{"$ne": null}` → `REQUIRED_IF`, ...). A value that fails every branch of an
130
+ `$or` reports the branches' code when they all fail with the same one (two `$regex` → `PATTERN`);
131
+ otherwise it reports `NONE_OF`, with each branch's code in the message
132
+ (`'zz' is neither (matches ^A-) [PATTERN] nor (one of x, y) [BAD_VALUE]`) and, in `--json`, in
133
+ `alternatives: [{code, message}]`.
@@ -29,10 +29,20 @@ export function readJsonFile(path) {
29
29
  *
30
30
  * @param {string} value - Raw flag value.
31
31
  * @returns {unknown} The parsed JSON.
32
- * @throws {Error} When the file cannot be read or the JSON is invalid.
32
+ * @throws {Error} When the value is neither inline JSON nor an existing file, the file cannot be read, or the JSON is invalid.
33
33
  */
34
34
  export function readJsonArg(value) {
35
- const text = /^\s*[{[]/.test(value) ? value : readFileSync(value, "utf-8");
35
+ let text = value;
36
+ if (!/^\s*[{[]/.test(value)) {
37
+ try {
38
+ text = readFileSync(value, "utf-8");
39
+ }
40
+ catch (e) {
41
+ if (e.code !== "ENOENT")
42
+ throw e;
43
+ throw new Error(`'${value}' is neither inline JSON (starting with {) nor an existing .json file`);
44
+ }
45
+ }
36
46
  try {
37
47
  return JSON.parse(text);
38
48
  }
@@ -37,6 +37,7 @@ const HINTS = {
37
37
  "ROW_COUNT": "Add or remove rows so the count fits.",
38
38
  "NOT_MENTIONED": "Mention the value where the rule says.",
39
39
  "MATCH": "Correct the row so it matches the rule's query (shown in 'expected').",
40
+ "NONE_OF": "Make the value meet one of the alternatives in 'expected'.",
40
41
  // Inputs.
41
42
  "MISSING_FILE": "Check the path; the file must exist and be readable.",
42
43
  "MISSING_DEPENDENCY": "Pass the dependency's tables with --dep <alias>=<file>.",
@@ -52,12 +53,13 @@ const HINTS = {
52
53
  * @param {string} found - Observed value.
53
54
  * @param {string} expected - Required value.
54
55
  * @param {"FAIL"|"WARN"} [severity="FAIL"] - Severity.
56
+ * @param {Alternative[]} [alternatives] - For NONE_OF: why each `$or` branch failed.
55
57
  * @returns {Diagnostic} The diagnostic.
56
58
  */
57
- export function diag(code, message, at, found, expected, severity = "FAIL") {
59
+ export function diag(code, message, at, found, expected, severity = "FAIL", alternatives) {
58
60
  return {
59
61
  severity, code, table: at.table ?? "-", row: at.row ?? "-", column: at.column ?? "-", message, found, expected,
60
- fix: HINTS[code], ...(at.rule ? { rule: at.rule } : {}),
62
+ fix: HINTS[code], ...(at.rule ? { rule: at.rule } : {}), ...(alternatives ? { alternatives } : {}),
61
63
  };
62
64
  }
63
65
  /**
@@ -196,13 +196,19 @@ function explainFirst(column, cond, value, hooks) {
196
196
  }
197
197
  /**
198
198
  * Explains a failing `$or` of column conditions. When exactly one alternative blames part of the value
199
- * (e.g. one bad item of a list), its explanation is used; otherwise the value meets none of them.
199
+ * (e.g. one bad item of a list), its explanation is used. Otherwise the value meets none of them: when
200
+ * every alternative fails with the same code, that code is reported; when the codes differ, NONE_OF is,
201
+ * with each alternative's code in the message and in `alternatives`.
200
202
  *
201
203
  * @param {string} column - The column.
202
204
  * @param {unknown[]} alternatives - The `$or` conditions.
203
205
  * @param {Value} value - The value.
204
206
  * @param {CheckHooks} hooks - The hooks the query ran with.
205
207
  * @returns {Failure} The explanation; `expected` lists every alternative.
208
+ *
209
+ * @example
210
+ * // {"$or": [{"$regex": "^A-"}, {"$in": ["x", "y"]}]} on 'zz':
211
+ * // NONE_OF "'zz' is neither (matches ^A-) [PATTERN] nor (one of x, y) [BAD_VALUE]"
206
212
  */
207
213
  function explainOr(column, alternatives, value, hooks) {
208
214
  const text = valueText(value);
@@ -211,9 +217,11 @@ function explainOr(column, alternatives, value, hooks) {
211
217
  const narrowed = failures.filter(f => f.code !== "MATCH" && f.found !== text && f.found !== "");
212
218
  if (narrowed.length === 1)
213
219
  return { ...narrowed[0], expected };
214
- const names = alternatives.map(alt => `(${renderCondition(alt, plain)})`);
220
+ const shared = failures.every(f => f.code === failures[0].code) ? failures[0].code : null;
221
+ const names = alternatives.map((alt, i) => `(${renderCondition(alt, plain)})${shared ? "" : ` [${failures[i].code}]`}`);
215
222
  const none = names.length === 2 ? `neither ${names[0]} nor ${names[1]}` : `none of ${names.join(", ")}`;
216
- return { code: "MATCH", message: `'${text}' is ${none}`, column, found: text, expected };
223
+ const failure = { code: shared ?? "NONE_OF", message: `'${text}' is ${none}`, column, found: text, expected };
224
+ return shared ? failure : { ...failure, alternatives: failures.map(f => ({ code: f.code, message: f.message })) };
217
225
  }
218
226
  /**
219
227
  * Explains why one column's value fails a single-operator condition.
@@ -217,7 +217,7 @@ export function tableCheck(input) {
217
217
  continue;
218
218
  const hooks = hooksFor(t, true);
219
219
  for (const col of t.columns.filter(c => c.match)) {
220
- runParts({ [col.name]: col.match }, tableRows, hooks, r => r.shapeError || r.doc[col.name] === null || !!badCells.get(r)?.has(col.name), (f, r) => diags.push(diag(f.code, f.message, { table: t.name, row: r.label, column: f.column }, f.found, f.expected)));
220
+ runParts({ [col.name]: col.match }, tableRows, hooks, r => r.shapeError || r.doc[col.name] === null || !!badCells.get(r)?.has(col.name), (f, r) => diags.push(diag(f.code, f.message, { table: t.name, row: r.label, column: f.column }, f.found, f.expected, "FAIL", f.alternatives)));
221
221
  }
222
222
  (t.rules ?? []).forEach((rule, i) => checkRule(rule, `${t.name}.rules[${i}]`, t));
223
223
  }
@@ -248,7 +248,7 @@ export function tableCheck(input) {
248
248
  }
249
249
  return;
250
250
  }
251
- runParts(rule.require, matching, hooks, r => r.shapeError, (f, r) => diags.push(diag(f.code, f.message, { ...where, row: r.label, column: f.column }, f.found, rule.description ?? f.expected, severity)));
251
+ runParts(rule.require, matching, hooks, r => r.shapeError, (f, r) => diags.push(diag(f.code, f.message, { ...where, row: r.label, column: f.column }, f.found, rule.description ?? f.expected, severity, f.alternatives)));
252
252
  }
253
253
  }
254
254
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@turingcode/cli",
3
- "version": "0.2.9",
3
+ "version": "0.2.11",
4
4
  "description": "Turing Code toolkit for AI agents: check Markdown/CSV tables against table contracts, e.g. `turingcode table check`.",
5
5
  "type": "module",
6
6
  "bin": {