Skip to content

Instantly share code, notes, and snippets.

@pipopotamasu
Created August 6, 2020 01:54
Show Gist options
  • Save pipopotamasu/b0eb126c711176f84316ccf052bbb213 to your computer and use it in GitHub Desktop.
Save pipopotamasu/b0eb126c711176f84316ccf052bbb213 to your computer and use it in GitHub Desktop.
function validateDuplicatedEmail(email) {
const { users } = this.from[1].value;
if (users.length < 2) return true;
let dupCount = 0;
for (let i = 0; i < users.length; i += 1) {
if (users[i].email === email) {
dupCount += 1;
if (dupCount > 1) {
return false;
}
}
}
return true;
}
const schema = yup.object().shape({
users: yup.array(
yup.object().shape({
name: yup.string().required('name is required.'),
email: yup
.string()
.required('email is required.')
.email('invalid email type.')
.test('email-dup', 'duplicated email', validateDuplicatedEmail)
}),
),
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment