我使用Yup验证表单。我有来自API的动态对象值。所以我尝试了下面的Yup示意图。但是这个错误返回:
图像形成误差
如果你知道解决办法,有人能帮你吗?
我的代码是:
const ActivitySchema = yup.object().shape({
Activity: yup.lazy((value) => {
if (value) {
const ValidationObject = {
ActivityName: yup.string().required(''),
Answers: yup.lazy((val) => {
if (val) {
const ValidObject = {
LanguageName: yup.string().required(''),
Answer: yup.string().required('').max(maxChar, ''),
}
const NewEnt = Object.keys(val)?.reduce((acc, key) => ({
...acc,
[key]: yup.object().shape(ValidObject)
}))
return yup.object().shape(NewEnt)
}
return yup.mixed('').notRequired('')
})
}
const NewEntries = Object.keys(value)?.reduce((acc, key) => ({
...acc,
[key]: yup.object().shape(ValidationObject)
}))
return yup.object().shape(NewEntries)
}
return yup.mixed('').notRequired('')
}),
ActivityFiles: yup.array().min(1, ''),
})
const Activities= {
"Activity": {
"042ab05d-eff7-420d-84bd-be7535da0f7e": {
"ActivityName": "ActivityName",
"Answers": {
"en": {
"LanguageName": "English",
"Answer": ""
},
"fr": {
"LanguageName": "French",
"Answer": ""
}
}
},
"29102f46-f2b3-4050-87a4-92eaf31ddadb": {
"ActivityName": "ActivityName...",
"Answers": {
"en": {
"LanguageName": "English",
"Answer": ""
},
"fr": {
"LanguageName": "French",
"Answer": ""
}
}
},
"ActivityFiles": null
}
ActivitySchema.isValid(Activities).then((valid) => {
if (valid) {
console.log("validation ok.")
} else {
console.log("validation is not ok.")
}
})
我找遍了,但只找到了这个答案。:. required(不使用),. required()(按如下方式使用)
但我已经这样承诺了。
1条答案
按热度按时间vbopmzt11#
我解决了这个问题。