jquense / yup

Dead simple Object schema validation
MIT License
22.94k stars 935 forks source link

Cyclic dependency error when using pick #2097

Closed hoffmann-david closed 1 year ago

hoffmann-david commented 1 year ago

Describe the bug

Hi I am getting a cyclic dependency error when using pick()in combination with shape() in yup 1.2.0. The problem seems similar to https://github.com/jquense/yup/issues/1969

To Reproduce

import { object, string } from 'yup';

const testObject = {
        a1: undefined,
        a2: 'over9000',
      };

      const a1 = string().when('a2', {
        is: undefined,
        then: (schema) => schema.required(),
      });
      const a2 = string().when('a1', {
        is: undefined,
        then: (schema) => schema.required(),
      });
      const schema = object({ a3: string().required(), a4: string().required() }).shape({ a1, a2 }, [
        ['a1', 'a2'],
      ]);

      const isValid = schema.pick(['a1', 'a2']).isValid(testObject);

Here is a very basic demo showing the resulting error: https://plnkr.co/edit/Nbdwma2bEbtSrxje

The resulting error is: Error: Cyclic dependency, node was:"a2"

Expected behavior The schema is able to validate properly for the picked fields.

Platform (please complete the following information):

hoffmann-david commented 1 year ago

@jquense is there a known way to work around this problem?