From cd63388ab1e156c0085d95cddb88606b4625cd59 Mon Sep 17 00:00:00 2001 From: "IceHe.xyz" Date: Thu, 31 Mar 2022 14:19:12 +0800 Subject: [PATCH] feat(schemas): zod type guards of sign-in experience jsonb fields (#473) * feat(schemas): guard branding logo url and terms-of-use content url * feat(schemas): guard branding logo url non-optional * feat(schemas): guard branding colors and slogan nonempty * chore(core,schemas): upgrade zod dependency version from 3.8.1 to 3.14.3 * feat(schemas): jsonb types guard hex color values * feat(core): hex color regex --- packages/core/package.json | 2 +- packages/core/src/utils/regex.ts | 1 + packages/schemas/package.json | 2 +- packages/schemas/src/foundations/jsonb-types.ts | 16 +++++++++------- pnpm-lock.yaml | 12 ++++++------ 5 files changed, 18 insertions(+), 15 deletions(-) diff --git a/packages/core/package.json b/packages/core/package.json index 0f4614b2b..5fe0b0f97 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -44,7 +44,7 @@ "slonik": "^23.8.3", "slonik-interceptor-preset": "^1.2.10", "snakecase-keys": "^5.1.0", - "zod": "^3.8.1" + "zod": "^3.14.3" }, "devDependencies": { "@shopify/jest-koa-mocks": "^3.0.8", diff --git a/packages/core/src/utils/regex.ts b/packages/core/src/utils/regex.ts index fddeacf39..a27f09913 100644 --- a/packages/core/src/utils/regex.ts +++ b/packages/core/src/utils/regex.ts @@ -3,3 +3,4 @@ export const phoneRegEx = /^[1-9]\d{10}$/; export const usernameRegEx = /^.{3,}$/; export const nameRegEx = /^.{3,}$/; export const passwordRegEx = /^.{6,}$/; +export { hexColorRegEx } from '@logto/schemas'; diff --git a/packages/schemas/package.json b/packages/schemas/package.json index bacb075c1..2b43eb291 100644 --- a/packages/schemas/package.json +++ b/packages/schemas/package.json @@ -45,6 +45,6 @@ "prettier": "@silverhand/eslint-config/.prettierrc", "dependencies": { "@logto/phrases": "^0.1.0", - "zod": "^3.8.1" + "zod": "^3.14.3" } } diff --git a/packages/schemas/src/foundations/jsonb-types.ts b/packages/schemas/src/foundations/jsonb-types.ts index 51ae409a1..f63aec1bc 100644 --- a/packages/schemas/src/foundations/jsonb-types.ts +++ b/packages/schemas/src/foundations/jsonb-types.ts @@ -87,22 +87,24 @@ export enum BrandingStyle { Logo_Slogan = 'Logo_Slogan', } +export const hexColorRegEx = /^#[\da-f]{3}([\da-f]{3})?$/i; + export const brandingGuard = z.object({ - primaryColor: z.string(), - backgroundColor: z.string(), + primaryColor: z.string().regex(hexColorRegEx), + backgroundColor: z.string().regex(hexColorRegEx), darkMode: z.boolean(), - darkPrimaryColor: z.string(), - darkBackgroundColor: z.string(), + darkPrimaryColor: z.string().regex(hexColorRegEx), + darkBackgroundColor: z.string().regex(hexColorRegEx), style: z.nativeEnum(BrandingStyle), - logoUrl: z.string().optional(), - slogan: z.string().optional(), + logoUrl: z.string().url(), + slogan: z.string().nonempty().optional(), }); export type Branding = z.infer; export const termsOfUseGuard = z.object({ enabled: z.boolean(), - contentUrl: z.string().optional(), + contentUrl: z.string().url().optional(), }); export type TermsOfUse = z.infer; diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 63cdcc631..5e4da0e99 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -161,7 +161,7 @@ importers: ts-jest: ^27.0.5 tsc-watch: ^4.4.0 typescript: ^4.6.2 - zod: ^3.8.1 + zod: ^3.14.3 dependencies: '@logto/phrases': link:../phrases '@logto/schemas': link:../schemas @@ -188,7 +188,7 @@ importers: slonik: 23.9.0 slonik-interceptor-preset: 1.2.10 snakecase-keys: 5.1.2 - zod: 3.11.6 + zod: 3.14.3 devDependencies: '@shopify/jest-koa-mocks': 3.0.8 '@silverhand/eslint-config': 0.9.4_3a533fa6cc3da0cf8525ef55d41c4384 @@ -252,10 +252,10 @@ importers: prettier: ^2.3.2 ts-node: ^10.0.0 typescript: ^4.6.2 - zod: ^3.8.1 + zod: ^3.14.3 dependencies: '@logto/phrases': link:../phrases - zod: 3.11.6 + zod: 3.14.3 devDependencies: '@silverhand/eslint-config': 0.9.4_3a533fa6cc3da0cf8525ef55d41c4384 '@silverhand/essentials': 1.1.2 @@ -14446,8 +14446,8 @@ packages: engines: {node: '>=10'} dev: true - /zod/3.11.6: - resolution: {integrity: sha512-daZ80A81I3/9lIydI44motWe6n59kRBfNzTuS2bfzVh1nAXi667TOTWWtatxyG+fwgNUiagSj/CWZwRRbevJIg==} + /zod/3.14.3: + resolution: {integrity: sha512-OzwRCSXB1+/8F6w6HkYHdbuWysYWnAF4fkRgKDcSFc54CE+Sv0rHXKfeNUReGCrHukm1LNpi6AYeXotznhYJbQ==} dev: false /zwitch/1.0.5: