我有两个对象:发送国家和接收国家。这两个对象都有其他字段,但我只想验证其中一个。我想检查接收国家是否与接收国家相同,如果是,则返回错误。我尝试了以下模式,但它不起作用。我错过什么了吗?
const validationSchema = object({
sending_country: object({
name: string().ensure().required().max(100, "Name too long")
}),
receiving_country: object({
name: string().ensure().required().max(100, "Name too long").when(
"$sending_country.name", (sending_country, schema) => {
return schema.test({
test: receiving_country => receiving_country.name !== sending_country.name,
message: "Both countries cannot be identical"
})
})
})
})
我最终混合了一些溶液
const validationSchema = object({
sending_country: object({
name: string().ensure().required().max(100, "Name too long")
}),
receiving_country: object().shape({
name: string().ensure().required().max(100, "Name too long")
}).when(
"sending_country", (sending_country, schema) => {
return schema.test({
test: receiving_country => receiving_country.name !== sending_country.name,
message: "Both countries cannot be identical"
})
})
})