diff options
Diffstat (limited to 'src-migrate/common/stores/useRegisterStore.ts')
| -rw-r--r-- | src-migrate/common/stores/useRegisterStore.ts | 26 |
1 files changed, 14 insertions, 12 deletions
diff --git a/src-migrate/common/stores/useRegisterStore.ts b/src-migrate/common/stores/useRegisterStore.ts index ab2d7410..90ce8a2b 100644 --- a/src-migrate/common/stores/useRegisterStore.ts +++ b/src-migrate/common/stores/useRegisterStore.ts @@ -1,7 +1,7 @@ import { create } from 'zustand'; import { RegisterProps } from '../types/auth'; import { registerSchema } from '../validations/auth'; -import { ValidationError } from 'yup'; +import { ZodError } from 'zod'; type State = { form: RegisterProps; @@ -34,18 +34,20 @@ export const useRegisterStore = create<State & Action>((set, get) => ({ set((state) => ({ form: { ...state.form, [name]: value } })), errors: {}, - validate: () => - registerSchema - .validate(get().form, { abortEarly: false }) - .then(() => set({ errors: {} })) - .catch((err: ValidationError) => { - const validationErrors: State['errors'] = {}; - err.inner.forEach( - (e) => (validationErrors[e.path as keyof RegisterProps] = e.message) + validate: () => { + try { + registerSchema.parse(get().form); + set({ errors: {} }); + } catch (error) { + if (error instanceof ZodError) { + const errors: State['errors'] = {}; + error.errors.forEach( + (e) => (errors[e.path[0] as keyof RegisterProps] = e.message) ); - set({ errors: validationErrors }); - }), - + set({ errors }); + } + } + }, isCheckedTNC: false, toggleCheckTNC: () => set((state) => ({ isCheckedTNC: !state.isCheckedTNC })), |
