@geekmidas/schema 0.0.3 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +7 -0
- package/dist/{conversion-Civd6bIU.mjs → conversion-BJGLAqtd.mjs} +2 -3
- package/dist/conversion-BJGLAqtd.mjs.map +1 -0
- package/dist/{conversion-CrSNoSRa.cjs → conversion-CFlFrSru.cjs} +2 -3
- package/dist/conversion-CFlFrSru.cjs.map +1 -0
- package/dist/conversion-CH_EmflL.d.cts.map +1 -0
- package/dist/conversion-DUyZYTWO.d.mts.map +1 -0
- package/dist/conversion.cjs +1 -1
- package/dist/conversion.mjs +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +2 -2
- package/dist/index.d.mts +2 -2
- package/dist/index.mjs +1 -1
- package/dist/{openapi-DR4_PG-e.d.cts → openapi-Aeb_1e9T.d.cts} +3 -1
- package/dist/openapi-Aeb_1e9T.d.cts.map +1 -0
- package/dist/openapi-DH5yCqKh.mjs.map +1 -1
- package/dist/openapi-DVvLYx-8.cjs.map +1 -1
- package/dist/{openapi-j01kFjpI.d.mts → openapi-Dxc4FuMP.d.mts} +3 -1
- package/dist/openapi-Dxc4FuMP.d.mts.map +1 -0
- package/dist/openapi.d.cts +1 -1
- package/dist/openapi.d.mts +1 -1
- package/dist/parser.cjs.map +1 -1
- package/dist/parser.d.cts +3 -1
- package/dist/parser.d.cts.map +1 -0
- package/dist/parser.d.mts +3 -1
- package/dist/parser.d.mts.map +1 -0
- package/dist/parser.mjs.map +1 -1
- package/dist/{types-ByLHeRGs.d.mts → types-D7ZRea_7.d.cts} +3 -1
- package/dist/types-D7ZRea_7.d.cts.map +1 -0
- package/dist/{types-DfcgE7cO.d.cts → types-o4dOkvCd.d.mts} +3 -1
- package/dist/types-o4dOkvCd.d.mts.map +1 -0
- package/dist/types.d.cts +1 -1
- package/dist/types.d.mts +1 -1
- package/package.json +1 -1
- package/src/__benchmarks__/conversion.bench.ts +88 -0
- package/src/__tests__/conversion.spec.ts +440 -257
- package/src/__tests__/openapi.spec.ts +390 -390
- package/src/__tests__/parser.spec.ts +202 -202
- package/src/conversion.ts +154 -158
- package/src/index.ts +8 -9
- package/src/openapi.ts +49 -49
- package/src/parser.ts +11 -11
- package/src/types.ts +16 -16
- package/tsconfig.json +9 -0
- package/tsdown.config.ts +1 -1
- package/dist/conversion-Civd6bIU.mjs.map +0 -1
- package/dist/conversion-CrSNoSRa.cjs.map +0 -1
|
@@ -3,234 +3,234 @@ import { z } from 'zod/v4';
|
|
|
3
3
|
import { parseSchema, validate } from '../parser';
|
|
4
4
|
|
|
5
5
|
describe('Schema Parser', () => {
|
|
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
|
-
|
|
6
|
+
describe('validate', () => {
|
|
7
|
+
it('should validate data against a Zod schema', async () => {
|
|
8
|
+
const schema = z.object({
|
|
9
|
+
name: z.string(),
|
|
10
|
+
age: z.number(),
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
const result = await validate(schema, { name: 'John', age: 30 });
|
|
14
|
+
|
|
15
|
+
expect(result.issues).toBeUndefined();
|
|
16
|
+
if ('value' in result) {
|
|
17
|
+
expect(result.value).toEqual({ name: 'John', age: 30 });
|
|
18
|
+
}
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it('should return issues for invalid data', async () => {
|
|
22
|
+
const schema = z.object({
|
|
23
|
+
name: z.string(),
|
|
24
|
+
age: z.number(),
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
const result = await validate(schema, { name: 'John', age: 'invalid' });
|
|
28
|
+
|
|
29
|
+
expect(result.issues).toBeDefined();
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it('should validate string schema', async () => {
|
|
33
|
+
const schema = z.string();
|
|
34
|
+
|
|
35
|
+
const result = await validate(schema, 'test string');
|
|
36
|
+
|
|
37
|
+
expect(result.issues).toBeUndefined();
|
|
38
|
+
if ('value' in result) {
|
|
39
|
+
expect(result.value).toBe('test string');
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it('should validate array schema', async () => {
|
|
44
|
+
const schema = z.array(z.number());
|
|
45
|
+
|
|
46
|
+
const result = await validate(schema, [1, 2, 3]);
|
|
47
47
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
48
|
+
expect(result.issues).toBeUndefined();
|
|
49
|
+
if ('value' in result) {
|
|
50
|
+
expect(result.value).toEqual([1, 2, 3]);
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it('should return issues for invalid array items', async () => {
|
|
55
|
+
const schema = z.array(z.number());
|
|
56
56
|
|
|
57
|
-
|
|
57
|
+
const result = await validate(schema, [1, 'invalid', 3]);
|
|
58
58
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
59
|
+
expect(result.issues).toBeDefined();
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it('should validate nested objects', async () => {
|
|
63
|
+
const schema = z.object({
|
|
64
|
+
user: z.object({
|
|
65
|
+
name: z.string(),
|
|
66
|
+
profile: z.object({
|
|
67
|
+
bio: z.string(),
|
|
68
|
+
}),
|
|
69
|
+
}),
|
|
70
|
+
});
|
|
71
71
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
72
|
+
const result = await validate(schema, {
|
|
73
|
+
user: {
|
|
74
|
+
name: 'John',
|
|
75
|
+
profile: { bio: 'Developer' },
|
|
76
|
+
},
|
|
77
|
+
});
|
|
78
78
|
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
79
|
+
expect(result.issues).toBeUndefined();
|
|
80
|
+
if ('value' in result) {
|
|
81
|
+
expect(result.value).toEqual({
|
|
82
|
+
user: {
|
|
83
|
+
name: 'John',
|
|
84
|
+
profile: { bio: 'Developer' },
|
|
85
|
+
},
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
});
|
|
89
|
+
});
|
|
90
90
|
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
91
|
+
describe('parseSchema', () => {
|
|
92
|
+
it('should parse valid data', async () => {
|
|
93
|
+
const schema = z.object({
|
|
94
|
+
name: z.string(),
|
|
95
|
+
age: z.number(),
|
|
96
|
+
});
|
|
97
97
|
|
|
98
|
-
|
|
98
|
+
const result = await parseSchema(schema, { name: 'John', age: 30 });
|
|
99
99
|
|
|
100
|
-
|
|
101
|
-
|
|
100
|
+
expect(result).toEqual({ name: 'John', age: 30 });
|
|
101
|
+
});
|
|
102
102
|
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
103
|
+
it('should throw for invalid data', async () => {
|
|
104
|
+
const schema = z.object({
|
|
105
|
+
name: z.string(),
|
|
106
|
+
age: z.number(),
|
|
107
|
+
});
|
|
108
108
|
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
109
|
+
await expect(
|
|
110
|
+
parseSchema(schema, { name: 'John', age: 'invalid' }),
|
|
111
|
+
).rejects.toBeDefined();
|
|
112
|
+
});
|
|
113
113
|
|
|
114
|
-
|
|
115
|
-
|
|
114
|
+
it('should return undefined for undefined schema', async () => {
|
|
115
|
+
const result = await parseSchema(undefined as any, { name: 'John' });
|
|
116
116
|
|
|
117
|
-
|
|
118
|
-
|
|
117
|
+
expect(result).toBeUndefined();
|
|
118
|
+
});
|
|
119
119
|
|
|
120
|
-
|
|
121
|
-
|
|
120
|
+
it('should parse string schema', async () => {
|
|
121
|
+
const schema = z.string().min(3);
|
|
122
122
|
|
|
123
|
-
|
|
123
|
+
const result = await parseSchema(schema, 'test');
|
|
124
124
|
|
|
125
|
-
|
|
126
|
-
|
|
125
|
+
expect(result).toBe('test');
|
|
126
|
+
});
|
|
127
127
|
|
|
128
|
-
|
|
129
|
-
|
|
128
|
+
it('should throw for string that is too short', async () => {
|
|
129
|
+
const schema = z.string().min(5);
|
|
130
130
|
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
131
|
+
await expect(parseSchema(schema, 'abc')).rejects.toBeDefined();
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
it('should parse number schema', async () => {
|
|
135
|
+
const schema = z.number().positive();
|
|
136
|
+
|
|
137
|
+
const result = await parseSchema(schema, 42);
|
|
138
|
+
|
|
139
|
+
expect(result).toBe(42);
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
it('should throw for negative number when positive required', async () => {
|
|
143
|
+
const schema = z.number().positive();
|
|
144
|
+
|
|
145
|
+
await expect(parseSchema(schema, -5)).rejects.toBeDefined();
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
it('should parse optional fields', async () => {
|
|
149
|
+
const schema = z.object({
|
|
150
|
+
required: z.string(),
|
|
151
|
+
optional: z.string().optional(),
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
const result = await parseSchema(schema, { required: 'value' });
|
|
155
|
+
|
|
156
|
+
expect(result).toEqual({ required: 'value' });
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
it('should parse with defaults', async () => {
|
|
160
|
+
const schema = z.object({
|
|
161
|
+
name: z.string(),
|
|
162
|
+
role: z.string().default('user'),
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
const result = await parseSchema(schema, { name: 'John' });
|
|
166
|
+
|
|
167
|
+
expect(result).toEqual({ name: 'John', role: 'user' });
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
it('should parse enum values', async () => {
|
|
171
|
+
const schema = z.object({
|
|
172
|
+
status: z.enum(['active', 'inactive', 'pending']),
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
const result = await parseSchema(schema, { status: 'active' });
|
|
176
|
+
|
|
177
|
+
expect(result).toEqual({ status: 'active' });
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
it('should throw for invalid enum value', async () => {
|
|
181
|
+
const schema = z.object({
|
|
182
|
+
status: z.enum(['active', 'inactive']),
|
|
183
|
+
});
|
|
184
184
|
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
185
|
+
await expect(
|
|
186
|
+
parseSchema(schema, { status: 'invalid' }),
|
|
187
|
+
).rejects.toBeDefined();
|
|
188
|
+
});
|
|
189
189
|
|
|
190
|
-
|
|
191
|
-
|
|
190
|
+
it('should parse union types', async () => {
|
|
191
|
+
const schema = z.union([z.string(), z.number()]);
|
|
192
192
|
|
|
193
|
-
|
|
194
|
-
|
|
193
|
+
const result1 = await parseSchema(schema, 'text');
|
|
194
|
+
const result2 = await parseSchema(schema, 42);
|
|
195
195
|
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
196
|
+
expect(result1).toBe('text');
|
|
197
|
+
expect(result2).toBe(42);
|
|
198
|
+
});
|
|
199
199
|
|
|
200
|
-
|
|
201
|
-
|
|
200
|
+
it('should throw for invalid union value', async () => {
|
|
201
|
+
const schema = z.union([z.string(), z.number()]);
|
|
202
202
|
|
|
203
|
-
|
|
204
|
-
|
|
203
|
+
await expect(parseSchema(schema, true)).rejects.toBeDefined();
|
|
204
|
+
});
|
|
205
205
|
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
206
|
+
it('should parse array of objects', async () => {
|
|
207
|
+
const schema = z.array(
|
|
208
|
+
z.object({
|
|
209
|
+
id: z.number(),
|
|
210
|
+
name: z.string(),
|
|
211
|
+
}),
|
|
212
|
+
);
|
|
213
213
|
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
214
|
+
const result = await parseSchema(schema, [
|
|
215
|
+
{ id: 1, name: 'Alice' },
|
|
216
|
+
{ id: 2, name: 'Bob' },
|
|
217
|
+
]);
|
|
218
218
|
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
219
|
+
expect(result).toEqual([
|
|
220
|
+
{ id: 1, name: 'Alice' },
|
|
221
|
+
{ id: 2, name: 'Bob' },
|
|
222
|
+
]);
|
|
223
|
+
});
|
|
224
224
|
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
225
|
+
it('should validate required fields', async () => {
|
|
226
|
+
const schema = z.object({
|
|
227
|
+
required: z.string(),
|
|
228
|
+
optional: z.string().optional(),
|
|
229
|
+
});
|
|
230
230
|
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
231
|
+
await expect(
|
|
232
|
+
parseSchema(schema, { optional: 'value' }),
|
|
233
|
+
).rejects.toBeDefined();
|
|
234
|
+
});
|
|
235
|
+
});
|
|
236
236
|
});
|