我目前有一个如下所示的对象:
const initialValues = {
created: {
position: 1,
name: 'created',
type: 'timestamp',
desc: 'The date and time the lead is created',
mapping: {
name: '',
defaultValue: '',
map: false
}
}
}
我希望当映射对象中的映射值设置为 true 时,映射对象中的名称成为必需的。我已尝试通过执行以下操作:
const validationSchema = yup.object({
created: yup.object().when('mapping.map', {
is: true,
then: yup.object({
mapping: yup.object({
name: yup.string().required('name is required')
})
})
})
})
我相信我没有足够的隧道来准确设置映射对象的验证,任何和所有帮助/建议将不胜感激。
最佳答案
我找到了执行以下操作的解决方案:
const validationSchema = yup.object({
created: yup.object().shape({
mapping: yup.object().shape({
map: yup.boolean(),
defaultValue: yup.string(),
name: yup.string().when('map', {
is: true,
then: yup.string().required('name is required')
})
})
})
})
关于javascript - 是的,基于嵌套对象内的嵌套对象进行验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72478522/