Warm tip: This article is reproduced from serverfault.com, please click

validation-验证日期,然后将其传输到iso字符串 React

(validation - Validate dates then transfer it to iso string react)

发布于 2020-12-07 04:31:02

我的问题是我需要使用任何验证库(joi,yup,superstruct..etc)来验证日期,然后,我需要将日期对象转换为iso字符串

我使用Flatpikr,react-hook-form,joi或yup

乍看之下这是我的方法

  1. Flatpickr >>日期>>验证>>如果验证成功,则toISOString()

我想通过yup实现第一种方法,但是没有希望

我想在yup中进行前后转换

我在他们的资料库中打开了一个问题,解释了我的步骤

https://github.com/jquense/yup/issues/1153

我在joi的方法也没有成功

我用了这段代码

joi
  .date()
  .iso()
  .required()
  .min(new Date())
  .messages({
    'any.required': `Required.`,
    'date.format': `Required`,
    'date.base': `Should be a type of number`,
    'date.min': `The date should be in future`,
  });

我已经用过

{ convert : false }

在joi选项中以防止joi转换值

当将convert设置为false时,即使我的变量是必需的或为空,我所有的验证模式都将成功

Questioner
Ahmed Ahmed Sayed
Viewed
11
Ahmed Ahmed Sayed 2020-12-11 04:04:26

通过使用找到了解决方案

.raw()

和乔

参考:https ://joi.dev/api/ ?v= 17.3.0#anyrawenabled