Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
88
rated 0 times [  92] [ 4]  / answers: 1 / hits: 22309  / 5 Years ago, tue, january 8, 2019, 12:00:00

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')


What is best approach here?
FYI, as a form helper I use Formik.


More From » validation

 Answers
21

Try this:



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;
});
});


Then use it like this:



const headersSchema = Yup.object().shape({
adminEmails: Yup.array().of(
Yup.string()
)
.unique('email must be unique')
})

[#52810] Thursday, January 3, 2019, 6 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ryderalfonsos

Total Points: 655
Total Questions: 88
Total Answers: 91

Location: Nauru
Member since Thu, Feb 2, 2023
1 Year ago
ryderalfonsos questions
Mon, Sep 9, 19, 00:00, 5 Years ago
Wed, Feb 13, 19, 00:00, 5 Years ago
Tue, Feb 12, 19, 00:00, 5 Years ago
Fri, Dec 28, 18, 00:00, 6 Years ago
;