如何重用 Yup 验证模式 + 声纳扫描在 yup 模式验证文件上显示更多重复,它增加了重复覆盖率

问题描述

我正在处理多种表格。在某些情况下,表单具有类似的字段,例如姓名、电话号码、帐号等。我也在使用 formik 。我为每个表单创建了架构文件 (schema.js)。在里面,我列出了所有架构验证并返回该架构。

例如:schema.js

const schema = yup.object().shape({
 firmName: yup.string().label(Labels.FIRM).max(255)
});
export default schema;

如果我有 10 个表单,其中包含 FirmName 字段,那么我所有的 schema.js 文件都在属性 firmName 之上。 我不知道如何分离它并重新使用它。 如果有人遇到并解决了类似的情况,请告诉我方法。 上述编码方式增加了声纳扫描中的重复百分比。请帮忙

解决方法

我是这样做的:

const password = {
  password: yup.string().required().min(8),};
const setupPassword = {
  ...password,confirmPassword: yup
    .string()
    .defined(t('password.confirm.required'))
    .test('passwords-match',t('password.confirm.match'),function (value) {
      return this.parent.password === value;
    }),agreeToTerms: yup.boolean().test('is-true',value => value === true),};

export const SchemaPasswordValidation = yup.object().shape({
  ...setupPassword,});

相关问答

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