Skip to content

Commit 990cfb3

Browse files
committed
Improve example of rawCheckAsync API reference
1 parent 8802fa6 commit 990cfb3

File tree

1 file changed

+29
-31
lines changed
  • website/src/routes/api/(async)/rawCheckAsync

1 file changed

+29
-31
lines changed

website/src/routes/api/(async)/rawCheckAsync/index.mdx

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -40,48 +40,46 @@ The following examples show how `rawCheckAsync` can be used.
4040

4141
### Add users schema
4242

43-
Object schema that ensures only the users that are not already in the group are added.
43+
Object schema that ensures that only users not already in the group are included.
4444

4545
> This `rawCheckAsync` validation action adds an issue for any invalid username and forwards it via `path` to the appropriate nested field.
4646
4747
```ts
48-
import { isNewGroupUser } from '~/api';
48+
import { isAlreadyInGroup } from '~/api';
4949

5050
const AddUsersSchema = v.pipeAsync(
5151
v.object({
5252
groupId: v.pipe(v.string(), v.uuid()),
5353
usernames: v.array(v.pipe(v.string(), v.nonEmpty())),
5454
}),
5555
v.rawCheckAsync(async ({ dataset, addIssue }) => {
56-
if (!dataset.typed) {
57-
return;
58-
}
59-
const { groupId, usernames } = dataset.value;
60-
for (let index = 0; index < usernames.length; index++) {
61-
const username = usernames[index];
62-
if (await isNewGroupUser(username, groupId)) {
63-
continue;
64-
}
65-
addIssue({
66-
received: username,
67-
message: 'The user is already in the group.',
68-
path: [
69-
{
70-
type: 'object',
71-
origin: 'value',
72-
input: dataset.value,
73-
key: 'usernames',
74-
value: usernames,
75-
},
76-
{
77-
type: 'array',
78-
origin: 'value',
79-
input: usernames,
80-
key: index,
81-
value: username,
82-
},
83-
],
84-
});
56+
if (dataset.typed) {
57+
await Promise.all(
58+
dataset.value.usernames.map(async (username, index) => {
59+
if (await isAlreadyInGroup(username, dataset.value.groupId)) {
60+
addIssue({
61+
received: username,
62+
message: 'The user is already in the group.',
63+
path: [
64+
{
65+
type: 'object',
66+
origin: 'value',
67+
input: dataset.value,
68+
key: 'usernames',
69+
value: dataset.value.usernames,
70+
},
71+
{
72+
type: 'array',
73+
origin: 'value',
74+
input: dataset.value.usernames,
75+
key: index,
76+
value: username,
77+
},
78+
],
79+
});
80+
}
81+
})
82+
);
8583
}
8684
})
8785
);

0 commit comments

Comments
 (0)