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