问题描述
我正在使用 React 和 TypeScript,我需要检查我的 groupID 是否在 [2,3,4] 数组列表中。
我不确定我的validationSchema 好不好,但是这个keywrod 有问题,因为它在const 中不存在。 我的validationSchema const 是一个独立的文件。
有什么帮助吗?!
export const validationSchema = Yup.object().shape
({
text: Yup
.string()
.defined()
.max(2048,"Az üzenet szövege túl hosszú,"),groupID: Yup
.number()
.min(1),queryParam: Yup
.string()
.when("is_in_list",{
is: [2,4].includes(this.parent.groupID),then: Yup
.string()
.defined("Kötelező mező."),otherwise: Yup
.string()
.nullable()
}),isVoteType: Yup
.boolean(),positiveAnswer: Yup
.string()
.when("isVoteType",{
is: true,then: Yup.string()
.required("Kötelező mező.")
}),negativeAnswer: Yup
.string()
.when("isVoteType",then: Yup.string()
.required("Kötelező mező.")
})
});
解决方法
import * as Yup from "yup";
export const validationSchema = Yup.object().shape
({
text: Yup
.string()
.required()
.max(2048,"Az üzenet szövege túl hosszú,"),groupID: Yup
.number()
.min(1),queryParam: Yup
.string()
.when("groupID",{
is: (groupID: number) => groupID > 1,then: Yup.mixed()
.required("Kötelező mező.")
}),isVoteType: Yup
.boolean(),positiveAnswer: Yup
.string()
.when("isVoteType",{
is: (isVoteType: boolean) => isVoteType,then: Yup.string()
.required("Kötelező mező.")
}),negativeAnswer: Yup
.string()
.when("isVoteType",then: Yup.string()
.required("Kötelező mező.")
})
});