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 satisfier util #1462

Merged
merged 2 commits into from
Dec 14, 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/big-buttons-check.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@kubb/plugin-zod": patch
---

Use of `toZod` util to create schema based on a type
6 changes: 5 additions & 1 deletion docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@ title: Changelog

# Changelog

## 3.3.1
- [`plugin-zod`](/plugins/plugin-zod): Use of `tozod` util to create schema based on a type


## 3.3.0
- [`plugin-client`](/plugins/plugin-client): `client` to use `fetch` or `axios` as HTTP client
- [`plugin-zod`](/plugins/plugin-zod): use Regular expression literal instead of RegExp-contructor
- [`plugin-zod`](/plugins/plugin-zod): Use Regular expression literal instead of RegExp-contructor
- [`plugin-ts`](/plugins/plugin-ts): Switch between the use of type or interface when creating types

## 3.2.0
Expand Down
4 changes: 4 additions & 0 deletions docs/plugins/plugin-zod/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ Return the name of a group based on the group name, this will be used for the fi

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

> [!IMPORTANT]
> We rely on [`tozod`](https://github.com/colinhacks/tozod) from the creator of Zod to create a schema based on a type.
> Kubb contains its own version to those kind of conversions.

| | |
|----------:|:----------|
| Type: | `boolean` |
Expand Down
1 change: 1 addition & 0 deletions e2e/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"@kubb/plugin-msw": ["../packages/plugin-msw/src/index.ts"],
"@kubb/plugin-react-query": ["../packages/plugin-react-query/src/index.ts"],
"@kubb/plugin-ts": ["../packages/plugin-ts/src/index.ts"],
"@kubb/plugin-zod/utils": ["../packages/plugin-zod/src/utils/index.ts"],
"@kubb/plugin-zod": ["../packages/plugin-zod/src/index.ts"],
"@kubb/parser-ts": ["../packages/parser-ts/src/index.ts"]
}
Expand Down
3 changes: 2 additions & 1 deletion examples/advanced/configs/kubb.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export default defineConfig(() => {
},
enumType: 'asConst',
enumSuffix: 'enum',
dateType: 'date',
dateType: 'string',
override: [
{
type: 'operationId',
Expand Down Expand Up @@ -148,6 +148,7 @@ export default defineConfig(() => {
group: { type: 'tag' },
dateType: 'stringOffset',
inferred: true,
typed: true,
operations: false,
}),
pluginFaker({
Expand Down
2 changes: 1 addition & 1 deletion examples/advanced/src/gen/models/ts/Order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export type Order = {
/**
* @type string | undefined, date-time
*/
shipDate?: Date
shipDate?: string
/**
* @description Order Status
* @type string | undefined
Expand Down
6 changes: 4 additions & 2 deletions examples/advanced/src/gen/zod/addPetRequestSchema.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { AddPetRequest } from '../models/ts/AddPetRequest.ts'
import type { ToZod } from '@kubb/plugin-zod/utils'
import { categorySchema } from './categorySchema.ts'
import { tagTagSchema } from './tag/tagSchema.ts'
import { z } from 'zod'
Expand All @@ -9,6 +11,6 @@ export const addPetRequestSchema = z.object({
photoUrls: z.array(z.string()),
tags: z.array(z.lazy(() => tagTagSchema)).optional(),
status: z.enum(['available', 'pending', 'sold']).describe('pet status in the store').optional(),
})
} satisfies ToZod<AddPetRequest>)

export type AddPetRequestSchema = z.infer<typeof addPetRequestSchema>
export type AddPetRequestSchema = AddPetRequest
6 changes: 4 additions & 2 deletions examples/advanced/src/gen/zod/addressSchema.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import type { Address } from '../models/ts/Address.ts'
import type { ToZod } from '@kubb/plugin-zod/utils'
import { z } from 'zod'

export const addressSchema = z.object({
street: z.string().optional(),
city: z.string().optional(),
state: z.string().optional(),
zip: z.string().optional(),
})
} satisfies ToZod<Address>)

export type AddressSchema = z.infer<typeof addressSchema>
export type AddressSchema = Address
6 changes: 4 additions & 2 deletions examples/advanced/src/gen/zod/apiResponseSchema.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import type { ApiResponse } from '../models/ts/ApiResponse.ts'
import type { ToZod } from '@kubb/plugin-zod/utils'
import { z } from 'zod'

export const apiResponseSchema = z.object({
code: z.number().int().optional(),
type: z.string().optional(),
message: z.string().optional(),
})
} satisfies ToZod<ApiResponse>)

export type ApiResponseSchema = z.infer<typeof apiResponseSchema>
export type ApiResponseSchema = ApiResponse
6 changes: 4 additions & 2 deletions examples/advanced/src/gen/zod/categorySchema.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import type { Category } from '../models/ts/Category.ts'
import type { ToZod } from '@kubb/plugin-zod/utils'
import { z } from 'zod'

export const categorySchema = z.object({
id: z.number().int().optional(),
name: z.string().optional(),
})
} satisfies ToZod<Category>)

export type CategorySchema = z.infer<typeof categorySchema>
export type CategorySchema = Category
6 changes: 4 additions & 2 deletions examples/advanced/src/gen/zod/customerSchema.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import type { Customer } from '../models/ts/Customer.ts'
import type { ToZod } from '@kubb/plugin-zod/utils'
import { addressSchema } from './addressSchema.ts'
import { z } from 'zod'

export const customerSchema = z.object({
id: z.number().int().optional(),
username: z.string().optional(),
address: z.array(z.lazy(() => addressSchema)).optional(),
})
} satisfies ToZod<Customer>)

export type CustomerSchema = z.infer<typeof customerSchema>
export type CustomerSchema = Customer
6 changes: 4 additions & 2 deletions examples/advanced/src/gen/zod/orderSchema.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { Order } from '../models/ts/Order.ts'
import type { ToZod } from '@kubb/plugin-zod/utils'
import { z } from 'zod'

export const orderSchema = z.object({
Expand All @@ -13,6 +15,6 @@ export const orderSchema = z.object({
.describe('HTTP Status')
.optional(),
complete: z.boolean().optional(),
})
} satisfies ToZod<Order>)

export type OrderSchema = z.infer<typeof orderSchema>
export type OrderSchema = Order
12 changes: 7 additions & 5 deletions examples/advanced/src/gen/zod/petController/addPetSchema.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { AddPet200, AddPet405, AddPetMutationRequest, AddPetMutationResponse } from '../../models/ts/petController/AddPet.ts'
import type { ToZod } from '@kubb/plugin-zod/utils'
import { addPetRequestSchema } from '../addPetRequestSchema.ts'
import { petSchema } from '../petSchema.ts'
import { z } from 'zod'
Expand All @@ -7,25 +9,25 @@ import { z } from 'zod'
*/
export const addPet200Schema = z.lazy(() => petSchema).and(z.object({ name: z.never() }))

export type AddPet200Schema = z.infer<typeof addPet200Schema>
export type AddPet200Schema = AddPet200

/**
* @description Pet not found
*/
export const addPet405Schema = z.object({
code: z.number().int().optional(),
message: z.string().optional(),
})
} satisfies ToZod<AddPet405>)

export type AddPet405Schema = z.infer<typeof addPet405Schema>
export type AddPet405Schema = AddPet405

/**
* @description Create a new pet in the store
*/
export const addPetMutationRequestSchema = z.lazy(() => addPetRequestSchema)

export type AddPetMutationRequestSchema = z.infer<typeof addPetMutationRequestSchema>
export type AddPetMutationRequestSchema = AddPetMutationRequest

export const addPetMutationResponseSchema = z.lazy(() => addPet200Schema)

export type AddPetMutationResponseSchema = z.infer<typeof addPetMutationResponseSchema>
export type AddPetMutationResponseSchema = AddPetMutationResponse
14 changes: 8 additions & 6 deletions examples/advanced/src/gen/zod/petController/deletePetSchema.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
import type { DeletePetPathParams, DeletePetHeaderParams, DeletePet400, DeletePetMutationResponse } from '../../models/ts/petController/DeletePet.ts'
import type { ToZod } from '@kubb/plugin-zod/utils'
import { z } from 'zod'

export const deletePetPathParamsSchema = z.object({
petId: z.number().int().describe('Pet id to delete'),
})
} satisfies ToZod<DeletePetPathParams>)

export type DeletePetPathParamsSchema = z.infer<typeof deletePetPathParamsSchema>
export type DeletePetPathParamsSchema = DeletePetPathParams

export const deletePetHeaderParamsSchema = z
.object({
api_key: z.string().optional(),
})
} satisfies ToZod<DeletePetHeaderParams>)
.optional()

