astahmer / openapi-zod-client

Generate a zodios (typescript http client with zod validation) from an OpenAPI spec (json/yaml)

Home Page:openapi-zod-client.vercel.app

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Allow timezone offsets for ISO datetimes

leogallot opened this issue · comments

The ISO datetimes that my client receives from the server contain the timezone information "2020-01-01T00:00:00+02:00", which causes the validation to fail as the timezone offsets are not enabled.

Is there a way to allow the timezone offsets for string datetimes? z.string().datetime({ offset: true });
https://zod.dev/?id=iso-datetimes

not atm, you could add a datetime validation in here

Hello Leogallot,

I had the exact same requirement, but indeed it's not supported. As a workaround, I did the following :

package.json

"generate-zod-client": "npx openapi-zod-client http://localhost:8080/openapi.yaml -o ./src/api/openapizod.ts && node fixDatesZodios.cjs"

fixDatesZodios.cjs

// this script has been written to add {offset : true} in dateTime zod schemas, to correctly handle dateTime from the backend
// It is directly called in the script "generate-zod-client" defined in "package.json"

const fs = require('fs');

// Read the file contents into a string
let fileContent = fs.readFileSync('./src/api/openapizod.ts', 'utf-8');

// Replace all occurrences of '.datetime()' with '.datetime({ offset: true })'
fileContent = fileContent.replace(/\.datetime\(\)/g, '.datetime({ offset: true })');

// Write the modified content back to the file
fs.writeFileSync('./src/api/openapizod.ts', fileContent, 'utf-8');

I hope it can help, and if integrated in this lib I would appreciate, many thanks for this lib in any cases :)

Fixed in #161