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

Zod: Recursive schema with option typed #780

Merged
merged 5 commits into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/chatty-owls-grow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@kubb/swagger-tanstack-query": minor
---

useQuery with Zod validation
5 changes: 5 additions & 0 deletions .changeset/eight-plums-try.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@kubb/swagger-zod": patch
---

Add types with `@kubb/swagger.ts` for zodSchemas
5 changes: 5 additions & 0 deletions .changeset/yellow-roses-push.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@kubb/swagger-zod": patch
---

dateType for zod
8 changes: 4 additions & 4 deletions docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@
"serve": "vitepress serve"
},
"dependencies": {
"@vercel/analytics": "^1.1.1",
"@vercel/analytics": "^1.1.2",
"sitemap": "^7.1.1",
"vitepress": "^1.0.0-rc.36",
"vue": "^3.4.13"
"vitepress": "^1.0.0-rc.39",
"vue": "^3.4.15"
},
"devDependencies": {
"@types/node": "^20.11.0"
"@types/node": "^20.11.5"
},
"packageManager": "[email protected]",
"engines": {
Expand Down
106 changes: 106 additions & 0 deletions docs/plugins/swagger-zod/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,112 @@ export default defineConfig({
})
```

### typed

Use TypeScript(`@kubb/swagger-ts`) to add type annotation.

::: info

Type: `boolean` <br/>

::: code-group

```typescript [kubb.config.js]
import { defineConfig } from '@kubb/core'
import createSwagger from '@kubb/swagger'
import createSwaggerZod from '@kubb/swagger-zod'
import createSwaggerTs from '@kubb/swagger-ts'

export default defineConfig({
input: {
path: './petStore.yaml',
},
output: {
path: './src/gen',
},
plugins: [
createSwagger({ output: false }),
createSwaggerZod(
{
typed: true,
},
),
createSwaggerTs(
{},
),
],
})
```

:::

### dateType

Choose to use `date` or `datetime` as JavaScript `Date` instead of `string`.

::: info type

::: code-group

```typescript ['string']
date: string
```

```typescript ['date']
date: Date
```

:::

::: info

Type: `'string' | 'date'` <br/>
Default: `'string'`

```typescript ['string']
import { defineConfig } from '@kubb/core'
import createSwagger from '@kubb/swagger'
import createSwaggerZod from '@kubb/swagger-zod'

export default defineConfig({
input: {
path: './petStore.yaml',
},
output: {
path: './src/gen',
},
plugins: [
createSwagger({ output: false }),
createSwaggerZod({
dateType: 'string',
}),
],
})
```

```typescript ['date']
import { defineConfig } from '@kubb/core'
import createSwagger from '@kubb/swagger'
import createSwaggerZod from '@kubb/swagger-zod'

export default defineConfig({
input: {
path: './petStore.yaml',
},
output: {
path: './src/gen',
},
plugins: [
createSwagger({ output: false }),
createSwaggerZod({
dateType: 'date',
}),
],
})
```

:::

:::

### transformers
Expand Down
10 changes: 5 additions & 5 deletions e2e/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@
"@kubb/swagger-ts": "workspace:*",
"@kubb/swagger-zod": "workspace:*",
"@kubb/swagger-zodios": "workspace:*",
"@tanstack/react-query": "^5.17.10",
"@tanstack/solid-query": "^5.17.10",
"@tanstack/svelte-query": "^5.17.10",
"@tanstack/vue-query": "^5.17.10",
"@tanstack/react-query": "^5.17.15",
"@tanstack/solid-query": "^5.17.15",
"@tanstack/svelte-query": "^5.17.15",
"@tanstack/vue-query": "^5.17.15",
"@zodios/core": "^10.9.6",
"axios": "^1.6.5",
"msw": "^1.3.2",
Expand All @@ -44,7 +44,7 @@
"svelte": "^3.59.2",
"swr": "^2.2.4",
"tsup": "^8.0.1",
"vue": "^3.4.13",
"vue": "^3.4.15",
"zod": "^3.22.4"
},
"devDependencies": {
Expand Down
2 changes: 2 additions & 0 deletions examples/advanced/configs/kubb.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ export default defineConfig(async () => {
},
],
group: { type: 'tag' },
dateType: 'date',
typed: true,
},
],
[
Expand Down
2 changes: 1 addition & 1 deletion examples/advanced/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"solid-js": "^1.8.11",
"svelte": "^3.59.2",
"swr": "^2.2.4",
"vue": "^3.4.13",
"vue": "^3.4.15",
"zod": "^3.22.4"
},
"devDependencies": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export type DeletePetHeaderParams = {
* @type string | undefined
*/
api_key?: string
}
} | undefined

export type DeletePetMutationResponse = any | null

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export type FindPetsByStatusQueryParams = {
* @default 'available'
*/
status?: FindPetsByStatusQueryParamsStatus
}
} | undefined

/**
* @description successful operation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export type FindPetsByTagsQueryParams = {
* @type string | undefined
*/
pageSize?: string
}
} | undefined

/**
* @description successful operation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export type UpdatePetWithFormQueryParams = {
* @type string | undefined
*/
status?: string
}
} | undefined
export namespace UpdatePetWithFormMutation {
export type Response = UpdatePetWithFormMutationResponse
export type PathParams = UpdatePetWithFormPathParams
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export type UploadFileQueryParams = {
* @type string | undefined
*/
additionalMetadata?: string
}
} | undefined

/**
* @description successful operation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export type CreatePetsQueryParams = {
* @type integer | undefined
*/
offset?: number
}
} | undefined

/**
* @description unexpected error
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export type LoginUserQueryParams = {
* @type string | undefined
*/
password?: string
}
} | undefined

/**
* @description successful operation
Expand Down
3 changes: 2 additions & 1 deletion examples/advanced/src/gen/zod/addPetRequestSchema.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { categorySchema } from './categorySchema'
import { tagSchema } from './tagSchema'
import { z } from 'zod'
import type { AddPetRequest } from '../models/ts/AddPetRequest'

export const addPetRequestSchema = z.object({
export const addPetRequestSchema: z.ZodType<AddPetRequest> = z.object({
'id': z.number().optional(),
'name': z.string(),
'category': z.lazy(() => categorySchema).optional(),
Expand Down
3 changes: 2 additions & 1 deletion examples/advanced/src/gen/zod/addressSchema.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { z } from 'zod'
import type { Address } from '../models/ts/Address'

export const addressSchema = z.object({
export const addressSchema: z.ZodType<Address> = z.object({
'street': z.string().optional(),
'city': z.string().optional(),
'state': z.string().optional(),
Expand Down
7 changes: 6 additions & 1 deletion examples/advanced/src/gen/zod/apiResponseSchema.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
import { z } from 'zod'
import type { ApiResponse } from '../models/ts/ApiResponse'

export const apiResponseSchema = z.object({ 'code': z.number().optional(), 'type': z.string().optional(), 'message': z.string().optional() })
export const apiResponseSchema: z.ZodType<ApiResponse> = z.object({
'code': z.number().optional(),
'type': z.string().optional(),
'message': z.string().optional(),
})
3 changes: 2 additions & 1 deletion examples/advanced/src/gen/zod/categorySchema.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { z } from 'zod'
import type { Category } from '../models/ts/Category'

export const categorySchema = z.object({ 'id': z.number().optional(), 'name': z.string().optional() })
export const categorySchema: z.ZodType<Category> = z.object({ 'id': z.number().optional(), 'name': z.string().optional() })
3 changes: 2 additions & 1 deletion examples/advanced/src/gen/zod/customerSchema.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { addressSchema } from './addressSchema'
import { z } from 'zod'
import type { Customer } from '../models/ts/Customer'

export const customerSchema = z.object({
export const customerSchema: z.ZodType<Customer> = z.object({
'id': z.number().optional(),
'username': z.string().optional(),
'address': z.array(z.lazy(() => addressSchema)).optional(),
Expand Down
7 changes: 4 additions & 3 deletions examples/advanced/src/gen/zod/orderSchema.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { z } from 'zod'
import type { Order } from '../models/ts/Order'

export const orderSchema = z.object({
export const orderSchema: z.ZodType<Order> = z.object({
'id': z.number().optional(),
'petId': z.number().optional(),
'quantity': z.number().optional(),
'shipDate': z.string().datetime().optional(),
'shipDate': z.date().optional(),
'status': z.enum([`placed`, `approved`, `delivered`]).describe(`Order Status`).optional(),
'http_status': z.enum([`ok`, `not_found`]).describe(`HTTP Status`).optional(),
'http_status': z.union([z.literal(200), z.literal(400)]).describe(`HTTP Status`).optional(),
'complete': z.boolean().optional(),
})
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ export const addPetMutationRequestSchema = z.lazy(() => addPetRequestSchema)
/**
* @description Successful operation
*/
export const addPetMutationResponseSchema = z.lazy(() => petSchema).schema.omit({ name: true })
export const addPetMutationResponseSchema = z.lazy(() => petSchema).schema.and(z.object({ name: z.never() }))
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ import { z } from 'zod'
* @description Invalid pet value
*/
export const deletePet400Schema = z.any()
export const deletePetHeaderParamsSchema = z.object({ 'api_key': z.string().optional() })
export const deletePetHeaderParamsSchema = z.object({ 'api_key': z.string().optional() }).optional()
export const deletePetMutationResponseSchema = z.any()
export const deletePetPathParamsSchema = z.object({ 'petId': z.number().describe(`Pet id to delete`) })
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { petSchema } from '../petSchema'
export const findPetsByStatus400Schema = z.any()
export const findPetsByStatusQueryParamsSchema = z.object({
'status': z.enum([`available`, `pending`, `sold`]).default('available').describe(`Status values that need to be considered for filter`).optional(),
})
}).optional()

/**
* @description successful operation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const findPetsByTagsQueryParamsSchema = z.object({
'tags': z.array(z.string()).describe(`Tags to filter by`).optional(),
'page': z.string().describe(`to request with required page number or pagination`).optional(),
'pageSize': z.string().describe(`to request with required page size`).optional(),
})
}).optional()

/**
* @description successful operation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ export const getPetByIdPathParamsSchema = z.object({ 'petId': z.number().describ
/**
* @description successful operation
*/
export const getPetByIdQueryResponseSchema = z.lazy(() => petSchema).schema.omit({ name: true })
export const getPetByIdQueryResponseSchema = z.lazy(() => petSchema).schema.and(z.object({ name: z.never() }))
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ export const updatePet405Schema = z.any()
/**
* @description Update an existent pet in the store
*/
export const updatePetMutationRequestSchema = z.lazy(() => petSchema).schema.omit({ id: true })
export const updatePetMutationRequestSchema = z.lazy(() => petSchema).schema.and(z.object({ id: z.never() }))

/**
* @description Successful operation
*/
export const updatePetMutationResponseSchema = z.lazy(() => petSchema).schema.omit({ name: true })
export const updatePetMutationResponseSchema = z.lazy(() => petSchema).schema.and(z.object({ name: z.never() }))
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ export const updatePetWithFormPathParamsSchema = z.object({ 'petId': z.number().
export const updatePetWithFormQueryParamsSchema = z.object({
'name': z.string().describe(`Name of pet that needs to be updated`).optional(),
'status': z.string().describe(`Status of pet that needs to be updated`).optional(),
})
}).optional()
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { apiResponseSchema } from '../apiResponseSchema'

export const uploadFileMutationRequestSchema = z.string()
export const uploadFilePathParamsSchema = z.object({ 'petId': z.number().describe(`ID of pet to update`) })
export const uploadFileQueryParamsSchema = z.object({ 'additionalMetadata': z.string().describe(`Additional Metadata`).optional() })
export const uploadFileQueryParamsSchema = z.object({ 'additionalMetadata': z.string().describe(`Additional Metadata`).optional() }).optional()

/**
* @description successful operation
Expand Down
3 changes: 2 additions & 1 deletion examples/advanced/src/gen/zod/petNotFoundSchema.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { z } from 'zod'
import type { PetNotFound } from '../models/ts/PetNotFound'

export const petNotFoundSchema = z.object({ 'code': z.number().optional(), 'message': z.string().optional() })
export const petNotFoundSchema: z.ZodType<PetNotFound> = z.object({ 'code': z.number().optional(), 'message': z.string().optional() })
3 changes: 2 additions & 1 deletion examples/advanced/src/gen/zod/petSchema.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { categorySchema } from './categorySchema'
import { tagSchema } from './tagSchema'
import { z } from 'zod'
import type { Pet } from '../models/ts/Pet'

export const petSchema = z.object({
export const petSchema: z.ZodType<Pet> = z.object({
'id': z.number().readonly().optional(),
'name': z.string(),
'category': z.lazy(() => categorySchema).optional(),
Expand Down
Loading
Loading