- number-поля теперь рендерятся как text + inputMode=numeric, чтобы браузер не округлял значения через input type=number - пробелы при вставке в number-поля удаляются - бэкенд нормализует значения number-полей в строку перед сохранением - добавлен хелпер normalizeFieldValueForStorage Closes: искажение расчётного счёта и других длинных числовых полей
57 lines
1.5 KiB
TypeScript
57 lines
1.5 KiB
TypeScript
import type { Request, Response, NextFunction } from 'express';
|
|
import { ZodSchema, ZodError } from 'zod';
|
|
|
|
export const validateRequest = (schema: ZodSchema) => {
|
|
return (req: Request, res: Response, next: NextFunction) => {
|
|
try {
|
|
schema.parse(req.body);
|
|
next();
|
|
} catch (error) {
|
|
if (error instanceof ZodError) {
|
|
const errors = error.errors.map(err => ({
|
|
field: err.path.join('.'),
|
|
message: err.message
|
|
}));
|
|
|
|
return res.status(400).json({
|
|
success: false,
|
|
error: 'Ошибка валидации данных',
|
|
details: errors
|
|
});
|
|
}
|
|
|
|
return res.status(400).json({
|
|
success: false,
|
|
error: 'Неверные данные запроса'
|
|
});
|
|
}
|
|
};
|
|
};
|
|
|
|
export const validateQuery = (schema: ZodSchema) => {
|
|
return (req: Request, res: Response, next: NextFunction) => {
|
|
try {
|
|
schema.parse(req.query);
|
|
next();
|
|
} catch (error) {
|
|
if (error instanceof ZodError) {
|
|
const errors = error.errors.map(err => ({
|
|
field: err.path.join('.'),
|
|
message: err.message
|
|
}));
|
|
|
|
return res.status(400).json({
|
|
success: false,
|
|
error: 'Ошибка валидации параметров',
|
|
details: errors
|
|
});
|
|
}
|
|
|
|
return res.status(400).json({
|
|
success: false,
|
|
error: 'Неверные параметры запроса'
|
|
});
|
|
}
|
|
};
|
|
};
|