-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson-schema-to-zod.spec.ts
More file actions
93 lines (85 loc) · 3.12 KB
/
Copy pathjson-schema-to-zod.spec.ts
File metadata and controls
93 lines (85 loc) · 3.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import { describe, it, expect } from 'vitest';
import { convertSchema } from '../src/utils/json-schema-to-zod';
import type { OpenAPIV3_1 as OpenAPI } from 'openapi-types';
describe('convertSchema', () => {
it('converts enums', () => {
const { zodString, imports } = convertSchema({
type: 'string',
enum: ['a', 'b']
} as OpenAPI.SchemaObject);
expect(zodString).toBe('z.enum(["a", "b"])');
expect(imports.size).toBe(0);
});
it('converts arrays of refs', () => {
const { zodString, imports } = convertSchema({
type: 'array',
items: { $ref: '#/components/schemas/User' }
} as OpenAPI.SchemaObject);
expect(zodString).toBe('z.array(User)');
expect(imports.has('User')).toBe(true);
});
it('converts objects with optional fields', () => {
const schema = {
type: 'object',
properties: { id: { type: 'string' }, name: { type: 'string' } },
required: ['id']
} as OpenAPI.SchemaObject;
const { zodString } = convertSchema(schema);
expect(zodString).toBe('z.object({\n id: z.string(),\n name: z.string().optional()\n})');
});
it('adds descriptions as meta', () => {
const schema = {
type: 'object',
description: 'User object',
properties: {
id: { type: 'string', description: 'identifier' }
},
required: ['id']
} as OpenAPI.SchemaObject;
const { zodString } = convertSchema(schema);
expect(zodString).toBe('z.object({\n id: z.string().meta({ description: "identifier" })\n})');
});
it('handles allOf with refs and properties', () => {
const schema = {
allOf: [{ $ref: '#/components/schemas/Base' }, { type: 'object', properties: { extra: { type: 'string' } }, required: ['extra'] }]
} as OpenAPI.SchemaObject;
const { zodString, imports } = convertSchema(schema);
expect(zodString).toBe('z.intersection(Base, z.object({\n extra: z.string()\n}))');
expect(imports.has('Base')).toBe(true);
});
it('handles oneOf with refs', () => {
const schema = {
oneOf: [{ $ref: '#/components/schemas/A' }, { $ref: '#/components/schemas/B' }]
} as OpenAPI.SchemaObject;
const { zodString, imports } = convertSchema(schema);
expect(zodString).toBe('z.union([A, B])');
expect(imports.has('A')).toBe(true);
expect(imports.has('B')).toBe(true);
});
it('handles oneOf with a single ref', () => {
const schema = {
oneOf: [{ $ref: '#/components/schemas/A' }]
} as OpenAPI.SchemaObject;
const { zodString, imports } = convertSchema(schema);
expect(zodString).toBe('A');
expect(imports.has('A')).toBe(true);
});
it('converts inline object properties', () => {
const schema = {
type: 'object',
properties: {
data: {
type: 'object',
properties: {
id: { type: 'string' },
flag: { type: 'boolean' }
},
required: ['id']
}
},
required: ['data']
} as OpenAPI.SchemaObject;
const { zodString } = convertSchema(schema);
expect(zodString).toBe('z.object({\n data: z.object({\n id: z.string(),\n flag: z.boolean().optional()\n})\n})');
});
});