From f96277b41088769a492b540b419ed8fb90f8579a Mon Sep 17 00:00:00 2001 From: Gao Sun Date: Sat, 13 Jul 2024 21:30:35 +0800 Subject: [PATCH] refactor: patch type issues --- packages/core/src/oidc/utils.ts | 15 +++++++++++---- .../src/routes/logto-config/jwt-customizer.ts | 3 ++- packages/core/src/utils/SchemaRouter.ts | 5 ++++- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/packages/core/src/oidc/utils.ts b/packages/core/src/oidc/utils.ts index f25a4c323..83c4e5f01 100644 --- a/packages/core/src/oidc/utils.ts +++ b/packages/core/src/oidc/utils.ts @@ -55,12 +55,19 @@ export const buildOidcClientMetadata = (metadata?: OidcClientMetadata): OidcClie ...metadata, }); -export const validateCustomClientMetadata = (key: string, value: unknown) => { - const result = customClientMetadataGuard.pick({ [key]: true }).safeParse({ [key]: value }); +// eslint-disable-next-line @typescript-eslint/ban-types +const isKeyOf = (object: T, key: string | number | symbol): key is keyof T => + key in object; - if (!result.success) { - throw new errors.InvalidClientMetadata(key); +export const validateCustomClientMetadata = (key: string, value: unknown) => { + if (isKeyOf(customClientMetadataGuard.shape, key)) { + const result = customClientMetadataGuard.shape[key].safeParse(value); + if (result.success) { + return; + } } + + throw new errors.InvalidClientMetadata(key); }; export const isOriginAllowed = ( diff --git a/packages/core/src/routes/logto-config/jwt-customizer.ts b/packages/core/src/routes/logto-config/jwt-customizer.ts index 43ae020e5..437601a0a 100644 --- a/packages/core/src/routes/logto-config/jwt-customizer.ts +++ b/packages/core/src/routes/logto-config/jwt-customizer.ts @@ -8,6 +8,7 @@ import { jwtCustomizerConfigsGuard, jwtCustomizerTestRequestBodyGuard, } from '@logto/schemas'; +import { removeUndefinedKeys } from '@silverhand/essentials'; import { ResponseError } from '@withtyped/client'; import { ZodError, z } from 'zod'; @@ -236,7 +237,7 @@ export default function logtoConfigJwtCustomizerRoutes { @@ -337,6 +339,7 @@ export default class SchemaRouter< this.post( '/', koaGuard({ + // @ts-expect-error -- `.omit()` doesn't play well with generics body: schema.createGuard.omit({ id: true }), response: entityGuard ?? schema.guard, status: [201], // TODO: 409/422 for conflict?