|
| 1 | +import * as z from 'zod' |
| 2 | +import { JSON_SCHEMA_INPUT_REGISTRY, JSON_SCHEMA_OUTPUT_REGISTRY, JSON_SCHEMA_REGISTRY } from './registries' |
| 3 | + |
| 4 | +const user = z.object({ |
| 5 | + name: z.string(), |
| 6 | + age: z.string().transform(v => Number(v)), |
| 7 | +}) |
| 8 | + |
| 9 | +describe('JSON_SCHEMA_REGISTRY', () => { |
| 10 | + it('accepts both input and output shapes', () => { |
| 11 | + JSON_SCHEMA_REGISTRY.add(user, { examples: [{ name: 'John', age: '20' }] }) |
| 12 | + JSON_SCHEMA_REGISTRY.add(user, { examples: [{ name: 'John', age: 20 }] }) |
| 13 | + JSON_SCHEMA_REGISTRY.add(user, { default: { name: 'John', age: '20' } }) |
| 14 | + JSON_SCHEMA_REGISTRY.add(user, { default: { name: 'John', age: 20 } }) |
| 15 | + |
| 16 | + // @ts-expect-error --- age must match input or output type |
| 17 | + JSON_SCHEMA_REGISTRY.add(user, { examples: [{ name: 'John', age: true }] }) |
| 18 | + // @ts-expect-error --- age is required |
| 19 | + JSON_SCHEMA_REGISTRY.add(user, { default: { name: 'John' } }) |
| 20 | + }) |
| 21 | +}) |
| 22 | + |
| 23 | +describe('JSON_SCHEMA_INPUT_REGISTRY', () => { |
| 24 | + it('only accepts input shapes', () => { |
| 25 | + JSON_SCHEMA_INPUT_REGISTRY.add(user, { examples: [{ name: 'John', age: '20' }] }) |
| 26 | + JSON_SCHEMA_INPUT_REGISTRY.add(user, { default: { name: 'John', age: '20' } }) |
| 27 | + |
| 28 | + // @ts-expect-error --- age must be the input type (string) |
| 29 | + JSON_SCHEMA_INPUT_REGISTRY.add(user, { examples: [{ name: 'John', age: 20 }] }) |
| 30 | + // @ts-expect-error --- age must be the input type (string) |
| 31 | + JSON_SCHEMA_INPUT_REGISTRY.add(user, { default: { name: 'John', age: 20 } }) |
| 32 | + }) |
| 33 | +}) |
| 34 | + |
| 35 | +describe('JSON_SCHEMA_OUTPUT_REGISTRY', () => { |
| 36 | + it('only accepts output shapes', () => { |
| 37 | + JSON_SCHEMA_OUTPUT_REGISTRY.add(user, { examples: [{ name: 'John', age: 20 }] }) |
| 38 | + JSON_SCHEMA_OUTPUT_REGISTRY.add(user, { default: { name: 'John', age: 20 } }) |
| 39 | + |
| 40 | + // @ts-expect-error --- age must be the output type (number) |
| 41 | + JSON_SCHEMA_OUTPUT_REGISTRY.add(user, { examples: [{ name: 'John', age: '20' }] }) |
| 42 | + // @ts-expect-error --- age must be the output type (number) |
| 43 | + JSON_SCHEMA_OUTPUT_REGISTRY.add(user, { default: { name: 'John', age: '20' } }) |
| 44 | + }) |
| 45 | +}) |
0 commit comments