Search code examples
yup

How to customize yup validation messages?


Most answers I have seen customize yup validation messages when defining a schema, e.g.

const personSchema = yup.object().shape({
    firstName: yup.string().required('First name is a required field'),
    lastName: yup.string().required('Last name is a required field'),
});

But what if I want to customize the default validation message itself? For example, required fields should always render a fixed string "Required field".


Solution

  • You could do it by using setLocale function from yup like this:

      import * as Yup from 'yup';
    
      Yup.setLocale({
        mixed: {
          required: 'Required Field',
        },
      });
    

    For more information check the documentation: https://github.com/jquense/yup

    The section Using a custom locale dictionary is what you need.