Theo's Typesafe CultTTC
Theo's Typesafe Cult3y ago
2 replies
char

Is there a way to create "const objects" from zod?

Im looking to create an object of default values, and an object of literals to use as an enum:

export const homeAddressFormSchema = z.object({
  clientHomeAddressLine1: z.string().min(1).optional(),
  clientHomeAddressLine2: z.string().min(1).optional(),
  clientHomeCity: z.string().min(1).optional(),
  clientHomeState: z.string().min(1).optional(),
  clientHomeZipCode: z.string().min(1).optional(),
});

export type HomeAddressFormData = z.infer<typeof homeAddressFormSchema>;



// object of literals I'm trying to extract from zod schema 
export const HomeAddressConst = {
  clientHomeAddressLine1: 'clientHomeAddressLine1',
  clientHomeAddressLine2: 'clientHomeAddressLine2',
  clientHomeCity: 'clientHomeCity',
  clientHomeState: 'clientHomeState',
  clientHomeZipCode: 'clientHomeZipCode',
} as const;

// object of initial values I'm trying to extract from zod schema 
const initialHomeAddressFormData: HomeAddressFormData = {
  clientHomeAddressLine1: undefined,
  clientHomeAddressLine2: undefined,
  clientHomeCity: undefined,
  clientHomeState: undefined,
  clientHomeZipCode: undefined,
};


context is this is setup for a form
Was this page helpful?