Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: provide current values as context to yup validation by default #3786

Merged
merged 1 commit into from
May 26, 2023

Commits on May 26, 2023

  1. feat: provide current values as context to yup validation by default

    Yup by default only allows for cross-field validation within the
    same field object. This is not that useful in most scenarios because
    a sufficiently-complex form will have several `yup.object()` in the
    schema.
    
    ```ts
    const deepNestedSchema = Yup.object({
      object: Yup.object({
        nestedField: Yup.number().required(),
      }),
      object2: Yup.object({
        // this doesn't work because `object.nestedField` is outside of `object2`
        nestedFieldWithRef: Yup.number().min(0).max(Yup.ref('object.nestedField')),
      }),
    });
    ```
    
    However, Yup offers something called `context` which can operate across
    the entire schema when using a $ prefix:
    
    ```ts
    const deepNestedSchema = Yup.object({
      object: Yup.object({
        nestedField: Yup.number().required(),
      }),
      object2: Yup.object({
        // this works because of the "context" feature, enabled by $ prefix
        nestedFieldWithRef: Yup.number().min(0).max(Yup.ref('$object.nestedField')),
      }),
    });
    ```
    
    With this change, you may now validate against any field in the entire schema,
    regardless of position when using the $ prefix.
    quantizor committed May 26, 2023
    Configuration menu
    Copy the full SHA
    aed0b24 View commit details
    Browse the repository at this point in the history