solana-idl-foundation / solana-idl-spec

Specifying a Solana IDL Standard, inspired from the Anchor IDL Standard
Apache License 2.0
20 stars 2 forks source link

Add typed zod schemas #3

Closed ngundotra closed 1 year ago

ngundotra commented 1 year ago

Add Idl types in from anchor-ts package, and add some explicit recursive typing.

ngundotra commented 1 year ago

Going to fix merge conflicts later, just wanted to show what I was thinking

ngundotra commented 1 year ago

@callensm It might be worth actually writing out some more smaller JSON examples of what we want to support and then bringing this up for greater review

callensm commented 1 year ago

@callensm It might be worth actually writing out some more smaller JSON examples of what we want to support and then bringing this up for greater review

yeah for sure. I'll try and get some more examples on my branch with smaller scopes up at some point today.

callensm commented 1 year ago

want to merge this so the types are in and then work from there with the examples?

thlorenz commented 1 year ago

Nice! @ngundotra could you try to generate the zod parts from the TypeScript types via ts-to-zod(like I discussed in #2 )? Then we could include those inside a zod subfolder to separate generated files.

I hope this works since I wouldn't want us to have to manually update the spec in two places, but I can read the TypeScript definitions much easier :)

ngundotra commented 1 year ago

@thlorenz! Hey yes. The issue is that the IdlType typescript Type itself is recursive, so that has to be manually crafted using z.lazy(() => z.object({...}). ts-to-zod does not support "complex circular dependencies" so any other typescript Type that references IdlType has to be manually crafted as well 😪.

I'm in favor of posting only the Typescript definitions at first + along with validation requirements, and then writing the validation code later.