1
0
mirror of https://github.com/pocket-id/pocket-id.git synced 2026-03-23 23:55:07 +00:00

refactor: upgrade to Zod v4 (#623)

This commit is contained in:
Elias Schneider
2025-06-08 15:44:59 +02:00
committed by GitHub
parent 9a4aab465a
commit 388a874922
18 changed files with 270 additions and 51 deletions

View File

@@ -1,5 +1,5 @@
import { writable } from 'svelte/store';
import { z } from 'zod';
import { get, writable } from 'svelte/store';
import { z } from 'zod/v4';
export type FormInput<T> = {
value: T;
@@ -13,6 +13,7 @@ type FormInputs<T> = {
export function createForm<T extends z.ZodType<any, any>>(schema: T, initialValues: z.infer<T>) {
// Create a writable store for the inputs
const inputsStore = writable<FormInputs<z.infer<T>>>(initializeInputs(initialValues));
const errorsStore = writable<z.ZodError<any> | undefined>();
function initializeInputs(initialValues: z.infer<T>): FormInputs<z.infer<T>> {
const inputs: FormInputs<z.infer<T>> = {} as FormInputs<z.infer<T>>;
@@ -36,11 +37,12 @@ export function createForm<T extends z.ZodType<any, any>>(schema: T, initialValu
);
const result = schema.safeParse(values);
errorsStore.set(result.error);
if (!result.success) {
success = false;
for (const input of Object.keys(inputs)) {
const error = result.error.errors.find((e) => e.path[0] === input);
const error = result.error.issues.find((e) => e.path[0] === input);
if (error) {
inputs[input as keyof z.infer<T>].error = error.message;
} else {
@@ -58,15 +60,14 @@ export function createForm<T extends z.ZodType<any, any>>(schema: T, initialValu
}
function data() {
let values: z.infer<T> | null = null;
inputsStore.subscribe((inputs) => {
values = Object.fromEntries(
Object.entries(inputs).map(([key, input]) => {
input.value = trimValue(input.value);
return [key, input.value];
})
) as z.infer<T>;
})();
const inputs = get(inputsStore);
const values = Object.fromEntries(
Object.entries(inputs).map(([key, input]) => {
input.value = trimValue(input.value);
return [key, input.value];
})
) as z.infer<T>;
return values;
}
@@ -108,6 +109,7 @@ export function createForm<T extends z.ZodType<any, any>>(schema: T, initialValu
return {
schema,
inputs: inputsStore,
errors: errorsStore,
data,
validate,
setValue,

View File

@@ -0,0 +1,10 @@
import { setLocale as setParaglideLocale, type Locale } from '$lib/paraglide/runtime';
import { z } from 'zod/v4';
export function setLocale(locale: Locale, reload = true) {
import(`../../../node_modules/zod/dist/esm/v4/locales/${locale}.js`)
.then((zodLocale) => z.config(zodLocale.default()))
.finally(() => {
setParaglideLocale(locale, { reload });
});
}