Skip to content

Commit

Permalink
fix: duplicate validation
Browse files Browse the repository at this point in the history
  • Loading branch information
cpvalente committed Jun 23, 2024
1 parent b7c48f3 commit 57f47f2
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
3 changes: 2 additions & 1 deletion apps/server/src/api-data/db/db.router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
validatePatchProject,
validateFilenameBody,
validateFilenameParam,
validateNewFilenameBody,
} from './db.validation.js';

export const router = express.Router();
Expand All @@ -31,7 +32,7 @@ router.post('/new', validateFilenameBody, validateNewProject, createProjectFile)
router.get('/all', listProjects);

router.post('/load', validateFilenameBody, loadProject);
router.post('/:filename/duplicate', validateFilenameParam, validateFilenameBody, duplicateProjectFile);
router.post('/:filename/duplicate', validateFilenameParam, validateNewFilenameBody, duplicateProjectFile);
router.put('/:filename/rename', validateFilenameParam, validateFilenameBody, renameProjectFile);
router.delete('/:filename', validateFilenameParam, deleteProjectFile);

Expand Down
24 changes: 24 additions & 0 deletions apps/server/src/api-data/db/db.validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,30 @@ export const validatePatchProject = [
},
];

/**
* @description Validates request with newFilename in the body.
*/
export const validateNewFilenameBody = [
body('newFilename')
.exists()
.isString()
.trim()
.customSanitizer((input: string) => sanitize(input))
.withMessage('Failed to sanitize the filename')
.notEmpty()
.withMessage('Filename was empty or contained only invalid characters')
.customSanitizer((input: string) => ensureJsonExtension(input)),

(req: Request, res: Response, next: NextFunction) => {
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(422).json({ errors: errors.array() });
}

next();
},
];

/**
* @description Validates request with filename in the body.
*/
Expand Down

0 comments on commit 57f47f2

Please sign in to comment.