是的,根据另一个 object 的 child 验证 object 的 child
Yup validate child of an object against a child of another object
我有两个objects:发送国和接收国。这两个 objects 中的每一个都有其他字段,但我只想验证其中一个。我想检查接收国是否与接收国相同,如果是,return 错误。我尝试了以下架构,但它不起作用。我错过了什么吗?
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"
})
})
})
我有两个objects:发送国和接收国。这两个 objects 中的每一个都有其他字段,但我只想验证其中一个。我想检查接收国是否与接收国相同,如果是,return 错误。我尝试了以下架构,但它不起作用。我错过了什么吗?
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"
})
})
})