Zod Section 10 exercises

Complex Schema Validation

So far our form validator is able to check for values.

Our Form has name, email, and optionally will have phoneNumber and website.

However, we want to put some constraints on what the values can be.

The user shouldn't be able to pass an invalid URLs, phone numbers, or website urls.


Loading exercise


Matt Pocock: 0:00 Here we're back to another form file data, except this one is a little bit more complex because we've got this name, phoneNumber, email, website. All of these things we're validating.

0:11 What we want to do is we want to say you can't pass an invalid URL to the website. You can't pass an invalid email to this email, either. You can't pass an invalid phone number to here.

0:23 For phone number, we're just using you can't pass a phone number if it's got too few characters and you can't pass a phone number with too many characters. We are saying if it's not a good email, then you can't pass it. If it doesn't look like an email there. You should throw when you pass an invalid website URL.

0:39 This is not a valid website URL. We need a http://facebook.com or whatever. Your challenge is to go into the Zod API and see if you can find some kind of neat little ways to do this validation because Zod does provide them.