export type DeletePetHeaderParamsSchema = z.infer<typeof deletePetHeaderParamsSchema>
export type DeletePetHeaderParamsSchema = DeletePetHeaderParams

/**
* @description Invalid pet value
*/
export const deletePet400Schema = z.any()

export type DeletePet400Schema = z.infer<typeof deletePet400Schema>
export type DeletePet400Schema = DeletePet400

export const deletePetMutationResponseSchema = z.any()

export type DeletePetMutationResponseSchema = z.infer<typeof deletePetMutationResponseSchema>
export type DeletePetMutationResponseSchema = DeletePetMutationResponse
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import type {
FindPetsByStatusPathParams,
FindPetsByStatus200,
FindPetsByStatus400,
FindPetsByStatusQueryResponse,
} from '../../models/ts/petController/FindPetsByStatus.ts'
import type { ToZod } from '@kubb/plugin-zod/utils'
import { petSchema } from '../petSchema.ts'
import { z } from 'zod'

export const findPetsByStatusPathParamsSchema = z.object({
step_id: z.string(),
})
} satisfies ToZod<FindPetsByStatusPathParams>)

export type FindPetsByStatusPathParamsSchema = z.infer<typeof findPetsByStatusPathParamsSchema>
export type FindPetsByStatusPathParamsSchema = FindPetsByStatusPathParams

