Skip to content

Commit 5266131

Browse files
fix: isZodRawShape treats empty object as raw shape (matches v1)
1 parent 27e4ddf commit 5266131

2 files changed

Lines changed: 23 additions & 3 deletions

File tree

packages/core/src/util/standardSchema.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,8 @@ export function isStandardSchemaWithJSON(schema: unknown): schema is StandardSch
148148
export function isZodRawShape(obj: unknown): obj is Record<string, StandardSchemaV1> {
149149
if (typeof obj !== 'object' || obj === null) return false;
150150
if (isStandardSchema(obj)) return false;
151-
const values = Object.values(obj);
152-
return values.length > 0 && values.every(v => isStandardSchema(v) || (typeof v === 'object' && v !== null && '_def' in v));
151+
// [].every() is true, so an empty object is a valid raw shape (matches v1).
152+
return Object.values(obj).every(v => isStandardSchema(v) || (typeof v === 'object' && v !== null && '_def' in v));
153153
}
154154

155155
/**

packages/core/test/util/standardSchema.test.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,26 @@
11
import * as z from 'zod/v4';
22

3-
import { standardSchemaToJsonSchema } from '../../src/util/standardSchema.js';
3+
import { isZodRawShape, normalizeRawShapeSchema, standardSchemaToJsonSchema } from '../../src/util/standardSchema.js';
4+
5+
describe('isZodRawShape', () => {
6+
test('treats empty object as a raw shape (matches v1)', () => {
7+
expect(isZodRawShape({})).toBe(true);
8+
});
9+
test('detects raw shape with zod fields', () => {
10+
expect(isZodRawShape({ a: z.string() })).toBe(true);
11+
});
12+
test('rejects a Standard Schema instance', () => {
13+
expect(isZodRawShape(z.object({ a: z.string() }))).toBe(false);
14+
});
15+
});
16+
17+
describe('normalizeRawShapeSchema', () => {
18+
test('wraps empty raw shape into z.object({})', () => {
19+
const wrapped = normalizeRawShapeSchema({});
20+
expect(wrapped).toBeDefined();
21+
expect(standardSchemaToJsonSchema(wrapped!, 'input').type).toBe('object');
22+
});
23+
});
424

525
describe('standardSchemaToJsonSchema', () => {
626
test('emits type:object for plain z.object schemas', () => {

0 commit comments

Comments
 (0)