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

Sak*_*and 4 javascript reactjs yup formik

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

例如:schema.js

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

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

小智 6

我正在这样做:

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,
});
Run Code Online (Sandbox Code Playgroud)