Skip to content

Add options for datetime into zod plugin #1881

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
30 changes: 30 additions & 0 deletions packages/openapi-ts/src/plugins/zod/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,12 @@

const arrayTypeToZodSchema = ({
context,
plugin,

Check warning on line 44 in packages/openapi-ts/src/plugins/zod/plugin.ts

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/plugins/zod/plugin.ts#L44

Added line #L44 was not covered by tests
result,
schema,
}: {
context: IR.Context;
plugin: Config;

Check warning on line 49 in packages/openapi-ts/src/plugins/zod/plugin.ts

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/plugins/zod/plugin.ts#L49

Added line #L49 was not covered by tests
result: Result;
schema: SchemaWithType<'array'>;
}): ts.CallExpression => {
Expand Down Expand Up @@ -74,6 +76,7 @@
const itemExpressions = schema.items!.map((item) =>
schemaToZodSchema({
context,
plugin,

Check warning on line 79 in packages/openapi-ts/src/plugins/zod/plugin.ts

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/plugins/zod/plugin.ts#L79

Added line #L79 was not covered by tests
result,
schema: item,
}),
Expand Down Expand Up @@ -332,10 +335,12 @@

const objectTypeToZodSchema = ({
context,
plugin,

Check warning on line 338 in packages/openapi-ts/src/plugins/zod/plugin.ts

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/plugins/zod/plugin.ts#L338

Added line #L338 was not covered by tests
result,
schema,
}: {
context: IR.Context;
plugin: Config;

Check warning on line 343 in packages/openapi-ts/src/plugins/zod/plugin.ts

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/plugins/zod/plugin.ts#L343

Added line #L343 was not covered by tests
result: Result;
schema: SchemaWithType<'object'>;
}) => {
Expand All @@ -355,6 +360,7 @@
const propertyExpression = schemaToZodSchema({
context,
optional: !isRequired,
plugin,

Check warning on line 363 in packages/openapi-ts/src/plugins/zod/plugin.ts

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/plugins/zod/plugin.ts#L363

Added line #L363 was not covered by tests
result,
schema: property,
});
Expand Down Expand Up @@ -432,9 +438,11 @@
};

const stringTypeToZodSchema = ({
plugin,

Check warning on line 441 in packages/openapi-ts/src/plugins/zod/plugin.ts

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/plugins/zod/plugin.ts#L441

Added line #L441 was not covered by tests
schema,
}: {
context: IR.Context;
plugin: Config;

Check warning on line 445 in packages/openapi-ts/src/plugins/zod/plugin.ts

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/plugins/zod/plugin.ts#L445

Added line #L445 was not covered by tests
schema: SchemaWithType<'string'>;
}) => {
if (typeof schema.const === 'string') {
Expand Down Expand Up @@ -463,6 +471,11 @@
expression: stringExpression,
name: compiler.identifier({ text: 'datetime' }),
}),
parameters: [
plugin.dateTimeOptions
? JSON.stringify(plugin.dateTimeOptions)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm actually surprised this works 😂 it wouldn't work if you passed the object instead of string? I'd feel more comfortable with doing that, although being even more explicit and constructing the object would be ideal. Right now I'd need to go back to config to check what it contains. I worry it will break if the config shape changes. I don't want to introduce this as a pattern for other plugins, what do you think?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried other things but the only thing is working is to pass a stringfiied object to have the correct output.

There is another way ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to see that parameter object being constructed in the plugin, even if it's assigning the same config values to an object. Have a look at how we do parameters elsewhere we call this function. It will eliminate the need to cross reference another file

: undefined,
],

Check warning on line 478 in packages/openapi-ts/src/plugins/zod/plugin.ts

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/plugins/zod/plugin.ts#L474-L478

Added lines #L474 - L478 were not covered by tests
});
break;
case 'ipv4':
Expand Down Expand Up @@ -643,17 +656,20 @@

