How to omit/pick a value from an ZodEnum? #1922
Unanswered
PhilBookst
asked this question in
Q&A
Replies: 3 comments 2 replies
-
Is this what you are looking for? export const OccupationSchema = z.enum( [
'doctor',
'pharmacist',
'nutritionist',
'occupational-therapist',
] )
type OccupationOptions = typeof OccupationSchema[ 'options' ]
export const FacultySchema = z.enum( [
'general-medicine',
'anesthesiology',
'anatomy',
'occupational-medicine',
'ophthalmology',
] )
type ExcludeFromTuple<T extends readonly any[], E> =
T extends [ infer F, ...infer R ] ? [ F ] extends [ E ] ? ExcludeFromTuple<R, E> :
[ F, ...ExcludeFromTuple<R, E> ] : []
const MedicOccupationDiscriminator = z.discriminatedUnion( 'occupation', [
z.object( {
occupation: z.literal( OccupationSchema.enum.doctor ),
faculty: FacultySchema,
} ),
z.object( {
occupation: z.enum(
OccupationSchema.options
.filter( x => x !== OccupationSchema.enum.doctor ) as ExcludeFromTuple<
OccupationOptions,
typeof OccupationSchema.enum.doctor
>
),
} ),
] )
console.log(
MedicOccupationDiscriminator.safeParse( {
occupation: 'doctor',
faculty: 'anesthesiology',
} ).success
) // true
console.log(
MedicOccupationDiscriminator.safeParse( {
occupation: 'pharmacist',
} ).success
) // true
console.log(
MedicOccupationDiscriminator.safeParse( {
occupation: 'foo',
} ).success
) // false |
Beta Was this translation helpful? Give feedback.
1 reply
-
Is this what you are looking for? const PersonSchema = z.object( {
firstName: z.string(),
lastName: z.string(),
} )
const MedicOccupationDiscriminator = z.discriminatedUnion( 'occupation', [
PersonSchema.extend( {
occupation: z.literal( OccupationSchema.enum.doctor ),
faculty: FacultySchema,
} ),
PersonSchema.extend( {
occupation: z.enum(
OccupationSchema.options
.filter( x => x !== OccupationSchema.enum.doctor ) as ExcludeFromTuple<
OccupationOptions,
typeof OccupationSchema.enum.doctor
>
),
} ),
] ) |
Beta Was this translation helpful? Give feedback.
1 reply
-
While working on something else I noticed const OccupationSchema = z.enum( [
'doctor',
'pharmacist',
'nutritionist',
'occupational-therapist',
] )
const FacultySchema = z.enum( [
'general-medicine',
'anesthesiology',
'anatomy',
'occupational-medicine',
'ophthalmology',
] )
const MedicOccupationDiscriminator = z.discriminatedUnion( 'occupation', [
z.object( {
occupation: OccupationSchema.extract( [ 'doctor' ] ),
faculty: FacultySchema,
} ),
z.object( {
occupation: OccupationSchema.exclude( [ 'doctor' ] )
} ),
] )
console.log(
MedicOccupationDiscriminator.safeParse( {
occupation: 'doctor', faculty: 'general-medicine'
} ).success
) // true
console.log(
MedicOccupationDiscriminator.safeParse( {
occupation: 'pharmacist'
} ).success
) // true If you found my answer satisfactory, please consider supporting me. Even a small amount is greatly appreciated. Thanks friend! 🙏 |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Let's say I have an enum with different medical occupations:
and a faculty schema:
I need to create a discreminatedUnion schema that checks if the occupation is === "doctor" to allow a faculty to be set:
How can I achieve this behaviour? Thank you!
Beta Was this translation helpful? Give feedback.
All reactions