当字段匹配条件时,对一系列对象验证的YUP验证
我想对此数据形状进行YUP验证:
{
"values": [
{
"id": "Card ID",
"checkBox": true,
"csvField": ""
},
{
"id": "title",
"checkBox": false,
"csvField": ""
},
{
"id": "subtitle",
"checkBox": false,
"csvField": ""
},
{
"id": "phase",
"checkBox": false,
"csvField": "",
"fields": [
{
"id": "61d5bc37b995d200116bc73c",
"phaseInCsv": ""
},
{
"id": "61d5c43759bd07001134076a",
"phaseInCsv": ""
},
{
"id": "6203f08dc0eb7000120052a1",
"phaseInCsv": ""
}
]
}
]
}
因此,在ID = apeas时,该字段数组的验证应发生 我这样做了,但没有起作用!
任何建议!
这是我的验证,但行不通:
const validationSchema = Yup.object().shape({
values: Yup.array().of(
Yup.object().shape({
id: Yup.string(),
checkBox: Yup.boolean(),
csvField: Yup.string().when('checkBox', {
is: true,
then: Yup.string().required(
t('IMPORT.VALIDATION_ERROR.CSV_FIELD_MISSING')
),
}),
fields: Yup.array().when('id', {
is: FormFieldType.PHASE,
then: Yup.array()
.of(
Yup.object().shape({
id: Yup.string(),
phaseInCsv: Yup.string().required(
t('IMPORT.VALIDATION_ERROR.CSV_FIELD_MISSING')
),
})
)
.required(),
}),
})
),
})
i want to have yup validation for this shape of data :
{
"values": [
{
"id": "Card ID",
"checkBox": true,
"csvField": ""
},
{
"id": "title",
"checkBox": false,
"csvField": ""
},
{
"id": "subtitle",
"checkBox": false,
"csvField": ""
},
{
"id": "phase",
"checkBox": false,
"csvField": "",
"fields": [
{
"id": "61d5bc37b995d200116bc73c",
"phaseInCsv": ""
},
{
"id": "61d5c43759bd07001134076a",
"phaseInCsv": ""
},
{
"id": "6203f08dc0eb7000120052a1",
"phaseInCsv": ""
}
]
}
]
}
so the validation of the fields array should happen only once the id = phase
i did it this way but didn't work !
any suggestions guys !
this is my validation but it doesn't work :
const validationSchema = Yup.object().shape({
values: Yup.array().of(
Yup.object().shape({
id: Yup.string(),
checkBox: Yup.boolean(),
csvField: Yup.string().when('checkBox', {
is: true,
then: Yup.string().required(
t('IMPORT.VALIDATION_ERROR.CSV_FIELD_MISSING')
),
}),
fields: Yup.array().when('id', {
is: FormFieldType.PHASE,
then: Yup.array()
.of(
Yup.object().shape({
id: Yup.string(),
phaseInCsv: Yup.string().required(
t('IMPORT.VALIDATION_ERROR.CSV_FIELD_MISSING')
),
})
)
.required(),
}),
})
),
})
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论