Home > Software engineering >  How to strip an array if array length is 0 in yup
How to strip an array if array length is 0 in yup

Time:03-23

I want to strip the "other" array if its length is 0 Here is my schema

languages: Yup.object({
  native: Yup.string().oneOf(languages),
  other: Yup.array()
    .max(5)
    .of(
      Yup.object({
        language: Yup.string().oneOf(languages),
        speaking: Yup.string().oneOf(fluency),
        reading: Yup.string().oneOf(fluency),
        writing: Yup.string().oneOf(fluency),
      })
    )
    .when("other.length", {
      is: 0,
      then: (s) => s.strip(),
    }),
}),

The error I get:

Uncaught Error: Cyclic dependency, node was: "other"

Thank beforehand

CodePudding user response:

For someone who may encounter the same issue:

I found the issue in:

.when(".length", {
  is: 0,
  then: (s) => s.strip(),

I needed to reference the .length of the array rather than the arr.length and this resolved the problem.

  • Related