const schemaTypeToZodSchema = ({
context,
plugin,

Check warning on line 659 in packages/openapi-ts/src/plugins/zod/plugin.ts

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/plugins/zod/plugin.ts#L659

Added line #L659 was not covered by tests
result,
schema,
}: {
context: IR.Context;
plugin: Config;

Check warning on line 664 in packages/openapi-ts/src/plugins/zod/plugin.ts

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/plugins/zod/plugin.ts#L664

Added line #L664 was not covered by tests
result: Result;
schema: IR.SchemaObject;
}): ts.Expression => {
switch (schema.type as Required<IR.SchemaObject>['type']) {
case 'array':
return arrayTypeToZodSchema({
context,
plugin,

Check warning on line 672 in packages/openapi-ts/src/plugins/zod/plugin.ts

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/plugins/zod/plugin.ts#L672

Added line #L672 was not covered by tests
result,
schema: schema as SchemaWithType<'array'>,
});
Expand Down Expand Up @@ -686,12 +702,14 @@
case 'object':
return objectTypeToZodSchema({
context,
plugin,

Check warning on line 705 in packages/openapi-ts/src/plugins/zod/plugin.ts

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/plugins/zod/plugin.ts#L705

Added line #L705 was not covered by tests
result,
schema: schema as SchemaWithType<'object'>,
});
case 'string':
return stringTypeToZodSchema({
context,
plugin,

Check warning on line 712 in packages/openapi-ts/src/plugins/zod/plugin.ts

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/plugins/zod/plugin.ts#L712

Added line #L712 was not covered by tests
schema: schema as SchemaWithType<'string'>,
});
case 'tuple':
Expand Down Expand Up @@ -720,10 +738,12 @@
const operationToZodSchema = ({
context,
operation,
plugin,

Check warning on line 741 in packages/openapi-ts/src/plugins/zod/plugin.ts

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/plugins/zod/plugin.ts#L741

Added line #L741 was not covered by tests
result,
}: {
context: IR.Context;
operation: IR.OperationObject;
plugin: Config;

Check warning on line 746 in packages/openapi-ts/src/plugins/zod/plugin.ts

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/plugins/zod/plugin.ts#L746

Added line #L746 was not covered by tests
result: Result;
}) => {
if (operation.responses) {
Expand All @@ -737,6 +757,7 @@
type: 'response',
}),
context,
plugin,

Check warning on line 760 in packages/openapi-ts/src/plugins/zod/plugin.ts

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/plugins/zod/plugin.ts#L760

Added line #L760 was not covered by tests
result,
schema: response,
});
Expand All @@ -748,6 +769,7 @@
$ref,
context,
optional,
plugin,

Check warning on line 772 in packages/openapi-ts/src/plugins/zod/plugin.ts

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/plugins/zod/plugin.ts#L772

Added line #L772 was not covered by tests
result,
schema,
}: {
Expand All @@ -762,6 +784,7 @@
* `.default()` which is handled in this function.
*/
optional?: boolean;
plugin: Config;

Check warning on line 787 in packages/openapi-ts/src/plugins/zod/plugin.ts

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/plugins/zod/plugin.ts#L787

Added line #L787 was not covered by tests
result: Result;
schema: IR.SchemaObject;
}): ts.Expression => {
Expand Down Expand Up @@ -799,6 +822,7 @@
const ref = context.resolveIrRef<IR.SchemaObject>(schema.$ref);
expression = schemaToZodSchema({
context,
plugin,

Check warning on line 825 in packages/openapi-ts/src/plugins/zod/plugin.ts

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/plugins/zod/plugin.ts#L825

Added line #L825 was not covered by tests
result,
schema: ref,
});
Expand Down Expand Up @@ -837,6 +861,7 @@
} else if (schema.type) {
expression = schemaTypeToZodSchema({
context,
plugin,

Check warning on line 864 in packages/openapi-ts/src/plugins/zod/plugin.ts

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/plugins/zod/plugin.ts#L864

Added line #L864 was not covered by tests
result,
schema,
});
Expand All @@ -847,6 +872,7 @@
const itemTypes = schema.items.map((item) =>
schemaToZodSchema({
context,
plugin,

Check warning on line 875 in packages/openapi-ts/src/plugins/zod/plugin.ts

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/plugins/zod/plugin.ts#L875

Added line #L875 was not covered by tests
result,
schema: item,
}),
Expand Down Expand Up @@ -896,6 +922,7 @@
} else {
expression = schemaToZodSchema({
context,
plugin,

Check warning on line 925 in packages/openapi-ts/src/plugins/zod/plugin.ts

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/plugins/zod/plugin.ts#L925

Added line #L925 was not covered by tests
result,
schema,
});
Expand All @@ -904,6 +931,7 @@
// catch-all fallback for failed schemas
expression = schemaTypeToZodSchema({
context,
plugin,

Check warning on line 934 in packages/openapi-ts/src/plugins/zod/plugin.ts

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/plugins/zod/plugin.ts#L934

Added line #L934 was not covered by tests
result,
schema: {
type: 'unknown',
Expand Down Expand Up @@ -991,6 +1019,7 @@
operationToZodSchema({
context,
operation,
plugin,

Check warning on line 1022 in packages/openapi-ts/src/plugins/zod/plugin.ts

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/plugins/zod/plugin.ts#L1022

Added line #L1022 was not covered by tests
result,
});
});
Expand All @@ -1004,6 +1033,7 @@
schemaToZodSchema({
$ref,
context,
plugin,

Check warning on line 1036 in packages/openapi-ts/src/plugins/zod/plugin.ts

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/plugins/zod/plugin.ts#L1036

Added line #L1036 was not covered by tests
result,
schema,
});
Expand Down
9 changes: 9 additions & 0 deletions packages/openapi-ts/src/plugins/zod/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@
import type { Plugin } from '../types';

export interface Config extends Plugin.Name<'zod'> {
/**
* Zod options for the generated schema about datetimes.
* https://zod.dev/?id=datetimes
*/
dateTimeOptions?: {
local?: boolean;
offset?: boolean;
precision?: number;
};
/**
* Should the exports from the generated files be re-exported in the index
* barrel file?
Expand Down