Closed veganbeef closed 4 months ago
@veganbeef thank you for the kind word. Your issue is that z.custom
basically means anything (including undefined) as far as zod is concerned => the required field is false by default and so is the type. You've correctly addded the type manually. And now all you need to do is to add the required manually as well through the params
prop that we've exposed. So you schema should look like this:
const ethAddress = z
.custom<`0x${string}`>((val: any) => /^0x[a-fA-F0-9]{40}$/.test(val))
.openapi({
type: 'string',
pattern: '^0x[a-fA-F0-9]{40}$',
param: { required: true },
});
I am closing the issue since I've tested that this would produce the following result:
"parameters": [
{
"schema": {
"type": "string"
},
"required": true,
"name": "chainId",
"in": "path"
},
{
"schema": {
"type": "string",
"pattern": "^0x[a-fA-F0-9]{40}$"
},
"required": true,
"name": "contractAddress",
"in": "path"
}
],
If there are still problems - let us know and feel free to reopen the issue
@AGalabov this works, thank you for the assistance!
Thanks for all your work on this super handy library!
I'm encountering the following issue with the generated OpenAPI JSON file when using
zod-to-openapi
version 6.4.0:When generating an openapi.json file for a schema with path parameters that use a zod schema defined with
z.custom()
, the JSON output lists the parameter as"required": "false"
.However, this must be set to
true
according to the OpenAPI Specification.Example:
The above code results in the following generated openapi.json segment with the incorrect
false
value:And that in turn results in the following schema validation error: