StefanTerdell / zod-to-json-schema

Converts Zod schemas to Json schemas
ISC License
854 stars 67 forks source link

feat: added onParseDef callback #84

Closed 0xcadams closed 5 months ago

0xcadams commented 1 year ago

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 commented 11 months ago

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

StefanTerdell commented 9 months ago

@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.

0xcadams commented 9 months ago

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.

StefanTerdell commented 9 months ago

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

0xcadams commented 9 months ago

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.

StefanTerdell commented 5 months ago

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

0xcadams commented 5 months ago

Sounds great - I will check out that new option