question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Yup Custom Validation (addMethod) Bug

See original GitHub issue

Describe the bug Code breaks when adding custom validation logic via addMethod function.

Code returns : vendors-node_modules_apollo_client_index_js-node_modules_hookform_resolvers_yup_dist_yup_modu-7f163c.js:9521 Uncaught (in promise) TypeError: Cannot read property ‘reduce’ of undefined message.

To Reproduce Just add custom validation to yup

yup.addMethod(yup.object, 'localeEqual', function (errorMessage) {
    return this.test('locale-equal', errorMessage, function (value) {
        const {path, createError} = this
        return value.kz.length === value.ru.length || createError({path, message: errorMessage})
    })
})

CodeSandbox https://codesandbox.io/s/yup-custom-validation-error-41x1n?file=/src/App.js

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
jorisrecommented, May 11, 2021

You got that error because value could be undefined, here is the fixed part:

yup.addMethod(yup.object, "localeEqual", function (errorMessage) {
  return this.test("locale-equal", errorMessage, function (value) {
    const { path, createError } = this;
    return (
      value.en?.length === value.ru?.length || // <= Fixed line with optional chaining
      createError({ path, message: errorMessage })
    );
  });
});

This line: value.en?.length === value.ru?.length

Let me know if you still have the issue

2reactions
jorisrecommented, May 12, 2021

Thanks so much, @sanzhardanybayev that’s awesome to hear!

Read more comments on GitHub >

github_iconTop Results From Across the Web

How Does yup.addMethod() Work? Creating Custom ...
Yup's documentation is pretty vague about creating custom validation functions and the role of .addMethod() in it. This article will break it down...
Read more >
Does anyone have an example of `addMethod` in Typescript?
Context: I'm using Yup with Typescript and formik. Trying to add a custom method to enforce the format of a date value. The...
Read more >
How to write a custom schema validation using yup ...
My validation seems to work, but I find it too verbose. Trying to use the .addMethod() function of yup , but getting stuck...
Read more >
How to use the yup.addMethod function in yup - Snyk
To help you get started, we've selected a few yup.addMethod examples, based on popular ways it is used in public projects.
Read more >
Yup - npm
Dead simple Object schema validation. Latest version: 0.32.11, last published: a year ago. Start using yup in your project by running `npm i ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found