最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

reactjs - Yup field inheritance and conditional schemas - Stack Overflow

programmeradmin2浏览0评论

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

发布评论

评论列表(0)

  1. 暂无评论