1 min read 179 words Updated Sep 24, 2026 Created Sep 24, 2026
##TypeScript#TypeScript#backend#frontend

Zod is a #TypeScript schema validation library.

Basic API

The following is just an overview, consider the actual docs for more details.

Functions

  • parse() will throw an error if the validation is incorrect
  • safeParse() will not throw an error

Types

Dealing with partially correct schemas

There can be multiple types of partially correct schemas - based on types or even raw values, e. g. when having an array / set of potential values, but the input to be validated holds a value not given in the set.

For both case, use safeParse to avoid an error and catch incorrect values for an attribute with undefined. Thus, the schema won't fail and one can precisely work for correct and incorrect values.

// ... Schema definition: 
name: z.string().optional().catch(undefined)
state: z.enum(['ON', 'OFF']).optional().catch(undefined),

const mySchema = {
	name: 23, 
	state: 'Unkown'	
}

safeParse(mySchema) // Will return the schema object with undefined for fields that aren't correct 

Casting types / coerce

Will coerce possible strings to a number:

weight: z.coerce.number(), 

Dealing with