Skip to content

Instantly share code, notes, and snippets.

@Restoration
Last active December 10, 2019 05:51
Show Gist options
  • Select an option

  • Save Restoration/e1a7b4980155f63e308cad33b458d442 to your computer and use it in GitHub Desktop.

Select an option

Save Restoration/e1a7b4980155f63e308cad33b458d442 to your computer and use it in GitHub Desktop.

We’ve been using yup to validate a JavaScript form, and found ourselves facing a common problem when a user signs up for a service:

How do we ensure a user’s email matches their email confirmation?

yup’s test function helped us find a solution. It’s documented like this:

mixed.test(name: string, message: string, test: function) Text test takes a name, a message to show on failure, and a function that returns a boolean. We paired this with the email we get from our parent.

var schema = yup.object().shape({
  email: yup
    .string(),
  emailConfirmation: yup
    .string()
    .test('match', 
      'emails do not match', 
       function(emailConfirmation) { 
         return emailConfirmation === this.parent.email; 
       }),
}),
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment