在你的情况下第一个参数.when()应该是一个键数组:
StartIntensity: yup.number(),
EndIntensity: yup
.number()
.when(
["StartIntensity", "EndIntensity"],
(StartIntensity: number, EndIntensity: number, schema: any) => {
return !!StartIntensity && StartIntensity !== EndIntensity
? schema.moreThan(
StartIntensity,
"Max should be > min"
)
: schema;
}
),
键:字符串 |大批
https://github.com/jquense/yup#mixedwhenkeys-string--arraystring-builder-object--value-schema-schema-schema https://github.com/jquense/yup#mixedwhenkeys-string--arraystring-builder-object--value-schema-schema-schema
如果上面的代码抛出“循环依赖”错误(我认为它会),请尝试以下代码:
StartIntensity: yup.number(),
EndIntensity: yup
.number()
.when('StartIntensity', (StartIntensity, schema) => {
return schema.test({
test: EndIntensity => !!StartIntensity && EndIntensity > StartIntensity,
message: "Max should be > min"
})
}),
或者您也可以使用ref:
StartIntensity: yup.number(),
EndIntensity: yup
.number()
.moreThan(yup.ref('StartIntensity'), "Max should be > min")