Skip to content

Commit

Permalink
n99: refactor: Replace yup with zod
Browse files Browse the repository at this point in the history
  • Loading branch information
aleksandr-kiliushin committed Jun 18, 2023
1 parent 333903f commit 80b20cd
Show file tree
Hide file tree
Showing 11 changed files with 109 additions and 219 deletions.
95 changes: 12 additions & 83 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
"react-router-dom": "6.13.0",
"react-use": "17.4.0",
"recharts": "2.7.1",
"yup": "1.2.0",
"zod": "3.21.4"
},
"devDependencies": {
Expand Down
54 changes: 0 additions & 54 deletions src/components/form-contructor/RadioGroup.tsx

This file was deleted.

24 changes: 12 additions & 12 deletions src/views/auth/Login/form-helpers.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import * as yup from "yup"
import { z } from "zod"

export enum FieldName {
Password = "password",
Username = "username",
}

export const validationSchema = yup
.object({
[FieldName.Password]: yup.string().required(),
[FieldName.Username]: yup.string().required(),
})
.required()
export const validationSchema = z.object({
[FieldName.Password]: z.string().nonempty(),
[FieldName.Username]: z.string().nonempty(),
})

export const defaultValues: TFormValues = {
password: "",
username: "",
}
export type TFormValidValues = z.infer<typeof validationSchema>

export type TFormDefaultValues = TFormValidValues

export type TFormValues = yup.InferType<typeof validationSchema>
export const defaultValues: TFormValidValues = {
[FieldName.Password]: "",
[FieldName.Username]: "",
}
8 changes: 4 additions & 4 deletions src/views/auth/Login/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { yupResolver } from "@hookform/resolvers/yup"
import { zodResolver } from "@hookform/resolvers/zod"
import { Button, TextField, Typography } from "@mui/material"
import { FC } from "react"
import { useForm } from "react-hook-form"
Expand All @@ -10,7 +10,7 @@ import { RowGroup } from "#components/RowGroup"
import { apolloClient } from "#utils/apolloClient"

import { Container } from "../components"
import { FieldName, TFormValues, defaultValues, validationSchema } from "./form-helpers"
import { FieldName, TFormDefaultValues, TFormValidValues, defaultValues, validationSchema } from "./form-helpers"

export const Login: FC = () => {
const navigate = useNavigate()
Expand All @@ -21,10 +21,10 @@ export const Login: FC = () => {
handleSubmit,
register,
setError,
} = useForm<TFormValues>({
} = useForm<TFormDefaultValues, void, TFormValidValues>({
defaultValues,
mode: "onChange",
resolver: yupResolver(validationSchema),
resolver: zodResolver(validationSchema),
})

const onSubmit = handleSubmit(async ({ password, username }) => {
Expand Down
19 changes: 11 additions & 8 deletions src/views/boards/BoardSettings/CategoryFormDialog/form-helpers.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import * as yup from "yup"
import { z } from "zod"

export enum FieldName {
Name = "name",
TypeId = "typeId",
}

export const validationSchema = yup
.object({
name: yup.string().required(),
typeId: yup.number().required(),
})
.required()
export const validationSchema = z.object({
[FieldName.Name]: z.string().nonempty(),
[FieldName.TypeId]: z.number(),
})

export type TFormValues = yup.InferType<typeof validationSchema>
export type TFormValidValues = z.infer<typeof validationSchema>

export type TFormDefaultValues = {
[FieldName.Name]: string
[FieldName.TypeId]: number | null
}
Loading

0 comments on commit 80b20cd

Please sign in to comment.