Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Contradicting Zod schema generated #1585

Closed
RichAyotte opened this issue Mar 3, 2025 · 1 comment
Closed

Contradicting Zod schema generated #1585

RichAyotte opened this issue Mar 3, 2025 · 1 comment
Labels
bug Something isn't working

Comments

@RichAyotte
Copy link

RichAyotte commented Mar 3, 2025

What version of kubb is running?

3.6.1

What kind of platform do you use?

Bun

How does your kubb.config.ts config look like

import { defineConfig } from '@kubb/core'
import { pluginMsw } from '@kubb/plugin-msw'
import { pluginOas } from '@kubb/plugin-oas'
import { pluginTs } from '@kubb/plugin-ts'
import { pluginVueQuery } from '@kubb/plugin-vue-query'
import { pluginZod } from '@kubb/plugin-zod'

export default defineConfig(() => {
	return {
		name: 'internal-api',
		input: {
			path: 'https://api.payman.dev/api/v3/api-docs',
		},
		output: {
			barrelType: 'named',
			clean: true,
			extension: {
				'.ts': '',
			},
			path: './src/generated',
		},
		plugins: [
			pluginOas({
				output: {
					path: 'schemas',
					barrelType: false,
				},
			}),
			pluginTs({
				enumType: 'literal',
				output: {
					path: 'types',
					barrelType: false,
				},
				unknownType: 'unknown',
			}),
			pluginVueQuery({
				client: {
					importPath: '@/utils/paymanClient',
				},
				infinite: {
					queryParam: 'page',
					cursorParam: 'nextPage',
				},
				output: {
					path: 'composables',
					barrelType: false,
				},
				paramsType: 'object',
				pathParamsType: 'object',
				parser: 'zod',
			}),
			pluginZod({
				output: {
					path: 'zod',
					barrelType: false,
				},
				typed: false,
				unknownType: 'unknown',
				inferred: true,
			}),
			pluginMsw({
				output: {
					path: 'mocks',
					barrelType: false,
				},
				handlers: true,
			}),
		],
	}
})

Swagger/OpenAPI file?

fetch https://api.payman.dev/api/v3/api-docs

What version of external packages are you using(@tanstack-query, MSW, React, Vue, ...)

"zod": "^3.24.2"

What steps can reproduce the bug?

The following schemas are generated.

import { z } from 'zod'
import { agentMetadataSchema } from './agentMetadataSchema'

export const agentSchema = z.object({
	id: z.string().optional(),
	createdAt: z.string().datetime().optional(),
	updatedAt: z.string().datetime().optional(),
	createdBy: z.string().optional(),
	updatedBy: z.string().optional(),
	handle: z.string().optional(),
	name: z.string(),
	description: z.string().optional(),
	organizationId: z.string(),
	metadata: z.lazy(() => agentMetadataSchema),
	apiKeyId: z.string().optional(),
	status: z.enum(['ACTIVE', 'DELETED']).optional(),
	stats: z.object({}).catchall(z.unknown()).optional(),
})

export type AgentSchema = z.infer<typeof agentSchema>

and

export const updateAgentMutationRequestSchema = z
	.lazy(() => agentSchema)
	.and(
		z.object({
			id: z.never(),
			createdAt: z.never(),
			updatedAt: z.never(),
			createdBy: z.never(),
			updatedBy: z.never(),
		}),
	)

Validating as follows:

import { updateAgentMutationRequestSchema } from 'src/generated/zod/updateAgentSchema'

updateAgentMutationRequestSchema.parse({
	metadata: {
		systemControlledAgent: false,
	},
	name: 'Default Agent 1',
	organizationId: 'org-1fee819f-9e05-6409-b5bf-3f8c1c327d0b',
})

Gives this error

errorHandler.ts:90 ZodError: [
  {
    "code": "invalid_type",
    "expected": "never",
    "received": "undefined",
    "path": [
      "id"
    ],
    "message": "Required"
  },
  {
    "code": "invalid_type",
    "expected": "never",
    "received": "undefined",
    "path": [
      "createdAt"
    ],
    "message": "Required"
  },
  {
    "code": "invalid_type",
    "expected": "never",
    "received": "undefined",
    "path": [
      "updatedAt"
    ],
    "message": "Required"
  },
  {
    "code": "invalid_type",
    "expected": "never",
    "received": "undefined",
    "path": [
      "createdBy"
    ],
    "message": "Required"
  },
  {
    "code": "invalid_type",
    "expected": "never",
    "received": "undefined",
    "path": [
      "updatedBy"
    ],
    "message": "Required"
  }
]

The error indicates that fields like id, createdAt, updatedAt, createdBy, and updatedBy are "required" and expected to be of type never, but they are undefined in the input object. This seems contradictory—how can a field be required yet not allowed to exist?

Maybe something like the following would work.

export const updateAgentMutationRequestSchema = agentSchema
	.omit({
		id: true,
		createdAt: true,
		updatedAt: true,
		createdBy: true,
		updatedBy: true,
	})
	.strict()

How often does this bug happen?

Every time

What is the expected behavior?

No validation error.

Additional information

No response

@RichAyotte RichAyotte added the bug Something isn't working label Mar 3, 2025
@stijnvanhulle
Copy link
Collaborator

Fixed in Kubb v3.6.5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants