David AlonsoD
Convex Communityโ€ข16mo agoโ€ข
9 replies
David Alonso

prefix validator keys helper ๐Ÿ™

I'm playing around with this code:
// Helper function to prefix object keys for the Convex validator
function prefixValidatorKeys<T extends Record<string, any>>(obj: T, prefix: z.infer<typeof zBlockType>) {
  return Object.fromEntries(
    Object.entries(obj).map(([key, value]) => [`${prefix}_${key}`, value])
  );
}

// Create the Geopoint block properties validator
export const vGeopointBlockProperties = v.object({
  ...vCommonProps.fields,
  ...prefixValidatorKeys(vGeopointSpecificProps.fields, "geopoint"),
});

// Create a type that includes both common and prefixed specific properties
type PrefixProperties<T, P extends z.infer<typeof zBlockType>> = {
  [K in keyof T as `${P}_${string & K}`]: T[K]
};

export type GeopointBlockProperties = 
  Infer<typeof vCommonProps> & 
  PrefixProperties<Infer<typeof vGeopointSpecificProps>, 'geopoint'>;

const testGeopointBlock: GeopointBlockProperties = {
  gridLayout: { x: 0, y: 0 },
  geopoint_mapType: "roadmap",
  geopoint_zoom: 10,
};


The part that uses regular TS types works, but I'm having issues with the validators part, namely prefixValidatorKeys. Any idea of how this could be implemented?
Was this page helpful?