Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

376
Views
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") 
});
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

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

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!