mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-03 01:28:55 +00:00
fix(shared): count an open shape once per path, and see maps, sets and custom
openShapes() skipped every schema node it had already visited, so one z.unknown() instance reused across four fields counted once, and a baselined schema could gain any number of open fields that share a node without its count moving. z.map(), z.set() and z.custom() were not walked at all, so an open value inside them counted nothing. An open spot now counts once per path it sits at, and the visited set is only a guard on the ancestor chain, so a recursive z.lazy() still terminates. The same kind at the same path is listed once, which keeps the two sides of an intersection from counting a shared field twice. Map keys and values and set values are walked, and z.custom()/z.instanceof() count as a custom open shape. The request schemas hold the same 75 open shapes under the new walk, so the baseline does not change; the spec covers reuse, recursion and the new types.
This commit is contained in:
@@ -35,6 +35,30 @@ describe('open request schemas', () => {
|
||||
expect(openShapes(z.array(z.object({ meta: z.record(z.string(), z.any()) })))).toEqual(['record@$.meta']);
|
||||
expect(openShapes(z.union([z.string(), z.object({ x: z.unknown() })]))).toEqual(['unknown@$|1.x']);
|
||||
expect(openShapes(z.object({ x: z.unknown() }).nullable().default(null))).toEqual(['unknown@$.x']);
|
||||
expect(openShapes(z.object({ a: z.map(z.string(), z.unknown()), b: z.set(z.any()) }))).toEqual([
|
||||
'unknown@$.a{}',
|
||||
'unknown@$.b',
|
||||
]);
|
||||
expect(openShapes(z.object({ a: z.custom<string>(), b: z.instanceof(Date) }))).toEqual([
|
||||
'custom@$.a',
|
||||
'custom@$.b',
|
||||
]);
|
||||
});
|
||||
|
||||
it('OPEN-003b: one open node reused across fields counts at every path it sits at', () => {
|
||||
const anything = z.unknown().optional();
|
||||
expect(openShapes(z.object({ a: anything, b: anything, c: anything, d: anything }))).toHaveLength(4);
|
||||
const meta = z.record(z.string(), z.unknown());
|
||||
expect(openShapes(z.object({ x: meta, y: z.array(meta) }))).toEqual(['record@$.x', 'record@$.y']);
|
||||
// Both sides of an intersection naming the same open field still count it once.
|
||||
const left = z.object({ x: z.unknown() });
|
||||
expect(openShapes(left.and(z.object({ x: z.unknown() })))).toEqual(['unknown@$.x']);
|
||||
});
|
||||
|
||||
it('OPEN-003c: a recursive schema is walked once per level and terminates', () => {
|
||||
type Node = { meta?: unknown; children: Node[] };
|
||||
const node: z.ZodType<Node> = z.lazy(() => z.object({ meta: z.unknown().optional(), children: z.array(node) }));
|
||||
expect(openShapes(node)).toEqual(['unknown@$.meta']);
|
||||
});
|
||||
|
||||
it('OPEN-004: a closed schema counts nothing', () => {
|
||||
|
||||
Reference in New Issue
Block a user