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 1 commit
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
34 changes: 34 additions & 0 deletions packages/openapi-ts/src/plugins/zod/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,12 @@ const zIdentifier = compiler.identifier({ text: 'z' });
const nameTransformer = (name: string) => `z-${name}`;

const arrayTypeToZodSchema = ({
config,
context,
result,
schema,
}: {
config: Omit<Config, 'name'>;
context: IR.Context;
result: Result;
schema: SchemaWithType<'array'>;
Expand Down Expand Up @@ -73,6 +75,7 @@ const arrayTypeToZodSchema = ({
// at least one item is guaranteed
const itemExpressions = schema.items!.map((item) =>
schemaToZodSchema({
config,
context,
result,
schema: item,
Expand Down Expand Up @@ -331,10 +334,12 @@ const numberTypeToZodSchema = ({
};

const objectTypeToZodSchema = ({
config,
context,
result,
schema,
}: {
config: Omit<Config, 'name'>;
context: IR.Context;
result: Result;
schema: SchemaWithType<'object'>;
Expand All @@ -353,6 +358,7 @@ const objectTypeToZodSchema = ({
const isRequired = required.includes(name);

const propertyExpression = schemaToZodSchema({
config,
context,
optional: !isRequired,
result,
Expand Down Expand Up @@ -432,8 +438,10 @@ const objectTypeToZodSchema = ({
};

const stringTypeToZodSchema = ({
config,
schema,
}: {
config: Omit<Config, 'name'>;
context: IR.Context;
schema: SchemaWithType<'string'>;
}) => {
Expand Down Expand Up @@ -463,6 +471,11 @@ const stringTypeToZodSchema = ({
expression: stringExpression,
name: compiler.identifier({ text: 'datetime' }),
}),
parameters: [
config.dateTimeOptions
? JSON.stringify(config.dateTimeOptions)
: undefined,
],
});
break;
case 'ipv4':
Expand Down Expand Up @@ -642,17 +655,20 @@ const voidTypeToZodSchema = ({
};

const schemaTypeToZodSchema = ({
config,
context,
result,
schema,
}: {
config: Omit<Config, 'name'>;
context: IR.Context;
result: Result;
schema: IR.SchemaObject;
}): ts.Expression => {
switch (schema.type as Required<IR.SchemaObject>['type']) {
case 'array':
return arrayTypeToZodSchema({
config,
context,
result,
schema: schema as SchemaWithType<'array'>,
Expand Down Expand Up @@ -685,12 +701,14 @@ const schemaTypeToZodSchema = ({
});
case 'object':
return objectTypeToZodSchema({
config,
context,
result,
schema: schema as SchemaWithType<'object'>,
});
case 'string':
return stringTypeToZodSchema({
config,
context,
schema: schema as SchemaWithType<'string'>,
});
Expand Down Expand Up @@ -718,10 +736,12 @@ const schemaTypeToZodSchema = ({
};

const operationToZodSchema = ({
config,
context,
operation,
result,
}: {
config: Omit<Config, 'name'>;
context: IR.Context;
operation: IR.OperationObject;
result: Result;
Expand All @@ -736,6 +756,7 @@ const operationToZodSchema = ({
id: operation.id,
type: 'response',
}),
config,
context,
result,
schema: response,
Expand All @@ -746,6 +767,7 @@ const operationToZodSchema = ({

const schemaToZodSchema = ({
$ref,
config,
context,
optional,
result,
Expand All @@ -755,6 +777,7 @@ const schemaToZodSchema = ({
* When $ref is supplied, a node will be emitted to the file.
*/
$ref?: string;
config: Omit<Config, 'name'>;
context: IR.Context;
/**
* Accept `optional` to handle optional object properties. We can't handle
Expand Down Expand Up @@ -798,6 +821,7 @@ const schemaToZodSchema = ({
if (!identifierRef.name) {
const ref = context.resolveIrRef<IR.SchemaObject>(schema.$ref);
expression = schemaToZodSchema({
config,
context,
result,
schema: ref,
Expand Down Expand Up @@ -836,6 +860,7 @@ const schemaToZodSchema = ({
}
} else if (schema.type) {
expression = schemaTypeToZodSchema({
config,
context,
result,
schema,
Expand All @@ -846,6 +871,7 @@ const schemaToZodSchema = ({
if (schema.items) {
const itemTypes = schema.items.map((item) =>
schemaToZodSchema({
config,
context,
result,
schema: item,
Expand Down Expand Up @@ -895,6 +921,7 @@ const schemaToZodSchema = ({
}
} else {
expression = schemaToZodSchema({
config,
context,
result,
schema,
Expand All @@ -903,6 +930,7 @@ const schemaToZodSchema = ({
} else {
// catch-all fallback for failed schemas
expression = schemaTypeToZodSchema({
config,
context,
result,
schema: {
Expand Down Expand Up @@ -982,13 +1010,18 @@ export const handler: Plugin.Handler<Config> = ({ context, plugin }) => {
name: 'z',
});

const config: Omit<Config, 'name'> = {
dateTimeOptions: plugin.dateTimeOptions,
};

context.subscribe('operation', ({ operation }) => {
const result: Result = {
circularReferenceTracker: new Set(),
hasCircularReference: false,
};

operationToZodSchema({
config,
context,
operation,
result,
Expand All @@ -1003,6 +1036,7 @@ export const handler: Plugin.Handler<Config> = ({ context, plugin }) => {

schemaToZodSchema({
$ref,
config,
context,
result,
schema,
Expand Down
13 changes: 11 additions & 2 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 All @@ -13,8 +22,8 @@ export interface Config extends Plugin.Name<'zod'> {
* Customise the Zod schema name. By default, `z{{name}}` is used,
* where `name` is a definition name or an operation name.
*/
// nameBuilder?: (model: IR.OperationObject | IR.SchemaObject) => string;
/**
// nameBuilder?: (model: IR.OperationObject | IR.SchemaObject) => string;
/**
* Name of the generated file.
*
* @default 'zod'
Expand Down