如何在YUP验证中最多一天的一天结束日期?

发布于 2025-01-17 22:31:15 字数 625 浏览 2 评论 0原文

目前,我有一个 yup 模式来确保 end_time 至少在 start_time 之后,

    start_time: yup
        .date()
        .min(new Date(), 'Start datetime cannot be in the past')
        .required('Start datetime is required'),
    end_time: yup
        .date()
        .min(yup.ref('start_time'), 'End datetime must be after start datetime')
        .required('End datetime is required'),

我想让 end_time 最多在 start_time 之后 1 天/24 小时。 例如,如果 start_time 为 "Mon Apr 11 2022 18:30:00",我希望 end_time 最多为 "Tue Apr 12 2022 18:30:00"代码>. 我会考虑使用 .when().max(),但我不确定如何开始的格式。

Currently, I have a yup schema to ensure that the end_time is at least after the start_time

    start_time: yup
        .date()
        .min(new Date(), 'Start datetime cannot be in the past')
        .required('Start datetime is required'),
    end_time: yup
        .date()
        .min(yup.ref('start_time'), 'End datetime must be after start datetime')
        .required('End datetime is required'),

I want to make it so that the end_time is at most 1day/24hrs after the start_time.
For example, if the start_time is "Mon Apr 11 2022 18:30:00", I want the end_time to be at most "Tue Apr 12 2022 18:30:00".
I would think of using .when() or .max(), but I am not sure of the format of how to begin.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

唯憾梦倾城 2025-01-24 22:31:15

对于遇到类似问题的任何人,我已经设法使用 Schema 对象上的 .when() 方法解决了该问题

    start_time: yup
        .date()
        .min(new Date(), 'Start datetime cannot be in the past')
        .required('Start datetime is required'),
    end_time: yup
        .date()
        .when('start_time', (start_time, schema) => {
            if (start_time) {
                const currentDay = new Date(start_time.getTime());
                const nextDay = new Date(start_time.getTime() + 86400000);
                return schema
                    .min(currentDay, 'End time must be after start time')
                    .max(nextDay, 'End time cannot be more than 24 hours after start time');
            } else {
                return schema;
            }
        })
        .required('End datetime is required'),

For anyone that runs into a similar issue, I have managed to solve the issue using the .when() method on the Schema object

    start_time: yup
        .date()
        .min(new Date(), 'Start datetime cannot be in the past')
        .required('Start datetime is required'),
    end_time: yup
        .date()
        .when('start_time', (start_time, schema) => {
            if (start_time) {
                const currentDay = new Date(start_time.getTime());
                const nextDay = new Date(start_time.getTime() + 86400000);
                return schema
                    .min(currentDay, 'End time must be after start time')
                    .max(nextDay, 'End time cannot be more than 24 hours after start time');
            } else {
                return schema;
            }
        })
        .required('End datetime is required'),
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文