refactor(persons): centralise PersonType, PERSON_TYPES and normalizePersonType in person-validation

Removes four independent PersonType type declarations and the duplicated
TYPES/PERSON_TYPES arrays. normalizePersonType moves from the edit route
module into the shared lib so page.server.test.ts no longer imports from a
route. Both server actions now use normalizePersonType for personType
extraction instead of an inline type cast.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Marcel
2026-04-26 10:22:52 +02:00
committed by marcel
parent 23861055d1
commit 327fd89cb9
7 changed files with 15 additions and 28 deletions

View File

@@ -2,9 +2,7 @@
import { untrack } from 'svelte'; import { untrack } from 'svelte';
import { radioGroupNav } from '$lib/actions/radioGroupNav'; import { radioGroupNav } from '$lib/actions/radioGroupNav';
import { m } from '$lib/paraglide/messages.js'; import { m } from '$lib/paraglide/messages.js';
import { PERSON_TYPES as TYPES, type PersonType } from '$lib/person-validation';
const TYPES = ['PERSON', 'INSTITUTION', 'GROUP', 'UNKNOWN'] as const;
type PersonType = (typeof TYPES)[number];
let { let {
value = 'PERSON', value = 'PERSON',

View File

@@ -1,3 +1,10 @@
export const PERSON_TYPES = ['PERSON', 'INSTITUTION', 'GROUP', 'UNKNOWN'] as const;
export type PersonType = (typeof PERSON_TYPES)[number];
export function normalizePersonType(raw: string | undefined | null): PersonType {
return raw === 'SKIP' ? 'UNKNOWN' : ((raw ?? 'PERSON') as PersonType);
}
export function validatePersonFields( export function validatePersonFields(
personType: string, personType: string,
firstName: string | undefined | null, firstName: string | undefined | null,

View File

@@ -1,13 +1,7 @@
import { error, fail, redirect } from '@sveltejs/kit'; import { error, fail, redirect } from '@sveltejs/kit';
import { createApiClient } from '$lib/api.server'; import { createApiClient } from '$lib/api.server';
import { getErrorMessage } from '$lib/errors'; import { getErrorMessage } from '$lib/errors';
import { validatePersonFields } from '$lib/person-validation'; import { normalizePersonType, validatePersonFields } from '$lib/person-validation';
type PersonType = 'PERSON' | 'INSTITUTION' | 'GROUP' | 'UNKNOWN' | 'SKIP';
export function normalizePersonType(raw: string | undefined | null): Exclude<PersonType, 'SKIP'> {
return raw === 'SKIP' ? 'UNKNOWN' : ((raw ?? 'PERSON') as Exclude<PersonType, 'SKIP'>);
}
export async function load({ params, fetch, locals }) { export async function load({ params, fetch, locals }) {
const canWrite = const canWrite =
@@ -37,11 +31,7 @@ export async function load({ params, fetch, locals }) {
export const actions = { export const actions = {
update: async ({ request, params, fetch }) => { update: async ({ request, params, fetch }) => {
const formData = await request.formData(); const formData = await request.formData();
const personType = (formData.get('personType')?.toString() ?? 'PERSON') as const personType = normalizePersonType(formData.get('personType')?.toString());
| 'PERSON'
| 'INSTITUTION'
| 'GROUP'
| 'UNKNOWN';
const title = formData.get('title')?.toString().trim() || undefined; const title = formData.get('title')?.toString().trim() || undefined;
const firstName = formData.get('firstName')?.toString().trim(); const firstName = formData.get('firstName')?.toString().trim();
const lastName = formData.get('lastName')?.toString().trim(); const lastName = formData.get('lastName')?.toString().trim();

View File

@@ -2,8 +2,7 @@
import { untrack } from 'svelte'; import { untrack } from 'svelte';
import { m } from '$lib/paraglide/messages.js'; import { m } from '$lib/paraglide/messages.js';
import PersonTypeSelector from '$lib/components/PersonTypeSelector.svelte'; import PersonTypeSelector from '$lib/components/PersonTypeSelector.svelte';
import { PERSON_TYPES as TYPES, type PersonType } from '$lib/person-validation';
type PersonType = 'PERSON' | 'INSTITUTION' | 'GROUP' | 'UNKNOWN';
let { let {
person person
@@ -20,7 +19,6 @@ let {
}; };
} = $props(); } = $props();
const TYPES = ['PERSON', 'INSTITUTION', 'GROUP', 'UNKNOWN'] as const;
let selectedType = $state<PersonType>( let selectedType = $state<PersonType>(
untrack(() => untrack(() =>
TYPES.includes(person.personType as PersonType) ? (person.personType as PersonType) : 'PERSON' TYPES.includes(person.personType as PersonType) ? (person.personType as PersonType) : 'PERSON'

View File

@@ -1,5 +1,5 @@
import { describe, it, expect } from 'vitest'; import { describe, it, expect } from 'vitest';
import { normalizePersonType } from './+page.server'; import { normalizePersonType } from '$lib/person-validation';
describe('edit load — SKIP → UNKNOWN normalization', () => { describe('edit load — SKIP → UNKNOWN normalization', () => {
it('maps SKIP to UNKNOWN', () => { it('maps SKIP to UNKNOWN', () => {

View File

@@ -1,7 +1,7 @@
import { error, fail, redirect } from '@sveltejs/kit'; import { error, fail, redirect } from '@sveltejs/kit';
import { createApiClient } from '$lib/api.server'; import { createApiClient } from '$lib/api.server';
import { getErrorMessage } from '$lib/errors'; import { getErrorMessage } from '$lib/errors';
import { validatePersonFields } from '$lib/person-validation'; import { normalizePersonType, validatePersonFields } from '$lib/person-validation';
export async function load({ locals }: { locals: App.Locals }) { export async function load({ locals }: { locals: App.Locals }) {
const canWrite = const canWrite =
@@ -14,11 +14,7 @@ export async function load({ locals }: { locals: App.Locals }) {
export const actions = { export const actions = {
default: async ({ request, fetch }) => { default: async ({ request, fetch }) => {
const formData = await request.formData(); const formData = await request.formData();
const personType = (formData.get('personType')?.toString() ?? 'PERSON') as const personType = normalizePersonType(formData.get('personType')?.toString());
| 'PERSON'
| 'INSTITUTION'
| 'GROUP'
| 'UNKNOWN';
const title = formData.get('title')?.toString().trim() || undefined; const title = formData.get('title')?.toString().trim() || undefined;
const firstName = formData.get('firstName')?.toString().trim(); const firstName = formData.get('firstName')?.toString().trim();
const lastName = formData.get('lastName')?.toString().trim(); const lastName = formData.get('lastName')?.toString().trim();

View File

@@ -3,9 +3,7 @@ import { untrack } from 'svelte';
import { m } from '$lib/paraglide/messages.js'; import { m } from '$lib/paraglide/messages.js';
import BackButton from '$lib/components/BackButton.svelte'; import BackButton from '$lib/components/BackButton.svelte';
import PersonTypeSelector from '$lib/components/PersonTypeSelector.svelte'; import PersonTypeSelector from '$lib/components/PersonTypeSelector.svelte';
import { PERSON_TYPES as TYPES, type PersonType } from '$lib/person-validation';
type PersonType = 'PERSON' | 'INSTITUTION' | 'GROUP' | 'UNKNOWN';
const TYPES = ['PERSON', 'INSTITUTION', 'GROUP', 'UNKNOWN'] as const;
let { form } = $props(); let { form } = $props();