Phase 5/7: Доработки — конкретные ошибки валидации, CORS multi-origin, ApiError 413
This commit is contained in:
@@ -9,19 +9,16 @@ import { signUserToken } from '../utils/jwt.js';
|
||||
import type { AuthenticatedRequest } from '../middleware/auth.middleware.js';
|
||||
import type { Request, Response } from 'express';
|
||||
|
||||
/** Сообщение об ошибке валидации. */
|
||||
const VALIDATION_MSG = 'Некорректный email или пароль';
|
||||
|
||||
/**
|
||||
* Извлекает email и password из тела запроса.
|
||||
* Извлекает email и password из тела запроса с подробной валидацией.
|
||||
*
|
||||
* @param rawBody — тело запроса (any из Express)
|
||||
* @returns объект с валидированными email и password
|
||||
* @throws {ApiError} если email или password некорректны
|
||||
* @throws {ApiError} с конкретным сообщением о проблеме
|
||||
*/
|
||||
function extractCredentials(rawBody: unknown): { email: string; password: string } {
|
||||
if (typeof rawBody !== 'object' || rawBody === null) {
|
||||
throw new ApiError(400, 'VALIDATION_ERROR', VALIDATION_MSG);
|
||||
throw new ApiError(400, 'VALIDATION_ERROR', 'Тело запроса некорректно');
|
||||
}
|
||||
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions, @typescript-eslint/no-unsafe-type-assertion -- извлечение полей из unknown
|
||||
const record = rawBody as Record<string, unknown>;
|
||||
@@ -30,11 +27,18 @@ function extractCredentials(rawBody: unknown): { email: string; password: string
|
||||
const emailStr = typeof email === 'string' ? email.trim().toLowerCase() : '';
|
||||
const passwordStr = typeof password === 'string' ? password : '';
|
||||
|
||||
if (emailStr.length === 0 || !emailStr.includes('@')) {
|
||||
throw new ApiError(400, 'VALIDATION_ERROR', VALIDATION_MSG);
|
||||
if (emailStr.length === 0) {
|
||||
throw new ApiError(400, 'VALIDATION_ERROR', 'Email не указан');
|
||||
}
|
||||
// eslint-disable-next-line sonarjs/slow-regex -- email regex, ограниченный input
|
||||
if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(emailStr)) {
|
||||
throw new ApiError(400, 'VALIDATION_ERROR', `Некорректный формат email: ${emailStr}`);
|
||||
}
|
||||
if (passwordStr.length === 0) {
|
||||
throw new ApiError(400, 'VALIDATION_ERROR', 'Пароль не указан');
|
||||
}
|
||||
if (passwordStr.length < 8) {
|
||||
throw new ApiError(400, 'VALIDATION_ERROR', VALIDATION_MSG);
|
||||
throw new ApiError(400, 'VALIDATION_ERROR', 'Пароль должен содержать минимум 8 символов');
|
||||
}
|
||||
|
||||
return { email: emailStr, password: passwordStr };
|
||||
|
||||
Reference in New Issue
Block a user