Skip to content

Commit

Permalink
chore: update examples
Browse files Browse the repository at this point in the history
  • Loading branch information
stijnvanhulle committed Nov 26, 2023
1 parent 58235db commit 17daeec
Show file tree
Hide file tree
Showing 10 changed files with 29 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { petSchema } from '../petSchema'
*/
export const findPetsByStatus400Schema = z.any()
export const findPetsByStatusQueryParamsSchema = z.object({
'status': z.enum([`available`, `pending`, `sold`]).describe(`Status values that need to be considered for filter`).default('available').optional(),
'status': z.enum([`available`, `pending`, `sold`]).default('available').describe(`Status values that need to be considered for filter`).optional(),
})

/**
Expand Down
2 changes: 1 addition & 1 deletion examples/advanced/src/gen/zod/userSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export const userSchema = z.object({
'username': z.string().optional(),
'firstName': z.string().optional(),
'lastName': z.string().optional(),
'email': z.string().optional(),
'email': z.string().email().optional(),
'password': z.string().optional(),
'phone': z.string().optional(),
'userStatus': z.number().describe(`User Status`).optional(),
Expand Down
2 changes: 1 addition & 1 deletion examples/zod/src/gen/zod/findPetsByStatusSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { petSchema } from './petSchema'
*/
export const findPetsByStatus400Schema = z.any()
export const findPetsByStatusQueryParamsSchema = z.object({
status: z.enum([`available`, `pending`, `sold`]).describe(`Status values that need to be considered for filter`).default('available').optional(),
status: z.enum([`available`, `pending`, `sold`]).default('available').describe(`Status values that need to be considered for filter`).optional(),
})

/**
Expand Down
2 changes: 1 addition & 1 deletion examples/zod/src/gen/zod/getInventorySchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ import { z } from 'zod'
/**
* @description successful operation
*/
export const getInventoryQueryResponseSchema = z.object({}).catchall(z.number())
export const getInventoryQueryResponseSchema = z.object({}).catchall(z.number().min(-2147483648).max(2147483647))
2 changes: 1 addition & 1 deletion examples/zod/src/gen/zod/userSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export const userSchema = z.object({
username: z.string().optional(),
firstName: z.string().optional(),
lastName: z.string().optional(),
email: z.string().optional(),
email: z.string().email().optional(),
password: z.string().optional(),
phone: z.string().optional(),
userStatus: z.number().describe(`User Status`).optional(),
Expand Down
2 changes: 1 addition & 1 deletion examples/zodios/src/gen/zod/findPetsByStatusSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { petSchema } from './petSchema'
*/
export const findPetsByStatus400Schema = z.any()
export const findPetsByStatusQueryParamsSchema = z.object({
status: z.enum([`available`, `pending`, `sold`]).describe(`Status values that need to be considered for filter`).default('available').optional(),
status: z.enum([`available`, `pending`, `sold`]).default('available').describe(`Status values that need to be considered for filter`).optional(),
})

/**
Expand Down
2 changes: 1 addition & 1 deletion examples/zodios/src/gen/zod/getInventorySchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ import { z } from 'zod'
/**
* @description successful operation
*/
export const getInventoryQueryResponseSchema = z.object({}).catchall(z.number())
export const getInventoryQueryResponseSchema = z.object({}).catchall(z.number().min(-2147483648).max(2147483647))
2 changes: 1 addition & 1 deletion examples/zodios/src/gen/zod/userSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export const userSchema = z.object({
username: z.string().optional(),
firstName: z.string().optional(),
lastName: z.string().optional(),
email: z.string().optional(),
email: z.string().email().optional(),
password: z.string().optional(),
phone: z.string().optional(),
userStatus: z.number().describe(`User Status`).optional(),
Expand Down
34 changes: 19 additions & 15 deletions packages/swagger-zod/src/ZodGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,21 @@ export class ZodGenerator extends Generator<PluginOptions['resolvedOptions'], Co
return this.#getRefAlias(schema, baseName)
}

const baseItems: ZodMeta[] = []

if (schema.default !== undefined && !Array.isArray(schema.default)) {
if (typeof schema.default === 'string') {
baseItems.push({ keyword: zodKeywords.default, args: `"${schema.default}"` })
}
if (typeof schema.default === 'boolean') {
baseItems.push({ keyword: zodKeywords.default, args: schema.default ?? false })
}
}

if (schema.description) {
baseItems.push({ keyword: zodKeywords.describe, args: `\`${schema.description.replaceAll('\n', ' ').replaceAll('`', "'")}\`` })
}

if (schema.oneOf) {
// union
const schemaWithoutOneOf = { ...schema, oneOf: undefined }
Expand Down Expand Up @@ -240,6 +255,7 @@ export class ZodGenerator extends Generator<PluginOptions['resolvedOptions'], Co
keyword: zodKeywords.enum,
args: [...new Set(schema['x-enumNames'] as string[])].map((value: string) => `\`${value}\``),
},
...baseItems,
]
}

Expand All @@ -255,6 +271,7 @@ export class ZodGenerator extends Generator<PluginOptions['resolvedOptions'], Co
}
}),
},
...baseItems,
]
}

Expand All @@ -263,12 +280,13 @@ export class ZodGenerator extends Generator<PluginOptions['resolvedOptions'], Co
keyword: zodKeywords.enum,
args: [...new Set(schema.enum)].map((value: string) => `\`${value}\``),
},
...baseItems,
]
}

if ('items' in schema) {
// items -> array
return [{ keyword: zodKeywords.array, args: this.getTypeFromSchema(schema.items as OasTypes.SchemaObject, baseName) }]
return [{ keyword: zodKeywords.array, args: this.getTypeFromSchema(schema.items as OasTypes.SchemaObject, baseName) }, ...baseItems]
}

if ('prefixItems' in schema) {
Expand Down Expand Up @@ -322,20 +340,6 @@ export class ZodGenerator extends Generator<PluginOptions['resolvedOptions'], Co
nullable ? { keyword: zodKeywords.nullable } : undefined,
].filter(Boolean)
}
const baseItems: ZodMeta[] = []

if (schema.default !== undefined && !Array.isArray(schema.default)) {
if (typeof schema.default === 'string') {
baseItems.push({ keyword: zodKeywords.default, args: `"${schema.default}"` })
}
if (typeof schema.default === 'boolean') {
baseItems.push({ keyword: zodKeywords.default, args: schema.default ?? false })
}
}

if (schema.description) {
baseItems.push({ keyword: zodKeywords.describe, args: `\`${schema.description.replaceAll('\n', ' ').replaceAll('`', "'")}\`` })
}

if (schema.type === zodKeywords.number || schema.type === zodKeywords.integer) {
const min = schema.minimum ?? schema.minLength ?? schema.minItems ?? undefined
Expand Down
4 changes: 2 additions & 2 deletions packages/swagger-zod/src/zodParser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,10 @@ const input = [
keyword: 'object',
args: {
firstName: [{ keyword: 'string' }, { keyword: 'min', args: 2 }],
address: [{ keyword: 'string' }, { keyword: 'null' }, { keyword: 'describe', args: '"Your address"' }],
address: [{ keyword: 'string' }, { keyword: 'nullable' }, { keyword: 'describe', args: '"Your address"' }],
},
}),
expected: 'z.object({"firstName": z.string().min(2),"address": z.string().describe("Your address").nullable()})',
expected: 'z.object({"firstName": z.string().min(2),"address": z.string().nullable().describe("Your address")})',
},
{
input: parseZodMeta({
Expand Down

0 comments on commit 17daeec

Please sign in to comment.