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:
{"location":{"lat":123.123,"lng":1.23}}
---
{"location":{}}
---
{"location":null}
---
{"location":{"lat":null,"lng":null}}
---
{/* missing the field entirely */}
{"location":{"lat":123.123,"lng":1.23}}
---
{"location":{}}
---
{"location":null}
---
{"location":{"lat":null,"lng":null}}
---
{/* missing the field entirely */}
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
Neto
Neto•2y ago
npx ts-node path/to/folder/validate.ts on validate.ts you import the json file and handle that
stevekuznetsov
stevekuznetsov•2y ago
@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:
module.exports = function (api) {
api.cache(true);
return {
presets: ['babel-preset-expo'],
plugins: [
[
'module-resolver',
{
root: ['.'],
extensions: ['.tsx', '.ts', '.js', '.json'],
},
],
'react-native-reanimated/plugin',
],
};
};
module.exports = function (api) {
api.cache(true);
return {
presets: ['babel-preset-expo'],
plugins: [
[
'module-resolver',
{
root: ['.'],
extensions: ['.tsx', '.ts', '.js', '.json'],
},
],
'react-native-reanimated/plugin',
],
};
};
Is there something specific I need to do to get ts-node to honor this to allow imports to work as expected?
Neto
Neto•2y ago
can't you execute the script as a standalone?
stevekuznetsov
stevekuznetsov•2y ago
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 it
Neto
Neto•2y ago
can you separate the file from the react native modules?
stevekuznetsov
stevekuznetsov•2y ago
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 separate
Neto
Neto•2y ago
separate the zod schema from the react native .tsx file
stevekuznetsov
stevekuznetsov•2y ago
They're in a separate schemas.ts file that only imports zod right now and some enums from an enums.ts file next to it
Neto
Neto•2y ago
can you show the complete error?
stevekuznetsov
stevekuznetsov•2y ago
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
Neto
Neto•2y ago
i just created a new expo app with ts
stevekuznetsov
stevekuznetsov•2y ago
That worked!
Neto
Neto•2y ago
well fixed itself haha
stevekuznetsov
stevekuznetsov•2y ago
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.
Neto
Neto•2y ago
yeah i know the pain
stevekuznetsov
stevekuznetsov•2y ago
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 work
Neto
Neto•2y ago
im blaming babel on this one ts does support relative imports ~/components/Yeah the babel part is the yikes one
stevekuznetsov
stevekuznetsov•2y ago
So 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?
Neto
Neto•2y ago
yeah babel is being replaced for vite funny enough
stevekuznetsov
stevekuznetsov•2y ago
I haven't learned enough to switch to anything else 😉 What is the slower way to run this script without ts-node?
Neto
Neto•2y ago
transpile everything and run plain js files you can try bun running the file no transpile required
stevekuznetsov
stevekuznetsov•2y ago
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 ... ?
Neto
Neto•2y ago
usually just running tsc will build your app with typescript some other tools were created for additional features aside from just transpiling
Neto
Neto•2y ago
npm
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.
Neto
Neto•2y ago
Rollup
The JavaScript module bundler
stevekuznetsov
stevekuznetsov•2y ago
Gotcha, I'll try looking into that. Thank you so much for your help today
Neto
Neto•2y ago
glad to help
Want results from more Discord servers?
Add your server