我在我的表单(Formik)中有一个字段,其中包含一个由每个对象两个时间戳组成的数组。 我的验证模式现在适用于数组中的所有元素,但目标是仅检查数组中的第一个和最后一个对象,所有其他对象都可以为空。 数组的长度会动态变化。 我该怎么办?
"timeslots": [
{
"startTime": "2021-10-31T22:30:00.000Z",
"endTime": "2021-11-01T00:30:00.000Z"
},
{
"startTime": "",
"endTime": ""
},
{
"startTime": "2021-11-02T22:30:00.000Z",
"endTime": "2021-11-03T00:00:00.000Z"
}]
只有所有对象都填满了时间戳,这个才能工作:
validationSchema={() =>
Yup.lazy(({ timeslots }) =>
? Yup.object({
timeslots:
timeslots.length > 0 &&
Yup.array().of(
Yup.object().shape({
startTime: Yup.string().required(INVALID_FORM_MESSAGE.requiredField),
endTime: Yup.string().required(INVALID_FORM_MESSAGE.requiredField),
}),
),