Zod: client and server side validation

It’s a validation tool. In the example @stucork gave at the top of this thread, he has a string which should be a valid email address.

He first defines the zod schema for a valid email address:

from anvil_labs import zod as z
email_schema = z.string().strip().email()

and then uses that to check three strings:

email_schema.parse("foo@bar.com")

which returns foo@bar.com because it passes the validation

email_schema.parse("   foo@bar.com   ")

which also returns foo@bar.com because the scheme strips the whitespace before running the check, and,

email_schema.parse("foo@")

which raises an error because the string isn’t a valid email address.

There are lots of other ways to create a schema described in the docs. This one is a simple string validation schema.

Also, because it’s not tied to anvil components, it can be used anywhere - both client and server side.

4 Likes