test(persons): extract validatePersonFields and cover validation branches
Some checks failed
CI / Unit & Component Tests (push) Failing after 3m28s
CI / OCR Service Tests (push) Successful in 36s
CI / Backend Unit Tests (push) Failing after 2m57s
CI / Unit & Component Tests (pull_request) Failing after 3m12s
CI / OCR Service Tests (pull_request) Successful in 50s
CI / Backend Unit Tests (pull_request) Failing after 3m3s
Some checks failed
CI / Unit & Component Tests (push) Failing after 3m28s
CI / OCR Service Tests (push) Successful in 36s
CI / Backend Unit Tests (push) Failing after 2m57s
CI / Unit & Component Tests (pull_request) Failing after 3m12s
CI / OCR Service Tests (pull_request) Successful in 50s
CI / Backend Unit Tests (pull_request) Failing after 3m3s
- New src/lib/person-validation.ts exports validatePersonFields (pure function) - 8 unit tests covering: valid PERSON, lastName missing/undefined, firstName missing/undefined for PERSON, non-PERSON types without firstName - Both edit and new-person server actions now call the shared helper instead of inline if-chains, making the logic testable and non-duplicated Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
38
frontend/src/lib/person-validation.test.ts
Normal file
38
frontend/src/lib/person-validation.test.ts
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
import { describe, it, expect } from 'vitest';
|
||||||
|
import { validatePersonFields } from './person-validation';
|
||||||
|
|
||||||
|
describe('validatePersonFields', () => {
|
||||||
|
it('returns null when all required fields are present for PERSON', () => {
|
||||||
|
expect(validatePersonFields('PERSON', 'Hans', 'Müller')).toBeNull();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns lastName error when lastName is missing', () => {
|
||||||
|
expect(validatePersonFields('PERSON', 'Hans', '')).toBe('Nachname ist Pflichtfeld.');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns lastName error when lastName is undefined', () => {
|
||||||
|
expect(validatePersonFields('INSTITUTION', undefined, undefined)).toBe(
|
||||||
|
'Nachname ist Pflichtfeld.'
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns firstName error when type is PERSON and firstName is missing', () => {
|
||||||
|
expect(validatePersonFields('PERSON', '', 'Müller')).toBe('Vorname ist Pflichtfeld.');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns firstName error when type is PERSON and firstName is undefined', () => {
|
||||||
|
expect(validatePersonFields('PERSON', undefined, 'Müller')).toBe('Vorname ist Pflichtfeld.');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns null for INSTITUTION without firstName', () => {
|
||||||
|
expect(validatePersonFields('INSTITUTION', undefined, 'Verlag GmbH')).toBeNull();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns null for GROUP without firstName', () => {
|
||||||
|
expect(validatePersonFields('GROUP', undefined, 'Familie Raddatz')).toBeNull();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns null for UNKNOWN without firstName', () => {
|
||||||
|
expect(validatePersonFields('UNKNOWN', undefined, 'Unbekannt')).toBeNull();
|
||||||
|
});
|
||||||
|
});
|
||||||
9
frontend/src/lib/person-validation.ts
Normal file
9
frontend/src/lib/person-validation.ts
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
export function validatePersonFields(
|
||||||
|
personType: string,
|
||||||
|
firstName: string | undefined | null,
|
||||||
|
lastName: string | undefined | null
|
||||||
|
): string | null {
|
||||||
|
if (!lastName) return 'Nachname ist Pflichtfeld.';
|
||||||
|
if (personType === 'PERSON' && !firstName) return 'Vorname ist Pflichtfeld.';
|
||||||
|
return null;
|
||||||
|
}
|
||||||
@@ -1,6 +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';
|
||||||
|
|
||||||
type PersonType = 'PERSON' | 'INSTITUTION' | 'GROUP' | 'UNKNOWN' | 'SKIP';
|
type PersonType = 'PERSON' | 'INSTITUTION' | 'GROUP' | 'UNKNOWN' | 'SKIP';
|
||||||
|
|
||||||
@@ -51,11 +52,9 @@ export const actions = {
|
|||||||
const birthYear = birthYearStr ? parseInt(birthYearStr, 10) : undefined;
|
const birthYear = birthYearStr ? parseInt(birthYearStr, 10) : undefined;
|
||||||
const deathYear = deathYearStr ? parseInt(deathYearStr, 10) : undefined;
|
const deathYear = deathYearStr ? parseInt(deathYearStr, 10) : undefined;
|
||||||
|
|
||||||
if (!lastName) {
|
const validationError = validatePersonFields(personType, firstName, lastName);
|
||||||
return fail(400, { updateError: 'Nachname ist Pflichtfeld.' });
|
if (validationError) {
|
||||||
}
|
return fail(400, { updateError: validationError });
|
||||||
if (personType === 'PERSON' && !firstName) {
|
|
||||||
return fail(400, { updateError: 'Vorname ist Pflichtfeld.' });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const api = createApiClient(fetch);
|
const api = createApiClient(fetch);
|
||||||
|
|||||||
@@ -1,6 +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';
|
||||||
|
|
||||||
export async function load({ locals }: { locals: App.Locals }) {
|
export async function load({ locals }: { locals: App.Locals }) {
|
||||||
const canWrite =
|
const canWrite =
|
||||||
@@ -26,23 +27,14 @@ export const actions = {
|
|||||||
const deathYearStr = formData.get('deathYear')?.toString().trim();
|
const deathYearStr = formData.get('deathYear')?.toString().trim();
|
||||||
const notes = formData.get('notes')?.toString().trim() || undefined;
|
const notes = formData.get('notes')?.toString().trim() || undefined;
|
||||||
|
|
||||||
if (!lastName) {
|
const validationError = validatePersonFields(personType, firstName, lastName);
|
||||||
|
if (validationError) {
|
||||||
return fail(400, {
|
return fail(400, {
|
||||||
error: 'Nachname ist Pflichtfeld.',
|
error: validationError,
|
||||||
personType,
|
personType,
|
||||||
title,
|
title,
|
||||||
firstName,
|
firstName: firstName ?? '',
|
||||||
lastName: '',
|
lastName: lastName ?? '',
|
||||||
alias
|
|
||||||
});
|
|
||||||
}
|
|
||||||
if (personType === 'PERSON' && !firstName) {
|
|
||||||
return fail(400, {
|
|
||||||
error: 'Vorname ist Pflichtfeld.',
|
|
||||||
personType,
|
|
||||||
title,
|
|
||||||
firstName: '',
|
|
||||||
lastName,
|
|
||||||
alias
|
alias
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user