fix(captures): name the violated HAR schema constraint (#2708)

HAR validation reported only the AJV keyword, so a capture missing
`log.version` failed with "HAR schema validation failed: required." and
the caller could not tell which property to supply. Include AJV's
constraint message, which names the missing property, expected type, or
pattern without restating captured values.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Kaoru0822 2026-10-13 01:22:24 -07:00 committed by GitHub
parent 11f8b76257
commit b69d20a300
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 18 additions and 1 deletions

View File

@ -162,6 +162,19 @@ it("rejects identical duplicate core fields before schema validation", () => {
expect(() => decodeHarCapture(text, [])).toThrow("duplicate");
});
it("names the violated HAR schema constraint without restating capture values", () => {
expect(() =>
decodeHarCapture(
JSON.stringify({
log: { creator: { name: "x", version: "1" }, entries: [] },
}),
[],
),
).toThrow(
"HAR schema validation failed: required (must have required property 'version').",
);
});
it("preserves prototype-named producer members through the capture boundary", () => {
const text = JSON.stringify(historicalHar()).replace(
'"cache":{}',

View File

@ -23,9 +23,13 @@ export const createHarValidator = (): ((value: unknown) => void) => {
allowMitmproxyAbsentPostData(value);
if (!validate(value)) {
const error = validate.errors?.[0];
// AJV messages name the violated constraint, such as the missing
// property, without restating any captured value.
throw new CaptureFormatError(
"format",
`HAR schema validation failed: ${error?.keyword ?? "unknown constraint"}.`,
error === undefined
? "HAR schema validation failed: unknown constraint."
: `HAR schema validation failed: ${error.keyword} (${error.message ?? "no detail"}).`,
error?.instancePath ?? "",
);
}