是的,在独立常量中使用此验证

问题描述

我正在使用 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ő.")
                      })
});

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...