Validating Zod Schemas Against Static Data
I'm building a React Native application and we are interfacing with a couple APIs written in Python and PHP that do real violence to the idea of type-safety, so one of our largest pain points has been correctly typing their HTTP responses and making sure our application is robust to all the wild ways that PHP can serialize an object. For instance, we've got an object with a field "location" that has the following possible forms:
I know how to model this kind of thing in
zod
, but what I am struggling with is ensuring that I've modeled every possible permutation. I've been able to download an enormous corpus of test data from the third-party API, and I want to write a script like yarn ci:validate-zod
or something that will load these files in and attempt to validate them all with the zod
schemas we've written. I'm having a hard time figuring out how to write a CLI script in TypeScript and import our schemas from the React Native modules. I'm usually a back-end developer in Rust & Go, so this is my first foray into TypeScript and I've only ever worked in React, not with Node, so please forgive me if this is a really simple question.
If someone can help me figure out how to set up the TypeScript compiler, etc, to get a script working that can import that zod
schema, I would be greatly appreciative.29 Replies
npx ts-node path/to/folder/validate.ts
on validate.ts
you import the json file and handle that@nyx (Rustular DevRel) thank you! I have written something simple, but am getting a module resolution error from
npx ts-node
... we've got this Babel config from whatever template:
Is there something specific I need to do to get ts-node
to honor this to allow imports to work as expected?can't you execute the script as a standalone?
Sorry, could you be more precise? What I'm trying to do is to write a stand-alone script, but import the
zod
schema from the rest of the project to validate itcan you separate the file from the react native modules?
Do you mean separate the script from the rest of the repo, or separate the
.ts
file with schema definitions from the rest of the React Native?
I'm sorry, I don't know enough to answer either question, really - but as long as there's some reasonably-simple way to get both the script and the rest of the React Native project importing one common set of schemas, I don't have any other requirements on how those schemas are or are not separateseparate the zod schema from the react native .tsx file
They're in a separate
schemas.ts
file that only imports zod
right now and some enums from an enums.ts
file next to itcan you show the complete error?
One sec - originally it was failing to find the module itself since I was using an absolute import. I've switched it to use a relative import, which seems to work, but now it's failing on the absolute imports between files in that module. I'm removing those imports right now to see if it will work if the schemas module has no imports inside of it
i just created a new expo app with ts
That worked!
well
fixed itself
haha
It would be nice to not have to collapse the entire module into one
.ts
file, but /shrug
Another dev on this project really liked the abolute imports from the babel config, instead of relative ones, but I am totally ambivalent.yeah
i know the pain
Thank you so much - if you had to describe the interaction between
expo
, ts-node
and babel
in one sentence, what would that be? I am kind of totally lost on how these big picture things workim blaming babel on this one
ts does support relative imports
~/components/Yeah
the babel part is the yikes oneSo is it that
ts-node
is somehow doing transpilation on the fly, and not using babel
under the hood?
Is there some "slower" way to write the script (not using ts-node
) that could re-use the babel
config?yeah
babel is being replaced for vite funny enough
I haven't learned enough to switch to anything else 😉
What is the slower way to run this script without
ts-node
?transpile everything and run plain js files
you can try bun running the file
no transpile required
Assuming I'm a super-noob and don't know how to "transpile everything and run plain js files" - is there some equivalent to
npx ts-node
to run?
Or a good blog post to read ... ?usually
just running
tsc
will build your app with typescript
some other tools were created for additional features aside from just transpilingnpm
tsup
Bundle your TypeScript library with no config, powered by esbuild. Latest version: 7.0.0, last published: a day ago. Start using tsup in your project by running
npm i tsup
. There are 418 other projects in the npm registry using tsup.Rollup
The JavaScript module bundler
Gotcha, I'll try looking into that. Thank you so much for your help today
glad to help