-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: provide current values as context to yup validation by default (#…
…3786) 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.
- Loading branch information
Showing
3 changed files
with
76 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
--- | ||
'formik': minor | ||
--- | ||
|
||
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39a7bf7
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
formik-docs – ./website
formik-docs-jared.vercel.app
formik-docs.vercel.app
www.formik.org
formik-docs-git-master-jared.vercel.app
formik.org