@@ -40,48 +40,46 @@ The following examples show how `rawCheckAsync` can be used.
40
40
41
41
### Add users schema
42
42
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 .
44
44
45
45
> This ` rawCheckAsync ` validation action adds an issue for any invalid username and forwards it via ` path ` to the appropriate nested field.
46
46
47
47
``` ts
48
- import { isNewGroupUser } from ' ~/api' ;
48
+ import { isAlreadyInGroup } from ' ~/api' ;
49
49
50
50
const AddUsersSchema = v .pipeAsync (
51
51
v .object ({
52
52
groupId: v .pipe (v .string (), v .uuid ()),
53
53
usernames: v .array (v .pipe (v .string (), v .nonEmpty ())),
54
54
}),
55
55
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
+ );
85
83
}
86
84
})
87
85
);
0 commit comments