如何在Yup.array中测试值的唯一性? [英] How to test for uniqueness of value in Yup.array?
本文介绍了如何在Yup.array中测试值的唯一性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的表单具有动态输入(管理员电子邮件),但是检查唯一性失败:
I have form with dynamic amount of inputs (admin email) however checking for uniqueness fails:
validationSchema={Yup.object().shape({
adminEmails: Yup.array()
.of(
Yup.string()
.notOneOf(Yup.ref('adminEmails'), 'E-mail is already used')
这里最好的方法是什么?
仅供参考,我使用Formik
作为表单助手.
What is best approach here?
FYI, as a form helper I use Formik
.
推荐答案
尝试一下:
Yup.addMethod(Yup.array, 'unique', function(message, mapper = a => a) {
return this.test('unique', message, function(list) {
return list.length === new Set(list.map(mapper)).size;
});
});
然后像这样使用它:
const headersSchema = Yup.object().shape({
adminEmails: Yup.array().of(
Yup.string()
)
.unique('email must be unique')
})
这篇关于如何在Yup.array中测试值的唯一性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文