zod-to-json-schema icon indicating copy to clipboard operation
zod-to-json-schema copied to clipboard

feat: added onParseDef callback

Open 0xcadams opened this issue 1 year ago • 5 comments

This is to enable callbacks in the parsing of Zod schemas. The use-case is that I'm doing custom validation of the Zod schema (making sure there's a description, that no unsupported types are used, etc).

Let me know if you want to see the API look any different with this.

0xcadams avatar Jul 25 '23 15:07 0xcadams

Hey @StefanTerdell, just wanted to bump this. We're using your library with https://github.com/propology/hopfield and wanted to see if you're open to landing this change

0xcadams avatar Sep 29 '23 22:09 0xcadams

@0xcadams Hello! Sorry for the delay. I do kinda like the idea. Just wondering if it should receive the result even if its undefined to give the chance to create a node when the lib fails.

StefanTerdell avatar Nov 15 '23 09:11 StefanTerdell

No worries - cool, so you mean when selectParser fails - the interface would change to something like:

onParseDef: ((def: ZodTypeDef, refs: Refs, schema: JsonSchema7Type | undefined) => JsonSchema7Type | false | void) | undefined;

And that would be called always, and used as a backup in case the default parsers fail.

0xcadams avatar Nov 15 '23 18:11 0xcadams

Something like that. Not sure where the false in the return type fits in though?

StefanTerdell avatar Nov 15 '23 19:11 StefanTerdell

Okay great - I made those changes so onParseDef is always called and this is used as fallback in case the parser fails.

Let me know what you think. I can update docs as well, if this interface looks good to you.

0xcadams avatar Dec 12 '23 16:12 0xcadams

@0xcadams Hey, closing this due to inactivity, but the new override option should do what you were looking for

StefanTerdell avatar Mar 22 '24 19:03 StefanTerdell

Sounds great - I will check out that new option

0xcadams avatar Mar 22 '24 19:03 0xcadams