stevekuznetsov
stevekuznetsov
TTCTheo's Typesafe Cult
Created by stevekuznetsov on 6/17/2023 in #questions
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.
49 replies