kristianmandrup / schema-to-yup

Schema to Yup validation

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Multiple when causing Cyclic errors

tkamstra opened this issue · comments

Using the following JSON:

{
  "type": "object",
  "properties": {
    "field_cbf4ae9f-5558-0426-903b-7bd65cee1f51": {
      "label": "tb1",
      "type": "string",
      "format": "text",
      "nullable": true,
      "when": {
        "field_03e00e8c-c507-6663-7475-c5d724fa032f": {
          "is": "",
          "then": {
            "required": true
          },
          "otherwise": {
            "required": false,
            "nullable": true,
            "format": "string"
          }
        }
      },
      "required": false
    },
    "field_7d1e6118-f63b-c852-6a99-f3d68568d283": {
      "label": "tb2",
      "type": "string",
      "format": "text",
      "nullable": true,
      "required": false
    },
    "field_03e00e8c-c507-6663-7475-c5d724fa032f": {
      "label": "tb3",
      "type": "string",
      "format": "text",
      "nullable": true,
      "when": {
        "field_cbf4ae9f-5558-0426-903b-7bd65cee1f51": {
          "is": "",
          "then": {
            "required": true
          },
          "otherwise": {
            "required": false,
            "nullable": true,
            "format": "string"
          }
        }
      },
      "required": false
    }
  }
}

and the following config

{
    "errMessages": {
        "field_cbf4ae9f-5558-0426-903b-7bd65cee1f51": {
            "required": "Required",
        },
        "field_7d1e6118-f63b-c852-6a99-f3d68568d283": {
            "required": "Required",
        },
        "field_03e00e8c-c507-6663-7475-c5d724fa032f": {
            "required": "Required",
        }
    },
    "dependenciesMap": {
        "root": [
            [
                "field_cbf4ae9f-5558-0426-903b-7bd65cee1f51",
                "field_7d1e6118-f63b-c852-6a99-f3d68568d283",
                "field_03e00e8c-c507-6663-7475-c5d724fa032f"
            ]
        ]
    }
}

if you call buildYup, it produces the following error:
image
I am hoping you can some insights as different dependenciesMap arrays don't appear to make a difference

Interesting. Yeah, this library still needs a lot of work. Been working to fix some issues that were caused with the upgrade to support Yup 1.x

#166

The conditional support was always intended to be very basic and experimental in nature. If you want it bad enough I'm sure you could clone the repo and improve the current limited infra.

for reference:
Here is a working yup version:

yup.object().shape(
    {
        'field_0728af794bb010603767af9f0119155c': yup
            .string()
            .required()
            .when(['field_6a13b1f916a9d533a6a08ac294dcdc52', 'field_79dcda50087a73c0e541521d251667ff'], {
                is: (field_6a13b1f916a9d533a6a08ac294dcdc52: string, field_79dcda50087a73c0e541521d251667ff: string) =>
                    field_6a13b1f916a9d533a6a08ac294dcdc52 !== '' && field_79dcda50087a73c0e541521d251667ff != '',
                then: (schema) => schema.notRequired(),
                otherwise: (schema) => schema.required(),
            }),
        'field_79dcda50087a73c0e541521d251667ff': yup
            .string()
            .required()
            .when('field_0728af794bb010603767af9f0119155c', {
                is: (val: string) => val !== '',
                then: (schema) => schema.notRequired(),
                otherwise: (schema) => schema.required(),
            }),
    },
    [
        ['field_0728af794bb010603767af9f0119155c', 'field_6a13b1f916a9d533a6a08ac294dcdc52'],
        ['field_0728af794bb010603767af9f0119155c', 'field_79dcda50087a73c0e541521d251667ff'],
    ]
)

Very cool 👍 Should not be too hard to add support for that

I was able to get another version working by fixing my generated nosortedges array, to not have all fields in a single array, but match the layout as seen above! Closing the issue, as it appears to be a user error!