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

0

96
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") 
});
5 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

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