I have a complex property in one of my data sctructures.
enum BudgetPlanScheduleType {
Periodical,
FixedDuration
}
interface FormValues {
name: string;
schedule: BudgetPlanPeriodicalSchedule | BudgetPlanFixedDurationSchedule;
enableOverrunAlert: boolean;
}
interface BudgetPlanSchedule {
type: BudgetPlanScheduleType;
}
interface BudgetPlanFixedDurationSchedule extends BudgetPlanSchedule {
startOn: Date;
endOn: Date;
}
interface BudgetPlanPeriodicalSchedule extends BudgetPlanSchedule {
periodType: BudgetPlanPeriodType;
periodSpecific: boolean;
periodAmounts: {
value: number;
enabled: boolean;
}[];
}
The schedule property can either be one of two types, and depending on the type, the data structure will change. If the type is periodical, it should use the BudgetPlanPeriodicalSchedule structure and if the type is fixed duration, it should use the BudgetPlanFixedDurationSchedule
they each have different properties and so need validation depending on the type.
How can I do this using YUP in react? I cant seem to find a way to conditionally use interfaces/schemas for a property
Any help is much appreciated, thank you