Skip to content

Commit

Permalink
docs: omitWhen and skipWhen execution
Browse files Browse the repository at this point in the history
  • Loading branch information
ealush committed Sep 30, 2023
1 parent 53ac461 commit 6bf3bbc
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,36 @@ omitWhen(
}
);
```

```js
omitWhen(
(value, values) => {
if (!values) {
return false;
}
return values.length === 1;
},
({ value, values, field }) => {
if (!values) {
return false;
}

if (values.length === 1 && values[0] === value) {
return true;
}

return false;
},
'You need at least one option',
);
```
:::caution IMPORTANT
The code within omitWhen will always run, regardless of whether the condition is met or not. `omitWhen` only affects the validation result, but if you call a function within `omitWhen`, it will run regardless of the condition.

```js
omitWhen(true, () => {
console.log('This will always run');
});
```

:::
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,14 @@ export default create((data = {}) => {
In this example, we first validate that the `username` field is not empty. If it's empty, we report an error. Next, we use `skipWhen` to skip the verification of the username on the server-side if the `username` field has an error. If the `username` field does not have an error, we perform the server-side verification using a function called `doesUserExist`, which returns a promise that resolves to `true` if the username already exists, or `false` otherwise.

Using `skipWhen` allows us to conditionally skip tests, depending on the state of the validation result. In this case, it helps us avoid unnecessary server-side requests if the `username` field is empty, or if it already has an error.

:::caution IMPORTANT
The code within skipWhen will always run, regardless of whether the condition is met or not. `skipWhen` only affects the validation result, but if you call a function within `skipWhen`, it will run regardless of the condition.

```js
skipWhen(true, () => {
console.log('This will always run');
});
```

:::

2 comments on commit 6bf3bbc

@vercel
Copy link

@vercel vercel bot commented on 6bf3bbc Sep 30, 2023

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:

vest-next – ./website

vest-next.vercel.app
vest-website.vercel.app
vest-next-ealush.vercel.app
vest-next-git-latest-ealush.vercel.app

@vercel
Copy link

@vercel vercel bot commented on 6bf3bbc Sep 30, 2023

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:

vest – ./website

vest.vercel.app
vest-ealush.vercel.app
vestjs.dev
vest-git-latest-ealush.vercel.app
www.vestjs.dev

Please sign in to comment.