arktype
arktype copied to clipboard
[Request] Utility for creating unions from object keys
Request a feature
(Discord thread for context)
Our codebase has large collections of objects that represent enum values mapping keys of concrete types (like QualificationType
in my example below, which is a Prisma enum in our db) to string values that are rendered as labels in the UI, such as in select dropdowns.
It would be super helpful and valuable to have a way in arktype to convert these objects to string unions comprised of the enumerable keys in that object, accomplishing something similar to the arkEnumValidator
helper function I created below:
export const arkEnumValidator = <K extends string, V>(obj: Record<K, V>) => {
return type(
Object.keys(obj)
.map((t) => `'${t}'`)
.join('|') as Infer<K>
);
};
///
export const qualificationTypeOptions = {
drivers: "Driver's License",
cna: 'CNA Certification',
lpn: 'LPN License',
rn: 'RN License',
hha: 'HHA Certificate',
caregiver_cert: 'Caregiver Certificate',
first_aid: 'First Aid Certification',
cpr: 'CPR Certification',
food_handler_card: 'Food Handler Card',
custom: 'Custom'
} as const satisfies { [P in QualificationType]: string };
///
export const formSchema = type({
qualificationType: arkEnumValidator(qualificationTypeOptions)
});