110 lines
2.7 KiB
JavaScript
110 lines
2.7 KiB
JavaScript
class UserValidator {
|
|
/**
|
|
* User-Erstellungsdaten validieren
|
|
*/
|
|
validateCreateUser(userData) {
|
|
const errors = [];
|
|
|
|
if (!userData.name || userData.name.trim().length < 2) {
|
|
errors.push('Name muss mindestens 2 Zeichen lang sein');
|
|
}
|
|
|
|
if (!userData.email || !this.isValidEmail(userData.email)) {
|
|
errors.push('Gültige E-Mail-Adresse ist erforderlich');
|
|
}
|
|
|
|
if (!userData.password || userData.password.length < 6) {
|
|
errors.push('Passwort muss mindestens 6 Zeichen lang sein');
|
|
}
|
|
|
|
if (errors.length > 0) {
|
|
throw new Error(`VALIDATION_ERROR: ${errors.join(', ')}`);
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* User-Update-Daten validieren
|
|
*/
|
|
validateUpdateUser(updateData) {
|
|
const errors = [];
|
|
|
|
if (updateData.name !== undefined && (updateData.name.trim().length < 2)) {
|
|
errors.push('Name muss mindestens 2 Zeichen lang sein');
|
|
}
|
|
|
|
if (updateData.email !== undefined && !this.isValidEmail(updateData.email)) {
|
|
errors.push('Gültige E-Mail-Adresse ist erforderlich');
|
|
}
|
|
|
|
if (updateData.active !== undefined && typeof updateData.active !== 'boolean') {
|
|
errors.push('Active muss ein Boolean-Wert sein');
|
|
}
|
|
|
|
// Warnung für sensible Felder
|
|
if (updateData.password !== undefined) {
|
|
throw new Error('VALIDATION_ERROR: Passwort kann nicht über diese Route geändert werden');
|
|
}
|
|
|
|
if (updateData.id !== undefined) {
|
|
throw new Error('VALIDATION_ERROR: ID kann nicht geändert werden');
|
|
}
|
|
|
|
if (updateData.created_at !== undefined) {
|
|
throw new Error('VALIDATION_ERROR: Erstellungsdatum kann nicht geändert werden');
|
|
}
|
|
|
|
if (errors.length > 0) {
|
|
throw new Error(`VALIDATION_ERROR: ${errors.join(', ')}`);
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Passwort-Änderung validieren
|
|
*/
|
|
validatePasswordChange(currentPassword, newPassword) {
|
|
const errors = [];
|
|
|
|
if (!currentPassword) {
|
|
errors.push('Aktuelles Passwort ist erforderlich');
|
|
}
|
|
|
|
if (!newPassword || newPassword.length < 6) {
|
|
errors.push('Neues Passwort muss mindestens 6 Zeichen lang sein');
|
|
}
|
|
|
|
if (currentPassword === newPassword) {
|
|
errors.push('Neues Passwort muss sich vom aktuellen unterscheiden');
|
|
}
|
|
|
|
if (errors.length > 0) {
|
|
throw new Error(`VALIDATION_ERROR: ${errors.join(', ')}`);
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* E-Mail-Format validieren
|
|
*/
|
|
isValidEmail(email) {
|
|
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
return emailRegex.test(email);
|
|
}
|
|
|
|
/**
|
|
* ID validieren
|
|
*/
|
|
validateId(id) {
|
|
if (!id || isNaN(parseInt(id))) {
|
|
throw new Error('VALIDATION_ERROR: Ungültige ID');
|
|
}
|
|
return true;
|
|
}
|
|
}
|
|
|
|
module.exports = new UserValidator();
|