Zod Section 10 exercises

Create an Array of Custom Types

In this example, we're still using the Star Wars API but this time we want all of the people it returns.

The starting point is similar to what we saw previously, where the StarWarsPeopleResults variable is set to z.unknown().

const StarWarsPeopleResults = z.unknown();
export con

Loading exercise


Matt Pocock: 0:00 We've got a different problem this time. We're actually fetching from a different API endpoint on Swapi. We're not fetching all of the people. All of the people here, the way this looks is it's kind of like an interface. Let's say Results {. It's got results: . This is an array of objects. It's name: string; basically.

0:24 If we console.log this data that's coming back, you'll be able to see what's going on because, again, we've got some text down here to say expect StarWarsPeople to equal this. You can see there that inside the console.log here, there we go, there's an enormous amount of data in here, but it's inside a results object here.

0:43 There are some other keys, too, like the count, like the results here. We've got to find some way of representing an array inside Zod because we need to reuse this StarWarsPerson.

0:56 We've got our name. We can add eye color if we want to. This just isn't working at the moment because it's not parsing the data correctly and we're getting a type error here.