Skip to content

Commit

Permalink
fix: settings schema
Browse files Browse the repository at this point in the history
  • Loading branch information
634750802 committed Apr 16, 2024
1 parent 2de4fa4 commit 7e8ab60
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
1 change: 0 additions & 1 deletion src/app/(main)/(admin)/settings/page.client.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
FormLabel,
FormMessage,
} from '@/components/ui/form';
import {authGuard} from "@/lib/auth-server";
import { useRouter } from 'next/navigation';
import { useForm, useFieldArray } from 'react-hook-form';
import { zodResolver } from '@hookform/resolvers/zod';
Expand Down
21 changes: 13 additions & 8 deletions src/core/schema/setting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,15 @@ export const maxExampleQuestions = 5;
export const maxHomepageFooterLinks = 5;

const assetUrl = (message: string) =>
z.string().url(message).or(z.string().regex(/^\/assets\//, message));
z.union([
z.string().url(message),
z.string().regex(/^\/assets\//, message)
]);

const optionalAssetUrl = (message: string) =>
z.string().url(message).optional().or(z.string().regex(/^\/assets\//, message).optional());
const urlOrEmpty = (message: string) => z.union([
z.literal(''),
z.string().url(),
]).optional()

export const WebsiteSetting = z.object({
title: z.string().min(1, 'title must has at latest 1 character').max(20, 'title must has at most 20 characters'),
Expand All @@ -48,9 +53,9 @@ export const WebsiteSetting = z.object({
footer_links: z.array(z.object({ text: z.string().min(1), href: z.string().min(1) })).optional(),
}),
social: z.object({
twitter: z.string().url('twitter should be a correct URL').optional(),
github: z.string().url('github should be a correct URL').optional(),
discord: z.string().url('discord should be a correct URL').optional(),
twitter: urlOrEmpty('twitter should be a correct URL').optional(),
github: urlOrEmpty('github should be a correct URL').optional(),
discord: urlOrEmpty('discord should be a correct URL').optional(),
}).optional(),
});

Expand Down Expand Up @@ -95,15 +100,15 @@ export const CustomJsSetting = z.object({
button_label: z.string({
required_error: 'Button label is required',
}),
button_img_src: optionalAssetUrl('Button Image Src should be a correct URL of image'),
button_img_src: assetUrl('Button Image Src should be a correct URL of image').optional(),
example_questions: z
.array(z.object({ text: z.string().min(1) }))
.max(
maxExampleQuestions,
`example questions must has at most ${maxExampleQuestions} questions`,
)
.optional(),
logo_src: optionalAssetUrl('Logo Src should be a correct URL of image'),
logo_src: assetUrl('Logo Src should be a correct URL of image').optional(),
widget_title: z.string().min(1, 'title must has at latest 1 character').max(50, 'title must has at most 50 characters').optional(),
widget_input_placeholder: z.string().min(1, 'input placeholder must has at latest 1 character').max(50, 'input placeholder must has at most 50 characters').optional(),
widget_color_mode: theme.optional(),
Expand Down

0 comments on commit 7e8ab60

Please sign in to comment.