Yup验证检查是否为空。

23
const validationSchema = Yup.object().shape({
    newPassword: Yup.string().min(8, 'Password must be at least 8 characters');
});

如果newPassword字段不为空,我只想进行验证检查。 我该怎么做?

8个回答

24
解决这个问题有不同的方法。
使用测试
const validationSchema = Yup.object().shape({
    newPassword: Yup.string().test(
        'empty-check',
        'Password must be at least 8 characters',
         password => password.length == 0
    });

使用when

使用"when"方法不再起作用,因为它存在循环依赖关系[从评论部分复制]

const validationSchema = Yup.object().shape({
    newPassword: Yup.string().when('newPassword',{
        is:(password) => password.length > 0
        then: Yup.string().min(8, 'Password must be at least 8 characters');
    });

21
使用“when”方法不再起作用,因为它存在循环依赖。 - unlimittt
@unlimittt 也许你知道其他的选择? - pavel06081991
正确的选择是使用.test。您将在那里访问到原始值。 - undefined

13

使用 测试 的替代方案:

const validationSchema = Yup.object().shape({
    newPassword: Yup.string().test(
        'empty-or-8-characters-check',
        'Password must be at least 8 characters',
        password => !password || password.length >= 8,
    ),
});

9

使用 trim() 函数的另一种方法

const validationSchema = Yup.object().shape({
    newPassword: Yup.string().trim().required("Password must be at least 8 characters"),
  });

7
我解决这个问题的方法是先添加`.nullable()`,然后将空字符串转换为null,使用`.transform()`实现。
const validationSchema = Yup.object().shape({
    newPassword: Yup
        .string()
        .nullable()
        .transform((v, o) => (o === '' ? null : v))
        .min(8, 'Password must be at least 8 characters')
});

4

使用.when()的一种方法,不会像被接受的答案那样产生循环依赖。 .shape()接受一个详尽的依赖列表作为最后一个参数,以解决循环冲突问题,秘诀是使用相同的key两次 https://github.com/jquense/yup/issues/176#issuecomment-369925782

const validationSchema = Yup.object().shape({
    newPassword: Yup.string().test(
        'empty-or-8-characters-check',
        'Password must be at least 8 characters',
        password => !password || password.length >= 8,
    ),
}, [["newPassword","newPassword"]]);

感谢您提供这个答案并链接到相关问题。除了列出参数存在的文档之外,似乎没有关于noSortEdges的任何内容:https://github.com/jquense/yup#objectshapefields-object-nosortedges-arraystring-string-schema 看起来这里有一个关于文档的未解决问题:https://github.com/jquense/yup/issues/1339 - CTS_AE
必须接受此答案。 - Klem Lloyd Mwenya

1
最简单的方法是:
const validationSchema = Yup.object().shape({
    newPassword: Yup
     .string()
     .matches(/^\w{8}/, "Password must be at least 8 characters")
});

1

不允许有8个空字符的替代方案。

const notEmpty = Yup.string()
  .ensure() // Transforms undefined and null values to an empty string.
  .test('Only Empty?', 'Cannot be only empty characters', (value) => {
    const isValid = value.split(' ').join('').length !== 0;

    return isValid;
  });

const validationSchema = Yup.object({
    newPassword: notEmpty.min(8, 'Password must be at least 8 characters');
});

0

我使用了Yup函数nullable(),因此只有在它不为空时才会通过下一个函数进行验证:

kmBegin: Yup.number().nullable().positive().integer(),
parcelTotal: Yup.number().positive().integer(),
timeBegin: Yup.date(),
timeEnd: Yup.date().nullable(),

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接