Clarify param parser handling positive integers
This commit is contained in:
parent
297f7ccf5f
commit
928eed6671
@ -1,6 +1,6 @@
|
||||
import express from 'express';
|
||||
|
||||
import { parseIntParam, processParam } from '../parameters';
|
||||
import { parsePositiveIntParam, processParam } from '../parameters';
|
||||
import asyncController from '../routes/asyncController';
|
||||
import * as buildingService from '../services/building';
|
||||
import * as userService from '../services/user';
|
||||
@ -35,7 +35,7 @@ const getBuildingsByReference = asyncController(async (req: express.Request, res
|
||||
|
||||
// GET individual building, POST building updates
|
||||
const getBuildingById = asyncController(async (req: express.Request, res: express.Response) => {
|
||||
const buildingId = processParam(req.params, 'building_id', parseIntParam, true);
|
||||
const buildingId = processParam(req.params, 'building_id', parsePositiveIntParam, true);
|
||||
|
||||
try {
|
||||
const result = await buildingService.getBuildingById(buildingId);
|
||||
@ -63,7 +63,7 @@ const updateBuildingById = asyncController(async (req: express.Request, res: exp
|
||||
});
|
||||
|
||||
async function updateBuilding(req: express.Request, res: express.Response, userId: string) {
|
||||
const buildingId = processParam(req.params, 'building_id', parseIntParam, true);
|
||||
const buildingId = processParam(req.params, 'building_id', parsePositiveIntParam, true);
|
||||
|
||||
const buildingUpdate = req.body;
|
||||
|
||||
@ -84,7 +84,7 @@ async function updateBuilding(req: express.Request, res: express.Response, userI
|
||||
|
||||
// GET building UPRNs
|
||||
const getBuildingUPRNsById = asyncController(async (req: express.Request, res: express.Response) => {
|
||||
const buildingId = processParam(req.params, 'building_id', parseIntParam, true);
|
||||
const buildingId = processParam(req.params, 'building_id', parsePositiveIntParam, true);
|
||||
|
||||
try {
|
||||
const result = await buildingService.getBuildingUPRNsById(buildingId);
|
||||
@ -105,7 +105,7 @@ const getBuildingLikeById = asyncController(async (req: express.Request, res: ex
|
||||
return res.send({ like: false }); // not logged in, so cannot have liked
|
||||
}
|
||||
|
||||
const buildingId = processParam(req.params, 'building_id', parseIntParam, true);
|
||||
const buildingId = processParam(req.params, 'building_id', parsePositiveIntParam, true);
|
||||
|
||||
try {
|
||||
const like = await buildingService.getBuildingLikeById(buildingId, req.session.user_id);
|
||||
@ -118,7 +118,7 @@ const getBuildingLikeById = asyncController(async (req: express.Request, res: ex
|
||||
});
|
||||
|
||||
const getBuildingEditHistoryById = asyncController(async (req: express.Request, res: express.Response) => {
|
||||
const buildingId = processParam(req.params, 'building_id', parseIntParam, true);
|
||||
const buildingId = processParam(req.params, 'building_id', parsePositiveIntParam, true);
|
||||
|
||||
try {
|
||||
const editHistory = await buildingService.getBuildingEditHistory(buildingId);
|
||||
@ -134,7 +134,7 @@ const updateBuildingLikeById = asyncController(async (req: express.Request, res:
|
||||
return res.send({ error: 'Must be logged in' });
|
||||
}
|
||||
|
||||
const buildingId = processParam(req.params, 'building_id', parseIntParam, true);
|
||||
const buildingId = processParam(req.params, 'building_id', parsePositiveIntParam, true);
|
||||
const { like } = req.body;
|
||||
|
||||
try {
|
||||
|
@ -1,6 +1,6 @@
|
||||
import express from 'express';
|
||||
|
||||
import { parseIntParam, processParam } from '../parameters';
|
||||
import { parsePositiveIntParam, processParam } from '../parameters';
|
||||
import asyncController from '../routes/asyncController';
|
||||
import * as dataExtractService from '../services/dataExtract';
|
||||
|
||||
@ -15,7 +15,7 @@ const getAllDataExtracts = asyncController(async function(req: express.Request,
|
||||
});
|
||||
|
||||
const getDataExtract = asyncController(async function(req: express.Request, res: express.Response) {
|
||||
const extractId = processParam(req.params, 'extract_id', parseIntParam, true);
|
||||
const extractId = processParam(req.params, 'extract_id', parsePositiveIntParam, true);
|
||||
|
||||
try {
|
||||
const extract = await dataExtractService.getDataExtractById(extractId);
|
||||
|
@ -1,6 +1,11 @@
|
||||
import { EditHistoryEntry } from '../../../frontend/models/edit-history-entry';
|
||||
import { numAsc, numDesc } from '../../../helpers';
|
||||
|
||||
/**
|
||||
* Create an object mocking all method of editHistory dataAccess
|
||||
* The type is set to reflect the type of that module, with added methods
|
||||
* used when testing
|
||||
*/
|
||||
const mockEditHistory =
|
||||
jest.genMockFromModule('../editHistory') as typeof import('../editHistory') & {
|
||||
__setHistory: (mockHistoryData: EditHistoryEntry[]) => void
|
||||
@ -44,7 +49,6 @@ const {
|
||||
__setHistory,
|
||||
getHistoryAfterId,
|
||||
getHistoryBeforeId,
|
||||
getLatestHistory,
|
||||
getIdNewerThan,
|
||||
getIdOlderThan
|
||||
} = mockEditHistory;
|
||||
@ -53,7 +57,6 @@ export {
|
||||
__setHistory,
|
||||
getHistoryAfterId,
|
||||
getHistoryBeforeId,
|
||||
getLatestHistory,
|
||||
getIdNewerThan,
|
||||
getIdOlderThan
|
||||
};
|
||||
|
@ -23,12 +23,12 @@ export function processParam<T>(params: object, paramName: string, processingFn:
|
||||
}
|
||||
}
|
||||
|
||||
export function parseIntParam(param: string) {
|
||||
export function parsePositiveIntParam(param: string) {
|
||||
if(param == undefined) return undefined;
|
||||
|
||||
const result = strictParseInt(param);
|
||||
if (isNaN(result)) {
|
||||
throw new ParamInvalidFormatError('Invalid format: not an integer');
|
||||
throw new ParamInvalidFormatError('Invalid format: not a positive integer');
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user