From c566e33793a1c66feedde887a46916a5a00fea4a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CSebastian?= <64795732+slegarraga@users.noreply.github.com> Date: Thu, 6 Aug 2026 05:20:06 -0400 Subject: [PATCH] fix: guard invalid json schema patterns --- src/scorers/json-schema.ts | 10 ++++++++-- tests/scorers.test.ts | 9 +++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/scorers/json-schema.ts b/src/scorers/json-schema.ts index c299302..d59fd80 100644 --- a/src/scorers/json-schema.ts +++ b/src/scorers/json-schema.ts @@ -56,8 +56,14 @@ export function validate(value: unknown, schema: JsonSchema, path = "$"): string errors.push(`${path}: shorter than minLength ${schema.minLength}`); if (schema.maxLength !== undefined && value.length > schema.maxLength) errors.push(`${path}: longer than maxLength ${schema.maxLength}`); - if (schema.pattern && !new RegExp(schema.pattern).test(value)) - errors.push(`${path}: does not match pattern ${schema.pattern}`); + if (schema.pattern) { + try { + if (!new RegExp(schema.pattern).test(value)) + errors.push(`${path}: does not match pattern ${schema.pattern}`); + } catch (err) { + errors.push(`${path}: invalid pattern ${schema.pattern}: ${(err as Error).message}`); + } + } } if (isPlainObject(value)) { diff --git a/tests/scorers.test.ts b/tests/scorers.test.ts index b53c6a2..1cdcb6d 100644 --- a/tests/scorers.test.ts +++ b/tests/scorers.test.ts @@ -107,6 +107,15 @@ describe("json-schema", () => { ); expect(errs.length).toBe(2); }); + it("fails cleanly when a schema pattern is invalid", async () => { + const r = await run( + jsonSchemaScorer, + { type: "json-schema", schema: { type: "string", pattern: "[a-z" } }, + ctx('"abc"'), + ); + expect(r.passed).toBe(false); + expect(r.reason).toContain("invalid pattern"); + }); }); describe("embedding-similarity", () => {