• Empleos
  • Bootcamp
  • Acerca de nosotros
  • Para profesionales
    • Inicio
    • Empleos
    • Cursos y retos
    • Preguntas
    • Profesores
    • Bootcamp
  • Para empresas
    • Inicio
    • Nuestro proceso
    • Precios
    • Pruebas
    • Nómina
    • Blog
    • Comercial
    • Calculadora de salario

0

99
Vistas
How to return error message in Yup require method using function instead of just passing the string?

I want to add some expressions before throwing error message.

Below is the example code that I want to implement.

const schema = Yup.object().shape({
    name: yup.string().required(() => "error message") 
});

Instead of just passing a string like this

const schema = Yup.object().shape({
    name: yup.string().required("error message") 
});
7 months ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Should be able to pass in a function using Yup test method. You just have to be sure to createError and return that to yup once created.

const schema = Yup.object().shape({
  name: Yup.string()
    .string()
    .required()
    .test("name validation fn", "Name Validation Error", function (value) { // Use function
     const customError = myCustomErrorFn(value);
     return this.createError({message: customError });
    })
});

see Yup validation docs

7 months ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar empleo Precios Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2023 PeakU Inc. All Rights Reserved.