Skip to content

Commit 3cd61f0

Browse files
committed
fix errors
1 parent b1a7d7b commit 3cd61f0

File tree

2 files changed

+37
-6
lines changed

2 files changed

+37
-6
lines changed

src/codegen/errors.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,8 @@ export function createGeneratorError(
184184
export function parseZodErrors(zodError: any): string[] {
185185
const errors: string[] = [];
186186

187-
if (zodError.errors) {
188-
for (const error of zodError.errors) {
187+
if (zodError.issues) {
188+
for (const error of zodError.issues) {
189189
const path = error.path.join('.');
190190
const message = error.message;
191191

test/codegen/errors.spec.ts

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
parseZodErrors,
1313
enhanceError
1414
} from '../../src/codegen/errors';
15+
import {z} from 'zod';
1516

1617
describe('Error Handling', () => {
1718
describe('CodegenError', () => {
@@ -174,9 +175,39 @@ describe('Error Handling', () => {
174175
});
175176

176177
describe('parseZodErrors', () => {
178+
it('should parse actual Zod validation errors', () => {
179+
// Create a real Zod schema and get actual validation error
180+
const schema = z.object({
181+
inputType: z.enum(['asyncapi', 'openapi', 'jsonschema']),
182+
inputPath: z.string(),
183+
outputPath: z.string()
184+
});
185+
186+
const result = schema.safeParse({
187+
inputType: 'invalid',
188+
inputPath: 123,
189+
outputPath: ''
190+
});
191+
192+
// Verify that validation failed
193+
expect(result.success).toBe(false);
194+
195+
// Type guard to ensure we have the error
196+
if (result.success) {
197+
throw new Error('Expected validation to fail');
198+
}
199+
200+
const errors = parseZodErrors(result.error);
201+
202+
// Should parse all validation errors
203+
expect(errors.length).toBeGreaterThan(0);
204+
expect(errors.some(e => e.includes('inputType'))).toBe(true);
205+
expect(errors.some(e => e.includes('inputPath'))).toBe(true);
206+
});
207+
177208
it('should parse invalid union discriminator errors', () => {
178209
const zodError = {
179-
errors: [
210+
issues: [
180211
{
181212
code: 'invalid_union_discriminator',
182213
path: ['inputType'],
@@ -195,7 +226,7 @@ describe('Error Handling', () => {
195226

196227
it('should parse invalid type errors', () => {
197228
const zodError = {
198-
errors: [
229+
issues: [
199230
{
200231
code: 'invalid_type',
201232
path: ['inputPath'],
@@ -215,7 +246,7 @@ describe('Error Handling', () => {
215246

216247
it('should parse invalid literal errors', () => {
217248
const zodError = {
218-
errors: [
249+
issues: [
219250
{
220251
code: 'invalid_literal',
221252
path: ['preset'],
@@ -233,7 +264,7 @@ describe('Error Handling', () => {
233264

234265
it('should parse generic errors', () => {
235266
const zodError = {
236-
errors: [
267+
issues: [
237268
{
238269
code: 'custom',
239270
path: ['custom', 'field'],

0 commit comments

Comments
 (0)