/**
* @description successful operation
Expand All @@ -15,15 +22,15 @@ export const findPetsByStatus200Schema = z
.min(1)
.max(3)

export type FindPetsByStatus200Schema = z.infer<typeof findPetsByStatus200Schema>
export type FindPetsByStatus200Schema = FindPetsByStatus200

/**
* @description Invalid status value
*/
export const findPetsByStatus400Schema = z.any()

export type FindPetsByStatus400Schema = z.infer<typeof findPetsByStatus400Schema>
export type FindPetsByStatus400Schema = FindPetsByStatus400

export const findPetsByStatusQueryResponseSchema = z.lazy(() => findPetsByStatus200Schema)

export type FindPetsByStatusQueryResponseSchema = z.infer<typeof findPetsByStatusQueryResponseSchema>
export type FindPetsByStatusQueryResponseSchema = FindPetsByStatusQueryResponse
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
import type {
FindPetsByTagsQueryParams,
FindPetsByTagsHeaderParams,
FindPetsByTags200,
FindPetsByTags400,
FindPetsByTagsQueryResponse,
} from '../../models/ts/petController/FindPetsByTags.ts'
import type { ToZod } from '@kubb/plugin-zod/utils'
import { petSchema } from '../petSchema.ts'
import { z } from 'zod'

Expand All @@ -6,31 +14,31 @@ export const findPetsByTagsQueryParamsSchema = z
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(),
})
} satisfies ToZod<FindPetsByTagsQueryParams>)
.optional()

export type FindPetsByTagsQueryParamsSchema = z.infer<typeof findPetsByTagsQueryParamsSchema>
export type FindPetsByTagsQueryParamsSchema = FindPetsByTagsQueryParams

export const findPetsByTagsHeaderParamsSchema = z.object({
'X-EXAMPLE': z.enum(['ONE', 'TWO', 'THREE']).describe('Header parameters'),
})
} satisfies ToZod<FindPetsByTagsHeaderParams>)

export type FindPetsByTagsHeaderParamsSchema = z.infer<typeof findPetsByTagsHeaderParamsSchema>
export type FindPetsByTagsHeaderParamsSchema = FindPetsByTagsHeaderParams

/**
* @description successful operation
*/
export const findPetsByTags200Schema = z.array(z.lazy(() => petSchema))

export type FindPetsByTags200Schema = z.infer<typeof findPetsByTags200Schema>
export type FindPetsByTags200Schema = FindPetsByTags200

/**
* @description Invalid tag value
*/
export const findPetsByTags400Schema = z.any()

export type FindPetsByTags400Schema = z.infer<typeof findPetsByTags400Schema>
export type FindPetsByTags400Schema = FindPetsByTags400

export const findPetsByTagsQueryResponseSchema = z.lazy(() => findPetsByTags200Schema)

export type FindPetsByTagsQueryResponseSchema = z.infer<typeof findPetsByTagsQueryResponseSchema>
export type FindPetsByTagsQueryResponseSchema = FindPetsByTagsQueryResponse
14 changes: 8 additions & 6 deletions examples/advanced/src/gen/zod/petController/getPetByIdSchema.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,35 @@
import type { GetPetByIdPathParams, GetPetById200, GetPetById400, GetPetById404, GetPetByIdQueryResponse } from '../../models/ts/petController/GetPetById.ts'
import type { ToZod } from '@kubb/plugin-zod/utils'
import { petSchema } from '../petSchema.ts'
import { z } from 'zod'

export const getPetByIdPathParamsSchema = z.object({
petId: z.number().int().describe('ID of pet to return'),
})
} satisfies ToZod<GetPetByIdPathParams>)

export type GetPetByIdPathParamsSchema = z.infer<typeof getPetByIdPathParamsSchema>
export type GetPetByIdPathParamsSchema = GetPetByIdPathParams

/**
* @description successful operation
*/
export const getPetById200Schema = z.lazy(() => petSchema).and(z.object({ name: z.never() }))

export type GetPetById200Schema = z.infer<typeof getPetById200Schema>
export type GetPetById200Schema = GetPetById200

/**
* @description Invalid ID supplied
*/
export const getPetById400Schema = z.any()

export type GetPetById400Schema = z.infer<typeof getPetById400Schema>
export type GetPetById400Schema = GetPetById400

/**
* @description Pet not found
*/
export const getPetById404Schema = z.any()

export type GetPetById404Schema = z.infer<typeof getPetById404Schema>
export type GetPetById404Schema = GetPetById404

export const getPetByIdQueryResponseSchema = z.lazy(() => getPetById200Schema)

export type GetPetByIdQueryResponseSchema = z.infer<typeof getPetByIdQueryResponseSchema>
export type GetPetByIdQueryResponseSchema = GetPetByIdQueryResponse
Loading
Loading