-
Notifications
You must be signed in to change notification settings - Fork 617
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
76ed2a8
commit 4dddcea
Showing
42 changed files
with
730 additions
and
659 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
55 changes: 28 additions & 27 deletions
55
packages/api-admin-users/src/createAdminUsers/users.validation.ts
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 |
---|---|---|
@@ -1,46 +1,47 @@ | ||
// @ts-expect-error Package @commodo/fields does not have types. | ||
import { string, withFields } from "@commodo/fields"; | ||
// @ts-expect-error Package commodo-fields-object does not have types. | ||
import { object } from "commodo-fields-object"; | ||
import { validation } from "@webiny/validation"; | ||
import zod from "zod"; | ||
import { AdminUsers } from "~/types"; | ||
import { createZodError } from "@webiny/utils"; | ||
|
||
const CreateUserDataModel = withFields({ | ||
id: string({ validation: validation.create("minLength:1") }), | ||
displayName: string({ validation: validation.create("minLength:1") }), | ||
|
||
const createUserDataValidation = zod.object({ | ||
id: zod.string().min(1).optional(), | ||
displayName: zod.string().min(1).optional(), | ||
// We did not use an e-mail validator here, just because external | ||
// IdPs (Okta, Auth0) do not require e-mail to be present. When creating | ||
// admin users, they're actually passing the user's ID as the e-mail. | ||
// For example: packages/api-security-okta/src/createAdminUsersHooks.ts:13 | ||
// In the future, we might want to rename this field to `idpId` or similar. | ||
email: string({ validation: validation.create("required") }), | ||
|
||
firstName: string({ validation: validation.create("minLength:1") }), | ||
lastName: string({ validation: validation.create("minLength:1") }), | ||
avatar: object() | ||
})(); | ||
email: zod.string(), | ||
firstName: zod.string().min(1).optional(), | ||
lastName: zod.string().min(1).optional(), | ||
avatar: zod.object({}).passthrough() | ||
}); | ||
|
||
const UpdateUserDataModel = withFields({ | ||
displayName: string({ validation: validation.create("minLength:1") }), | ||
avatar: object(), | ||
firstName: string({ validation: validation.create("minLength:1") }), | ||
lastName: string({ validation: validation.create("minLength:1") }), | ||
group: string(), | ||
team: string() | ||
})(); | ||
const updateUserDataValidation = zod.object({ | ||
displayName: zod.string().min(1).optional(), | ||
avatar: zod.object({}).passthrough().optional(), | ||
firstName: zod.string().min(1).optional(), | ||
lastName: zod.string().min(1).optional(), | ||
group: zod.string().optional(), | ||
team: zod.string().optional() | ||
}); | ||
|
||
export const attachUserValidation = ( | ||
params: Pick<AdminUsers, "onUserBeforeCreate" | "onUserBeforeUpdate"> | ||
) => { | ||
const { onUserBeforeCreate, onUserBeforeUpdate } = params; | ||
onUserBeforeCreate.subscribe(async ({ inputData }) => { | ||
const model = await new CreateUserDataModel().populate(inputData); | ||
await model.validate(); | ||
const validation = createUserDataValidation.safeParse(inputData); | ||
if (validation.success) { | ||
return; | ||
} | ||
throw createZodError(validation.error); | ||
}); | ||
|
||
onUserBeforeUpdate.subscribe(async ({ inputData }) => { | ||
const model = await new UpdateUserDataModel().populate(inputData); | ||
await model.validate(); | ||
const validation = updateUserDataValidation.safeParse(inputData); | ||
if (validation.success) { | ||
return; | ||
} | ||
throw createZodError(validation.error); | ||
}); | ||
}; |
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
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
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
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
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
Oops, something went wrong.