|
| 1 | +import * as z from 'zod/v4'; |
| 2 | + |
| 3 | +import { standardSchemaToJsonSchema } from '../../src/util/standardSchema.js'; |
| 4 | +import { isZodRawShape, normalizeRawShapeSchema } from '../../src/util/zodCompat.js'; |
| 5 | + |
| 6 | +describe('isZodRawShape', () => { |
| 7 | + test('treats empty object as a raw shape (matches v1)', () => { |
| 8 | + expect(isZodRawShape({})).toBe(true); |
| 9 | + }); |
| 10 | + test('detects raw shape with zod fields', () => { |
| 11 | + expect(isZodRawShape({ a: z.string() })).toBe(true); |
| 12 | + }); |
| 13 | + test('rejects a Standard Schema instance', () => { |
| 14 | + expect(isZodRawShape(z.object({ a: z.string() }))).toBe(false); |
| 15 | + }); |
| 16 | + test('rejects a shape with non-Zod Standard Schema fields', () => { |
| 17 | + const nonZod = { '~standard': { version: 1, vendor: 'arktype', validate: () => ({ value: 'x' }) } }; |
| 18 | + expect(isZodRawShape({ a: nonZod })).toBe(false); |
| 19 | + }); |
| 20 | +}); |
| 21 | + |
| 22 | +describe('normalizeRawShapeSchema', () => { |
| 23 | + test('wraps empty raw shape into z.object({})', () => { |
| 24 | + const wrapped = normalizeRawShapeSchema({}); |
| 25 | + expect(wrapped).toBeDefined(); |
| 26 | + expect(standardSchemaToJsonSchema(wrapped!, 'input').type).toBe('object'); |
| 27 | + }); |
| 28 | + test('passes through an already-wrapped Standard Schema unchanged', () => { |
| 29 | + const schema = z.object({ a: z.string() }); |
| 30 | + expect(normalizeRawShapeSchema(schema)).toBe(schema); |
| 31 | + }); |
| 32 | + test('returns undefined for undefined input', () => { |
| 33 | + expect(normalizeRawShapeSchema(undefined)).toBeUndefined(); |
| 34 | + }); |
| 35 | + test('throws TypeError for an invalid object that is neither raw shape nor Standard Schema', () => { |
| 36 | + expect(() => normalizeRawShapeSchema({ a: 'not a zod schema' } as never)).toThrow(TypeError); |
| 37 | + }); |
| 38 | + test('throws TypeError for a Standard Schema without JSON Schema export', () => { |
| 39 | + const noJson = { '~standard': { version: 1, vendor: 'x', validate: () => ({ value: {} }) } }; |
| 40 | + expect(() => normalizeRawShapeSchema(noJson as never)).toThrow(/~standard\.jsonSchema/); |
| 41 | + }); |
| 42 | +}); |
0 commit comments