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

feat: added option to coerce specific types in plugin-zod #1282

Merged
merged 4 commits into from
Oct 4, 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/stupid-jeans-rush.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@kubb/plugin-zod": minor
---

Added coercion for specific types only
7 changes: 6 additions & 1 deletion docs/plugins/plugin-zod/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ Use of z.coerce.string() instead of z.string().

| | |
|----------:|:----------|
| Type: | `boolean` |
| Type: | `boolean \| { dates?: boolean, strings?: boolean, numbers?: boolean}` |
| Required: | `false` |
| Default: | `false'` |

Expand All @@ -201,6 +201,11 @@ z.string()
z.date()
z.number()
```
```typescript [{numbers: true, strings: false, dates: false}]
z.string()
ChilloManiac marked this conversation as resolved.
Show resolved Hide resolved
z.date()
z.coerce.number()
```
:::

### operations
Expand Down
3 changes: 3 additions & 0 deletions packages/plugin-zod/src/generators/__snapshots__/coercion.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { z } from "zod";

export const pet = z.object({ "id": z.coerce.number().int(), "name": z.coerce.string(), "date": z.coerce.date().optional(), "tag": z.coerce.string().min(5).max(100).optional() });
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { z } from "zod";

export const pet = z.object({ "id": z.number().int(), "name": z.string(), "date": z.coerce.date().optional(), "tag": z.string().min(5).max(100).optional() });
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { z } from "zod";

export const pet = z.object({ "id": z.coerce.number().int(), "name": z.string(), "date": z.date().optional(), "tag": z.string().min(5).max(100).optional() });
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { z } from "zod";

export const pet = z.object({ "id": z.number().int(), "name": z.coerce.string(), "date": z.date().optional(), "tag": z.coerce.string().min(5).max(100).optional() });
32 changes: 32 additions & 0 deletions packages/plugin-zod/src/generators/zodGenerator.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,38 @@ describe('zodGenerator schema', async () => {
input: '../../mocks/discriminator.yaml',
options: {},
},
{
name: 'coercion',
path: 'Pet',
input: '../../mocks/petStore.yaml',
options: {
coercion: true,
},
},
{
name: 'coercion-strings',
path: 'Pet',
input: '../../mocks/petStore.yaml',
options: {
coercion: { strings: true },
},
},
{
name: 'coercion-numbers',
path: 'Pet',
input: '../../mocks/petStore.yaml',
options: {
coercion: { numbers: true },
},
},
{
name: 'coercion-dates',
path: 'Pet',
input: '../../mocks/petStore.yaml',
options: {
coercion: { dates: true },
},
},
] as const satisfies Array<{
input: string
name: string
Expand Down
23 changes: 17 additions & 6 deletions packages/plugin-zod/src/parser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,14 +150,25 @@ export function sort(items?: Schema[]): Schema[] {
return transformers.orderBy(items, [(v) => order.indexOf(v.keyword)], ['asc'])
}

const shouldCoerce = (coercion: ParserOptions['coercion'] | undefined, type: 'dates' | 'strings' | 'numbers'): boolean => {
ChilloManiac marked this conversation as resolved.
Show resolved Hide resolved
if (coercion === undefined) {
return false
}
if (typeof coercion === 'boolean') {
return coercion
}

return !!coercion[type]
}

type ParserOptions = {
name: string
typeName?: string
description?: string

keysToOmit?: string[]
mapper?: Record<string, string>
coercion?: boolean
coercion?: boolean | { dates?: boolean; strings?: boolean; numbers?: boolean }
}

export function parse(parent: Schema | undefined, current: Schema, options: ParserOptions): string | undefined {
Expand Down Expand Up @@ -325,15 +336,15 @@ export function parse(parent: Schema | undefined, current: Schema, options: Pars
}

if (isKeyword(current, schemaKeywords.string)) {
return zodKeywordMapper.string(options.coercion)
return zodKeywordMapper.string(shouldCoerce(options.coercion, 'strings'))
}

if (isKeyword(current, schemaKeywords.number)) {
return zodKeywordMapper.number(options.coercion)
return zodKeywordMapper.number(shouldCoerce(options.coercion, 'numbers'))
}

if (isKeyword(current, schemaKeywords.integer)) {
return zodKeywordMapper.integer(options.coercion)
return zodKeywordMapper.integer(shouldCoerce(options.coercion, 'numbers'))
}

if (isKeyword(current, schemaKeywords.min)) {
Expand All @@ -348,11 +359,11 @@ export function parse(parent: Schema | undefined, current: Schema, options: Pars
}

if (isKeyword(current, schemaKeywords.date)) {
return zodKeywordMapper.date(current.args.type, options.coercion)
return zodKeywordMapper.date(current.args.type, shouldCoerce(options.coercion, 'dates'))
}

if (isKeyword(current, schemaKeywords.time)) {
return zodKeywordMapper.time(current.args.type, options.coercion)
return zodKeywordMapper.time(current.args.type, shouldCoerce(options.coercion, 'dates'))
}

if (current.keyword in zodKeywordMapper && 'args' in current) {
Expand Down
11 changes: 9 additions & 2 deletions packages/plugin-zod/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,15 @@ export type Options = {
inferred?: boolean
/**
* Use of z.coerce.string() instead of z.string()
* can also be an object to enable coercion for dates, strings, and numbers
*/
coercion?: boolean
coercion?:
| boolean
| {
dates?: boolean
strings?: boolean
numbers?: boolean
}
operations?: boolean
mapper?: Record<string, string>
transformers?: {
Expand Down Expand Up @@ -93,7 +100,7 @@ type ResolvedOptions = {
mapper: NonNullable<Options['mapper']>
importPath: NonNullable<Options['importPath']>
coercion: NonNullable<Options['coercion']>
operations: NonNullable<Options['coercion']>
operations: NonNullable<Options['operations']>
ChilloManiac marked this conversation as resolved.
Show resolved Hide resolved
}

export type PluginZod = PluginFactoryOptions<'plugin-zod', Options, ResolvedOptions, never, ResolvePathOptions>
Loading