Null fields not working as expected

Hi all,

I am using Drizzle ORM with Zod validation for useForm, and I am running into issues with fields that are allowed to be undefined / null, but I get Zod errors stating that the value of the optional fields (when left empty) are invalid_type and in required.

I was able to fix this by refining the field as follows:
export const selectDeploymentSchema = createSelectSchema(deployments, {
  city: (deployments) => deployments.city.nullish(),
});


This allows the city field to be left blank, but by default, even though i didn't specify notNull() on that field, Zod would error out saying that the field is required.

I am using Turso DB, so this is my schema:

export const deployments = sqliteTable("deployments", {
  id: integer("id").primaryKey(),
  organizationId: integer("organization_id")
    .notNull()
    .references(() => organizations.id),
  name: text("name").notNull().unique(),
  description: text("description"),
  streetAddress: text("street_address"),
  city: text("city"),
  state: text("state"),
  zip: text("zip"),
});


Notice for all fields by the id, organizationId, and name, they are optional; however, Zod treats them as ZodNullable but they should in fact be treated as ZodOptional<ZodNullable<ZodString>> to allow my fields to be empty (as is the intended affect).
Was this page helpful?