reactjs - schedules[0].timings[0].startTime 必须是 `string` 类型,但最终值为:`null` - 当字段为空时获取此值
问题描述
常量验证模式 = () => {
return Yup.lazy(values => {
return Yup.object().shape({
name: Yup.string().required("This field is required"),
streetAddress: Yup.string().required("This field is required"),
city: Yup.string().required("This field is required"),
state: Yup.string().required("This field is required"),
zipCode: Yup.string().required("This field is required"),
country: Yup.string().required("This field is required"),
phone: Yup.string().required("This field is required"),
// consultingCharge: Yup.string().required("This field is required"),
schedules: Yup.array().of(
Yup.object().shape({
available: Yup.boolean(),
timings: Yup.array().of(
Yup.object().shape({
startTime: Yup
.string()
.when('available', {
is: true,
then: Yup.string().required('Field is required'),
otherwise: Yup.string()
}),
endTime: Yup
.string()
.when('available', {
is: true,
then: Yup.string().required('Field is required'),
otherwise: Yup.string()
})
.test("", "End time must be after Start time" , function(values){
return this.parent.startTime < values;
}),
})
)
})
)
})
})
}
面对以下错误: schedules[0].timings[0].endTime 必须是一种string
类型,但最终值为:null
。如果“null”旨在作为空值,请务必将架构标记为.nullable()
何时将开始时间和结束时间字段留空。
解决方案
我不确定这是否与架构有关,我可以想象您将它与 formik 或其他表单库结合使用?看起来您正在针对该架构进行验证的状态正在null
为 endTime 属性提供一个值。你能验证它不是状态对象本身的问题吗?可能是您有一个为空的输入字段并且 onChange 处理程序将值从状态设置为null
而不是""
?:)
推荐阅读
- python - 如何使用网格环境创建一个 openAI 健身房观察空间
- docusignapi - 发送文档时的 ENVELOPE_HAS_DUPLICATE_RECIPIENTS
- paw-app - Paw - How do I disable Content-Type header?
- angularjs - Angular Js + Youtube 嵌入没有使用 ng-sanitize 与 ng-bing-html 一起渲染,如何解决?
- jquery - jQuery 函数返回 NaN
- django - Django - 登录后,将用户重定向到他/她所在的页面
- javascript - 今天日期和接下来的 2 天不显示周末 - 显示在选择选项中
- java - android-studio方法中的错误
- python - 格式化程序黑色不适用于我的 VSCode ......但为什么?
- c# - MS 甘特图 2 轴