Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: migration from zod to valibot #2286

Merged
merged 5 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 21 additions & 21 deletions apps/frontend/app/(client)/(main)/settings/_libs/schemas.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
import { z } from 'zod'
import * as v from 'valibot'

export const schemaSettings = (updateNow: boolean) =>
z.object({
currentPassword: z.string().min(1, { message: 'Required' }).optional(),
newPassword: z
.string()
.min(1)
.min(8)
.max(20)
.refine((data) => {
const invalidPassword = /^([a-z]*|[A-Z]*|[0-9]*|[^a-zA-Z0-9]*)$/
return !invalidPassword.test(data)
})
.optional(),
confirmPassword: z.string().optional(),
realName: z
.string()
.regex(/^[a-zA-Z\s]+$/, { message: 'Only English Allowed' })
.optional(),
export const getSchema = (updateNow: boolean) =>
v.object({
currentPassword: v.optional(v.pipe(v.string(), v.minLength(1, 'Required'))),
newPassword: v.optional(
v.pipe(
v.string(),
v.minLength(8),
v.maxLength(20),
v.check((input) => {
const invalidPassword = /^([a-z]*|[A-Z]*|[0-9]*|[^a-zA-Z0-9]*)$/
return !invalidPassword.test(input)
})
)
),
confirmPassword: v.optional(v.string()),
realName: v.optional(
v.pipe(v.string(), v.regex(/^[a-zA-Z\s]+$/, 'Only English Allowed'))
),
studentId: updateNow
? z.string().regex(/^\d{10}$/, { message: 'Only 10 numbers' })
: z.string().optional()
? v.pipe(v.string(), v.regex(/^\d{10}$/, 'Only 10 numbers'))
: v.optional(v.string())
})
6 changes: 3 additions & 3 deletions apps/frontend/app/(client)/(main)/settings/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import { safeFetcherWithAuth } from '@/libs/utils'
import type { SettingsFormat } from '@/types/type'
import { zodResolver } from '@hookform/resolvers/zod'
import { valibotResolver } from '@hookform/resolvers/valibot'
import { useRouter, useSearchParams } from 'next/navigation'
import { useEffect, useRef } from 'react'
import { useState } from 'react'
Expand All @@ -22,7 +22,7 @@
import { SettingsProvider } from './_components/context'
import type { Profile } from './_components/context'
import { useCheckPassword } from './_libs/hooks/useCheckPassword'
import { schemaSettings } from './_libs/schemas'
import { getSchema } from './_libs/schemas'
import { useConfirmNavigation } from './_libs/utils'

type UpdatePayload = Partial<{
Expand Down Expand Up @@ -72,7 +72,7 @@
watch,
formState: { errors }
} = useForm<SettingsFormat>({
resolver: zodResolver(schemaSettings(!!updateNow)),
resolver: valibotResolver(getSchema(!!updateNow)),
mode: 'onChange',
defaultValues: {
currentPassword: '',
Expand Down Expand Up @@ -127,7 +127,7 @@
setValue('newPassword', newPassword)
setValue('confirmPassword', confirmPassword)
}
}, [isPasswordsMatch, newPassword, confirmPassword])

Check warning on line 130 in apps/frontend/app/(client)/(main)/settings/page.tsx

View workflow job for this annotation

GitHub Actions / Lint

React Hook useEffect has a missing dependency: 'setValue'. Either include it or remove the dependency array

Check warning on line 130 in apps/frontend/app/(client)/(main)/settings/page.tsx

View workflow job for this annotation

GitHub Actions / Lint

React Hook useEffect has a missing dependency: 'setValue'. Either include it or remove the dependency array
const onSubmit = async (data: SettingsFormat) => {
try {
// 필요 없는 필드 제외 (defaultProfileValues와 값이 같은 것들은 제외)
Expand Down
4 changes: 2 additions & 2 deletions apps/frontend/app/admin/contest/[contestId]/edit/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import { UPDATE_CONTEST_PROBLEMS_ORDER } from '@/graphql/problem/mutations'
import { GET_CONTEST_PROBLEMS } from '@/graphql/problem/queries'
import { useMutation, useQuery } from '@apollo/client'
import type { UpdateContestInput } from '@generated/graphql'
import { zodResolver } from '@hookform/resolvers/zod'
import { valibotResolver } from '@hookform/resolvers/valibot'
import { PlusCircleIcon } from 'lucide-react'
import Link from 'next/link'
import { useRouter } from 'next/navigation'
Expand Down Expand Up @@ -69,7 +69,7 @@ export default function Page({ params }: { params: { contestId: string } }) {
useConfirmNavigation(shouldSkipWarning)

const methods = useForm<UpdateContestInput>({
resolver: zodResolver(editSchema),
resolver: valibotResolver(editSchema),
defaultValues: {
isRankVisible: true,
isVisible: true
Expand Down
50 changes: 28 additions & 22 deletions apps/frontend/app/admin/contest/_libs/schemas.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,34 @@
import { z } from 'zod'
import * as v from 'valibot'

export const createSchema = z.object({
title: z
.string()
.min(1, 'The title must contain at least 1 character(s)')
.max(200, 'The title can only be up to 200 characters long'),
isRankVisible: z.boolean(),
isVisible: z.boolean(),
description: z
.string()
.min(1)
.refine((value) => value !== '<p></p>'),
startTime: z.date(),
endTime: z.date(),
enableCopyPaste: z.boolean(),
isJudgeResultVisible: z.boolean(),
invitationCode: z
.string()
.regex(/^\d{6}$/, 'The invitation code must be a 6-digit number')
.nullish()
export const createSchema = v.object({
title: v.pipe(
v.string(),
v.minLength(1, 'The title must contain at least 1 character(s)'),
v.maxLength(200, 'The title can only be up to 200 characters long')
),

isRankVisible: v.boolean(),
isVisible: v.boolean(),
description: v.pipe(
v.string(),
v.minLength(1),
v.check((value) => value !== '<p></p>')
),
startTime: v.date(),
endTime: v.date(),
enableCopyPaste: v.boolean(),
isJudgeResultVisible: v.boolean(),
invitationCode: v.nullable(
v.pipe(
v.string(),
v.regex(/^\d{6}$/, 'The invitation code must be a 6-digit number')
)
)
})

export const editSchema = createSchema.extend({
id: z.number()
export const editSchema = v.object({
id: v.number(),
...createSchema.entries
})

export interface ContestProblem {
Expand Down
4 changes: 2 additions & 2 deletions apps/frontend/app/admin/contest/create/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {
import { UPDATE_CONTEST_PROBLEMS_ORDER } from '@/graphql/problem/mutations'
import { useMutation } from '@apollo/client'
import type { CreateContestInput } from '@generated/graphql'
import { zodResolver } from '@hookform/resolvers/zod'
import { valibotResolver } from '@hookform/resolvers/valibot'
import { PlusCircleIcon } from 'lucide-react'
import Link from 'next/link'
import { useRouter } from 'next/navigation'
Expand Down Expand Up @@ -60,7 +60,7 @@ export default function Page() {
useConfirmNavigation(shouldSkipWarning)

const methods = useForm<CreateContestInput>({
resolver: zodResolver(createSchema),
resolver: valibotResolver(createSchema),
defaultValues: {
isRankVisible: true,
isVisible: true,
Expand Down
4 changes: 2 additions & 2 deletions apps/frontend/app/admin/problem/[problemId]/edit/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { UPDATE_PROBLEM } from '@/graphql/problem/mutations'
import { GET_PROBLEM } from '@/graphql/problem/queries'
import { useMutation, useQuery } from '@apollo/client'
import type { Template, Testcase, UpdateProblemInput } from '@generated/graphql'
import { zodResolver } from '@hookform/resolvers/zod'
import { valibotResolver } from '@hookform/resolvers/valibot'
import Link from 'next/link'
import { useRouter } from 'next/navigation'
import { useRef, useState } from 'react'
Expand Down Expand Up @@ -38,7 +38,7 @@ export default function Page({ params }: { params: { problemId: string } }) {
useConfirmNavigation(shouldSkipWarning)

const methods = useForm<UpdateProblemInput>({
resolver: zodResolver(editSchema),
resolver: valibotResolver(editSchema),
defaultValues: { template: [] }
})

Expand Down
115 changes: 61 additions & 54 deletions apps/frontend/app/admin/problem/_libs/schemas.ts
Original file line number Diff line number Diff line change
@@ -1,73 +1,80 @@
import { levels, languages } from '@/libs/constants'
import { z } from 'zod'
import * as v from 'valibot'

const commonSchema = z.object({
title: z
.string()
.min(1, 'The title must contain at least 1 character(s)')
.max(200, 'The title can only be up to 200 characters long'),
difficulty: z.enum(levels),
languages: z.array(z.enum(languages)).min(1),
description: z
.string()
.min(1)
.refine((value) => value !== '<p></p>'),
inputDescription: z
.string()
.min(1)
.refine((value) => value !== '<p></p>'),
outputDescription: z
.string()
.min(1)
.refine((value) => value !== '<p></p>'),
testcases: z
.array(
z.object({
input: z.string().min(1),
output: z.string().min(1),
isHidden: z.boolean(),
scoreWeight: z.number()
const commonSchema = v.object({
title: v.pipe(
v.string(),
v.minLength(1, 'The title must contain at least 1 character(s)'),
v.maxLength(200, 'The title can only be up to 200 characters long')
),
difficulty: v.picklist(levels),
languages: v.pipe(v.array(v.picklist(languages)), v.minLength(1)),
description: v.pipe(
v.string(),
v.minLength(1),
v.check((value) => value !== '<p></p>')
),
inputDescription: v.pipe(
v.string(),
v.minLength(1),
v.check((value) => value !== '<p></p>')
),
outputDescription: v.pipe(
v.string(),
v.minLength(1),
v.check((value) => value !== '<p></p>')
),
testcases: v.pipe(
v.array(
v.object({
input: v.pipe(v.string(), v.minLength(1)),
output: v.pipe(v.string(), v.minLength(1)),
isHidden: v.boolean(),
scoreWeight: v.number()
})
)
.min(1),
timeLimit: z.number().min(0),
memoryLimit: z.number().min(0),
hint: z.string().optional(),
source: z.string().optional(),
template: z
.array(
z
.object({
language: z.enum([
),
v.minLength(1)
),
timeLimit: v.pipe(v.number(), v.minValue(0)),
memoryLimit: v.pipe(v.number(), v.minValue(0)),
hint: v.optional(v.string()),
source: v.optional(v.string()),
template: v.optional(
v.array(
v.optional(
v.object({
language: v.picklist([
'C',
'Cpp',
'Golang',
'Java',
'Python2',
'Python3'
]),
code: z.array(
z.object({
id: z.number(),
text: z.string(),
locked: z.boolean()
code: v.array(
v.object({
id: v.number(),
text: v.string(),
locked: v.boolean()
})
)
})
.optional()
)
)
.optional()
)
})

export const editSchema = commonSchema.extend({
id: z.number(),
isVisible: z.boolean().nullish(),
tags: z
.object({ create: z.array(z.number()), delete: z.array(z.number()) })
.optional()
export const editSchema = v.object({
...commonSchema.entries,
id: v.number(),
isVisible: v.nullable(v.boolean()),
tags: v.optional(
v.object({ create: v.array(v.number()), delete: v.array(v.number()) })
)
})

export const createSchema = commonSchema.extend({
isVisible: z.boolean(),
tagIds: z.array(z.number())
export const createSchema = v.object({
...commonSchema.entries,
isVisible: v.boolean(),
tagIds: v.array(v.number())
})
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { createSchema } from '@/app/admin/problem/_libs/schemas'
import { CREATE_PROBLEM } from '@/graphql/problem/mutations'
import { useMutation } from '@apollo/client'
import { Level, type CreateProblemInput } from '@generated/graphql'
import { zodResolver } from '@hookform/resolvers/zod'
import { valibotResolver } from '@hookform/resolvers/valibot'
import { useRouter } from 'next/navigation'
import { useState, type ReactNode } from 'react'
import { FormProvider, useForm } from 'react-hook-form'
Expand All @@ -21,7 +21,7 @@ export default function CreateProblemForm({
children
}: CreateProblemFormProps) {
const methods = useForm<CreateProblemInput>({
resolver: zodResolver(createSchema),
resolver: valibotResolver(createSchema),
defaultValues: {
difficulty: Level.Level1,
tagIds: [],
Expand Down
10 changes: 5 additions & 5 deletions apps/frontend/components/auth/FindUserId.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ import { Input } from '@/components/shadcn/input'
import { cn, fetcher } from '@/libs/utils'
import useAuthModalStore from '@/stores/authModal'
import useRecoverAccountModalStore from '@/stores/recoverAccountModal'
import { zodResolver } from '@hookform/resolvers/zod'
import { valibotResolver } from '@hookform/resolvers/valibot'
import { useState } from 'react'
import { useForm } from 'react-hook-form'
import { z } from 'zod'
import * as v from 'valibot'

interface FindUserIdInput {
email: string
}
const schema = z.object({
email: z.string().email({ message: 'Invalid email address' })
const schema = v.object({
email: v.pipe(v.string(), v.email('Invalid email address'))
})

export default function FindUserId() {
Expand All @@ -33,7 +33,7 @@ export default function FindUserId() {
getValues,
formState: { errors }
} = useForm<FindUserIdInput>({
resolver: zodResolver(schema)
resolver: valibotResolver(schema)
})

const onSubmit = async (data: FindUserIdInput) => {
Expand Down
Loading
Loading