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,
};
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
2 Replies
Brendonovich
Brendonovich2y ago
Have you tried z.literal()?
char
char2y ago
not yet! ill try it out. thank you!
Want results from more Discord servers?
Add your server