From 04fabf115ccea31ac504b06cc51d78423b697f60 Mon Sep 17 00:00:00 2001 From: Martin Kagamino Lehoux Date: Tue, 28 Jul 2026 15:07:57 +0200 Subject: refactor: services layer, valibot validation, shared CSS utilities, aria labels, and drizzle migrate - Extract groceries/categories/references services with db injection - Add valibot for form validation and safe numeric id parsing - Fix category delete FK behavior with onDelete: set null + migration - Replace custom migrate.js with drizzle-orm/libsql/migrator - Add shared CSS utility classes in app.css (forms, lists, buttons) - Add aria-pressed and aria-label on interactive elements - Remove redundant app.css imports and unused /stores import - Add integration tests for all services using drizzle migrate helper - Remove smoke test; add db:migrate and db:studio scripts --- src/lib/server/services/validation.ts | 45 +++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 src/lib/server/services/validation.ts (limited to 'src/lib/server/services/validation.ts') diff --git a/src/lib/server/services/validation.ts b/src/lib/server/services/validation.ts new file mode 100644 index 0000000..a8cebd9 --- /dev/null +++ b/src/lib/server/services/validation.ts @@ -0,0 +1,45 @@ +import * as v from 'valibot'; + +export const IdSchema = v.pipe(v.number(), v.integer(), v.minValue(1)); + +export const AddGrocerySchema = v.object({ + name: v.pipe(v.string(), v.minLength(1)), + categoryId: v.nullable(v.pipe(v.number(), v.integer(), v.minValue(1))), +}); + +export const AddReferenceSchema = v.object({ + reference: v.pipe(v.string(), v.minLength(1)), + price: v.nullable(v.pipe(v.number(), v.minValue(0))), + unit: v.nullable(v.string()), + qualityScore: v.nullable( + v.pipe(v.number(), v.integer(), v.minValue(0), v.maxValue(100)), + ), +}); + +export function numericId( + value: FormDataEntryValue | null | undefined, +): number { + const str = value?.toString().trim() ?? ''; + const n = Number(str); + if (!Number.isFinite(n) || n <= 0 || !Number.isInteger(n)) { + throw new Error('Invalid id'); + } + return n; +} + +export function parseFormString( + value: FormDataEntryValue | null | undefined, +): string | null { + const str = value?.toString().trim() ?? ''; + return str || null; +} + +export function parseOptionalNumber( + value: FormDataEntryValue | null | undefined, +): number | null { + const str = value?.toString().trim() ?? ''; + if (!str) return null; + const n = Number(str); + if (!Number.isFinite(n)) return null; + return n; +} -- cgit v1.3