简体   繁体   中英

Error during YUP validation “Cannot read property 'length' of undefined”

I got the following code block:

 const schema = useMemo(
    () =>
      yup.object().shape({
        name: yup
          .string()
          .trim()
          .required("Missing name")
          .max(40, "Too long"),
        template: yup
          .string()
          .trim()
          .max(2000, "Too long")
          .matches(/^https?:\/\//, "Invalid protocol")
          .required("Missing template")
          .test("variablesUsage", "Missing vars", function (
            value
          ) {
            return vars.some(v => value.includes(`{${v}}`))
          }),
        enabled: yup.boolean(),
      }),
    [message, vars]
  )

The problem is that everytime i type something in "name" field, I get the following error in console: "Uncaught (in promise) TypeError: Cannot read property 'length' of undefined"

It works fine if I remove the.test from "template", however I could not find the root cause of this error.

Basically, in.test, there are some specific keywords, at least 1 of these keywords must be present in "template"

It seems like the error was caused by this line

return vars.some(v => value.includes(`{${v}}`))

I have added the following line:

if (value == null) {return false;}

And it is working.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM