Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
fix(json-schema-to-zod): rename $ref pattern keys only in keyword position
A local $ref through a definition named patternProperties was mistaken for the keyword, so its next segment was renamed to a key the interpreter copy never renamed and the guard failed on a dangling reference. Track whether each pointer segment is a keyword, a map name, a patternProperties key, or an array index, and rename only patternProperties keys.
jkomyno committed
Sep 29, 2026 at 15:44 UTC
ef7d7133e9018c02cd0746cbc5de735198cb0811
2 files changed
+57
-19
ts/packages/json-schema-to-zod/src/whole-schema-validation.ts
+45
-19
@@ -199,36 +199,62 @@ const decodePointerSegment = (segment: string): string => {
199
}
200
};
201
202
+/**
203
+ * What a JSON Pointer segment addresses: a keyword of a schema, a name in a
204
+ * keyword's map (`properties`, `$defs`, ...), a `patternProperties` key, an
205
+ * index into a schema array, or something that holds no schemas.
206
+ */
207
+type PointerPosition = 'keyword' | 'name' | 'patternKey' | 'index' | 'other';
208
+
209
+const positionAfter = (position: PointerPosition, key: string, child: unknown): PointerPosition => {
210
+ if (position !== 'keyword') {
211
+ return position === 'other' ? 'other' : 'keyword';
212
+ }
213
+ if (key === 'patternProperties') {
214
+ return 'patternKey';
215
+ }
216
+ if (SCHEMA_MAP_KEYWORDS.has(key)) {
217
+ return 'name';
218
+ }
219
+ if (SCHEMA_ARRAY_KEYWORDS.has(key) || (SCHEMA_VALUE_KEYWORDS.has(key) && Array.isArray(child))) {
220
+ return 'index';
221
+ }
222
+ return SCHEMA_VALUE_KEYWORDS.has(key) ? 'keyword' : 'other';
223
+};
224
+
225
/**
226
* A local `$ref` rewritten to address the renamed `patternProperties` keys of
204
- * the interpreter copy. `root` is the unrenamed document it points into.
227
+ * the interpreter copy. `root` is the unrenamed document it points into. Only
228
+ * a segment in keyword position is a keyword, so a definition that happens to
229
+ * be named `patternProperties` is not mistaken for one.
230
*/
231
const renameRefThroughPatternKeys = (ref: string, root: unknown): string => {
232
if (!ref.startsWith('#/')) {
233
return ref;
234
}
235
211
- const segments = ref.slice(2).split('/');
236
let node: unknown = root;
237
+ let position: PointerPosition = 'keyword';
238
let changed = false;
214
- const renamed = segments.map((segment, index) => {
215
- const key = decodePointerSegment(segment);
216
- let result = segment;
217
- if (
218
- index > 0 &&
219
- decodePointerSegment(segments[index - 1]) === 'patternProperties' &&
220
- isObject(node)
221
- ) {
222
- const renamedKey = renamePatternKeys(node).get(key);
223
- if (renamedKey !== undefined && renamedKey !== key) {
224
- result = encodePointer(renamedKey);
225
- changed = true;
239
+ const renamed = ref
240
+ .slice(2)
241
+ .split('/')
242
+ .map(segment => {
243
+ const key = decodePointerSegment(segment);
244
+ let result = segment;
245
+ if (position === 'patternKey' && isObject(node)) {
246
+ const renamedKey = renamePatternKeys(node).get(key);
247
+ if (renamedKey !== undefined && renamedKey !== key) {
248
+ result = encodePointer(renamedKey);
249
+ changed = true;
250
+ }
251
}
227
- }
228
- node =
229
- isObject(node) || Array.isArray(node) ? (node as Record<string, unknown>)[key] : undefined;
230
- return result;
231
- });
252
+ const child =
253
+ isObject(node) || Array.isArray(node) ? (node as Record<string, unknown>)[key] : undefined;
254
+ position = positionAfter(position, key, child);
255
+ node = child;
256
+ return result;
257
+ });
258
259
return changed ? `#/${renamed.join('/')}` : ref;
260
};
ts/packages/json-schema-to-zod/test/semantic-regressions.test.ts
+12
@@ -400,6 +400,18 @@ describe('whole-schema semantic regressions', () => {
400
expect(parsed.safeParse({ x_1: 1 }).success).toBe(false);
401
});
402
403
+ it('does not mistake a definition named patternProperties for the keyword', () => {
404
+ const schema = {
405
+ type: 'object',
406
+ properties: { v: { $ref: `#/$defs/patternProperties/${encodeURI('^a\\_b$')}` } },
407
+ $defs: { patternProperties: { '^a\\_b$': { type: 'string', minLength: 2 } } },
408
+ } as unknown as JsonSchema;
409
+ const parsed = jsonSchemaToZod(schema);
410
+
411
+ expect(parsed.safeParse({ v: 'ok' }).success).toBe(true);
412
+ expect(parsed.safeParse({ v: 'x' }).success).toBe(false);
413
+ });
414
+
415
it('compares decimal multiples by their JSON number spelling', () => {
416
const schema: JsonSchema = { type: 'number', multipleOf: 0.1 };
417
expect(jsonSchemaToZod(schema).safeParse(0.3).success).toBe(true);