mirror of
https://github.com/morluto/rea.git
synced 2026-11-18 20:53:00 +00:00
fix(cli): bound advertised decimal-token schemas by the domain range (#9808)
`cliDecimalField` published the domain range on the integer branch and only the fixed `^[0-9]+$` guard on the decimal-token branch, and JSON Schema bounds do not constrain strings. A client validating raw argv text against `--schema` therefore admitted out-of-range tokens such as `--row-limit=0` or `--block-size=255` that the parser rejects with `VALIDATION_ERROR`. Publish the domain range on the token branch through the schema-constraint owner, keeping the CLI's decimal leading-zero convention and the existing parser diagnostics: the declaration is metadata, so accepted values and `too_small`/`too_big` messages are unchanged. Non-range refinements such as the power-of-two entropy block size stay parser-only, and an open-ended domain still publishes its floor. The getting-started guide states the same contract. Fixes #9348
This commit is contained in:
1 parent
41cf9654da
commit
80f533da8a
9 files changed
+338
-61
No files matched your search
@@ -0,0 +1,83 @@
|
||||
import { z } from "zod";
|
||||
|
||||
const digits = (count: number): string =>
|
||||
count === 0 ? "" : count === 1 ? "[0-9]" : `[0-9]{${count}}`;
|
||||
|
||||
const digitRange = (low: number, high: number): string =>
|
||||
low === high ? String(low) : `[${low}-${high}]`;
|
||||
|
||||
/** Lexicographic bounds for equal-width, unsigned decimal strings. */
|
||||
const sameWidthRange = (low: string, high: string): string => {
|
||||
if (low === high) return low;
|
||||
const width = low.length;
|
||||
if (low === "0".repeat(width) && high === "9".repeat(width))
|
||||
return digits(width);
|
||||
const first = Number(low[0]);
|
||||
const last = Number(high[0]);
|
||||
if (width === 1) return digitRange(first, last);
|
||||
if (
|
||||
low.slice(1) === "0".repeat(width - 1) &&
|
||||
high.slice(1) === "9".repeat(width - 1)
|
||||
)
|
||||
return `${digitRange(first, last)}${digits(width - 1)}`;
|
||||
if (first === last)
|
||||
return `${first}${sameWidthRange(low.slice(1), high.slice(1))}`;
|
||||
const branches = [
|
||||
`${first}${sameWidthRange(low.slice(1), "9".repeat(width - 1))}`,
|
||||
];
|
||||
if (first + 1 <= last - 1)
|
||||
branches.push(`${digitRange(first + 1, last - 1)}${digits(width - 1)}`);
|
||||
branches.push(
|
||||
`${last}${sameWidthRange("0".repeat(width - 1), high.slice(1))}`,
|
||||
);
|
||||
return `(?:${branches.join("|")})`;
|
||||
};
|
||||
|
||||
/** Keep the floor of an open-ended domain, whose tokens may grow past it. */
|
||||
const openEndedPattern = (minimum: number): string => {
|
||||
if (minimum === 0) return "^[0-9]+$";
|
||||
const low = String(minimum);
|
||||
const atFloorWidth = sameWidthRange(low, "9".repeat(low.length));
|
||||
return `^0*(?:${atFloorWidth}|[1-9][0-9]{${String(low.length)},})$`;
|
||||
};
|
||||
|
||||
/**
|
||||
* Publish the domain's numeric range on the decimal-token branch without
|
||||
* changing parser diagnostics. Refinements that are not a range stay
|
||||
* parser-only, so an advertised schema can admit a token the parser rejects;
|
||||
* it never rejects a token the parser admits.
|
||||
*/
|
||||
export const decimalTokenPattern = (domain: z.ZodNumber): string => {
|
||||
const schema = z.toJSONSchema(domain);
|
||||
const minimum = Math.max(
|
||||
0,
|
||||
Math.ceil(schema.minimum ?? 0),
|
||||
typeof schema.exclusiveMinimum === "number"
|
||||
? Math.floor(schema.exclusiveMinimum) + 1
|
||||
: 0,
|
||||
);
|
||||
const maximum = Math.min(
|
||||
Math.floor(schema.maximum ?? Infinity),
|
||||
typeof schema.exclusiveMaximum === "number"
|
||||
? Math.ceil(schema.exclusiveMaximum) - 1
|
||||
: Infinity,
|
||||
);
|
||||
// Non-integer floors and ceilings have no safe-integer range to publish.
|
||||
if (!Number.isSafeInteger(minimum)) return "^[0-9]+$";
|
||||
if (maximum === Infinity) return openEndedPattern(minimum);
|
||||
if (!Number.isSafeInteger(maximum)) return "^[0-9]+$";
|
||||
if (minimum > maximum) return "^(?!)$";
|
||||
const low = String(minimum);
|
||||
const high = String(maximum);
|
||||
const branches: string[] = [];
|
||||
for (let width = low.length; width <= high.length; width++) {
|
||||
branches.push(
|
||||
sameWidthRange(
|
||||
width === low.length ? low : `1${"0".repeat(width - 1)}`,
|
||||
width === high.length ? high : "9".repeat(width),
|
||||
),
|
||||
);
|
||||
}
|
||||
// Tokens retain the CLI's decimal leading-zero convention.
|
||||
return `^0*(?:${branches.join("|")})$`;
|
||||
};
|
||||
+7
-3
@@ -1,5 +1,8 @@
|
||||
import { z } from "zod";
|
||||
|
||||
import { declaredTokenPattern } from "../domain/schemaConstraints.js";
|
||||
import { decimalTokenPattern } from "./decimalTokenPattern.js";
|
||||
|
||||
import { OPEN_BINARY_TOOL_CONTRACT } from "../contracts/sessionToolContracts.js";
|
||||
import type { CliSessionTarget } from "./contractCliDescription.js";
|
||||
|
||||
@@ -14,9 +17,10 @@ type DecimalWrapper =
|
||||
};
|
||||
|
||||
const decimalToken = (domain: z.ZodNumber) =>
|
||||
z
|
||||
.string()
|
||||
.regex(DECIMAL_INTEGER_TOKEN, "Expected a decimal integer")
|
||||
declaredTokenPattern(
|
||||
z.string().regex(DECIMAL_INTEGER_TOKEN, "Expected a decimal integer"),
|
||||
new RegExp(decimalTokenPattern(domain), "u"),
|
||||
)
|
||||
.pipe(z.coerce.number<string>())
|
||||
.pipe(domain);
|
||||
|
||||
|
||||
Reference in new issue
Block a user