refactor: simplify validations

This commit is contained in:
Carlos Valente
2025-06-05 20:13:14 +02:00
committed by Carlos Valente
parent 9a62daf047
commit b1d23467a2
9 changed files with 33 additions and 39 deletions
@@ -2,7 +2,7 @@ import { Request, Response, NextFunction } from 'express';
import { body, validationResult } from 'express-validator';
export const validatePostCss = [
body('css').exists().isString().trim(),
body('css').isString().trim(),
(req: Request, res: Response, next: NextFunction) => {
const errors = validationResult(req);
@@ -18,7 +18,7 @@ import * as assert from '../../utils/assert.js';
import { isFilterOperator, isFilterRule, isOntimeActionAction } from './automation.utils.js';
export const paramContainsId = [
param('id').exists(),
param('id').isString().notEmpty(),
(req: Request, res: Response, next: NextFunction) => {
const errors = validationResult(req);
@@ -28,9 +28,9 @@ export const paramContainsId = [
];
export const validateAutomationSettings = [
body('enabledAutomations').exists().isBoolean(),
body('enabledOscIn').exists().isBoolean(),
body('oscPortIn').exists().isPort(),
body('enabledAutomations').isBoolean(),
body('enabledOscIn').isBoolean(),
body('oscPortIn').isPort(),
body('triggers').optional().isArray(),
body('triggers.*.title').optional().isString().trim(),
body('triggers.*.trigger').optional().isIn(timerLifecycleValues),
@@ -45,9 +45,9 @@ export const validateAutomationSettings = [
];
export const validateTrigger = [
body('title').exists().isString().trim(),
body('trigger').exists().isIn(timerLifecycleValues),
body('automationId').exists().isString().trim(),
body('title').isString().trim(),
body('trigger').isIn(timerLifecycleValues),
body('automationId').isString().trim(),
(req: Request, res: Response, next: NextFunction) => {
const errors = validationResult(req);
@@ -57,7 +57,7 @@ export const validateTrigger = [
];
export const validateTriggerPatch = [
param('id').exists(),
param('id').isString().notEmpty(),
body('title').optional().isString().trim(),
body('trigger').optional().isIn(timerLifecycleValues),
body('automationId').optional().isString().trim(),
@@ -80,7 +80,7 @@ export const validateAutomation = [
];
export const validateAutomationPatch = [
param('id').exists(),
param('id').isString().notEmpty(),
body().custom(parseAutomation),
(req: Request, res: Response, next: NextFunction) => {
@@ -140,7 +140,7 @@ function validateOutput(output: Array<unknown>): output is AutomationOutput[] {
}
export const validateTestPayload = [
body('type').exists().isIn(['osc', 'http', 'ontime']),
body('type').isIn(['osc', 'http', 'ontime']),
// validation for OSC message
oneOf([
@@ -80,7 +80,6 @@ export const validatePatchProject = [
*/
export const validateNewFilenameBody = [
body('newFilename')
.exists()
.isString()
.trim()
.customSanitizer((input: string) => sanitize(input))
@@ -104,7 +103,6 @@ export const validateNewFilenameBody = [
*/
export const validateFilenameBody = [
body('filename')
.exists()
.isString()
.trim()
.customSanitizer((input: string) => sanitize(input))
@@ -128,7 +126,6 @@ export const validateFilenameBody = [
*/
export const validateFilenameParam = [
param('filename')
.exists()
.isString()
.trim()
.customSanitizer((input: string) => sanitize(input))
@@ -14,7 +14,6 @@ export const validateFileExists = [
export const validateImportMapOptions = [
body('options')
.exists()
.isObject()
.custom((content) => {
return isImportMap(content);
@@ -2,7 +2,7 @@ import { body, param, validationResult } from 'express-validator';
import type { Request, Response, NextFunction } from 'express';
export const rundownPostValidator = [
body('type').isString().exists().isIn(['event', 'delay', 'block']),
body('type').isString().isIn(['event', 'delay', 'block']),
body('after').optional().isString(),
body('before').optional().isString(),
@@ -14,7 +14,7 @@ export const rundownPostValidator = [
];
export const rundownPutValidator = [
body('id').isString().exists(),
body('id').isString().notEmpty(),
(req: Request, res: Response, next: NextFunction) => {
const errors = validationResult(req);
@@ -24,7 +24,7 @@ export const rundownPutValidator = [
];
export const rundownBatchPutValidator = [
body('data').isObject().exists(),
body('data').isObject(),
body('ids').isArray().notEmpty(),
body('ids.*').isString(),
@@ -36,9 +36,9 @@ export const rundownBatchPutValidator = [
];
export const rundownReorderValidator = [
body('entryId').isString().exists(),
body('destinationId').isString().exists(),
body('order').isIn(['before', 'after', 'insert']).exists(),
body('entryId').isString().notEmpty(),
body('destinationId').isString().notEmpty(),
body('order').isIn(['before', 'after', 'insert']),
(req: Request, res: Response, next: NextFunction) => {
const errors = validationResult(req);
@@ -48,8 +48,8 @@ export const rundownReorderValidator = [
];
export const rundownSwapValidator = [
body('from').isString().exists(),
body('to').isString().exists(),
body('from').isString().notEmpty(),
body('to').isString().notEmpty(),
(req: Request, res: Response, next: NextFunction) => {
const errors = validationResult(req);
@@ -59,7 +59,7 @@ export const rundownSwapValidator = [
];
export const paramsMustHaveEntryId = [
param('entryId').exists(),
param('entryId').isString().notEmpty(),
(req: Request, res: Response, next: NextFunction) => {
const errors = validationResult(req);
@@ -2,10 +2,10 @@ import type { Request, Response, NextFunction } from 'express';
import { body, validationResult } from 'express-validator';
export const validateGenerateUrl = [
body('baseUrl').exists().isString().notEmpty().trim(),
body('path').exists().isString().trim(),
body('lock').exists().isBoolean(),
body('authenticate').exists().isBoolean(),
body('baseUrl').isString().trim().notEmpty(),
body('path').isString().trim().notEmpty(),
body('lock').isBoolean(),
body('authenticate').isBoolean(),
(req: Request, res: Response, next: NextFunction) => {
const errors = validationResult(req);
@@ -5,7 +5,7 @@ import type { Request, Response, NextFunction } from 'express';
* @description Validates object for POST /ontime/settings/welcomedialog
*/
export const validateWelcomeDialog = [
body('show').exists().isBoolean(),
body('show').isBoolean(),
(req: Request, res: Response, next: NextFunction) => {
const errors = validationResult(req);
if (!errors.isEmpty()) return res.status(422).json({ errors: errors.array() });
@@ -5,7 +5,6 @@ import { NextFunction, Request, Response } from 'express';
export const validateRequestConnection = [
param('sheetId')
.exists()
.isString()
.isLength({
min: 20,
@@ -25,7 +24,7 @@ export const validateRequestConnection = [
];
export const validateSheetId = [
param('sheetId').exists().isString(),
param('sheetId').isString(),
(req: Request, res: Response, next: NextFunction) => {
const errors = validationResult(req);
@@ -35,9 +34,8 @@ export const validateSheetId = [
];
export const validateSheetOptions = [
param('sheetId').exists().isString(),
param('sheetId').isString(),
body('options')
.exists()
.isObject()
.custom((content) => {
const isValid = isImportMap(content);
@@ -5,12 +5,12 @@ import { Request, Response, NextFunction } from 'express';
* @description Validates object for POST /ontime/views
*/
export const validateViewSettings = [
check('dangerColor').exists().isString().trim().withMessage('dangerColor value must be string'),
check('endMessage').exists().isString().trim().withMessage('endMessage value must be string'),
check('freezeEnd').exists().isBoolean().withMessage('freezeEnd value must be boolean'),
check('normalColor').exists().isString().trim().withMessage('normalColor value must be string'),
check('overrideStyles').exists().isBoolean().withMessage('overrideStyles value must be boolean'),
check('warningColor').exists().isString().trim().withMessage('warningColor value must be string'),
check('dangerColor').isString().trim().withMessage('dangerColor value must be string'),
check('endMessage').isString().trim().withMessage('endMessage value must be string'),
check('freezeEnd').isBoolean().withMessage('freezeEnd value must be boolean'),
check('normalColor').isString().trim().withMessage('normalColor value must be string'),
check('overrideStyles').isBoolean().withMessage('overrideStyles value must be boolean'),
check('warningColor').isString().trim().withMessage('warningColor value must be string'),
(req: Request, res: Response, next: NextFunction) => {
const errors = validationResult(req);