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:
context is this is setup for a form
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,
};
2 Replies
Have you tried
z.literal()
?not yet! ill try it out. thank you!