fix(striker-ui-api): hoist regex patterns

main
Tsu-ba-me 3 years ago
parent f476524461
commit f88ac0387d
  1. 8
      striker-ui-api/src/lib/consts/REG_EXP_PATTERNS.ts
  2. 22
      striker-ui-api/src/lib/request_handlers/server/createServer.ts

@ -0,0 +1,8 @@
const partHex = '[0-9a-f]';
export const REP_INTEGER = /^\d+$/;
export const REP_UUID = new RegExp(
`^${partHex}{8}-${partHex}{4}-[1-5]${partHex}{3}-[89ab]${partHex}{3}-${partHex}{12}$`,
'i',
);

@ -2,6 +2,7 @@ import assert from 'assert';
import { RequestHandler } from 'express';
import { OS_LIST_MAP } from '../../consts/OS_LIST';
import { REP_INTEGER, REP_UUID } from '../../consts/REG_EXP_PATTERNS';
import SERVER_PATHS from '../../consts/SERVER_PATHS';
import { dbQuery, sub } from '../../accessModule';
@ -25,13 +26,6 @@ export const createServer: RequestHandler = ({ body }, response) => {
console.dir(body, { depth: null });
const rgHex = '[0-9a-f]';
const patternInteger = /^\d+$/;
const patterUUID = new RegExp(
`^${rgHex}{8}-${rgHex}{4}-[1-5]${rgHex}{3}-[89ab]${rgHex}{3}-${rgHex}{12}$`,
'i',
);
const dataServerName = String(serverName);
const dataOS = String(optimizeForOS);
const dataCPUCores = String(cpuCores);
@ -61,31 +55,31 @@ export const createServer: RequestHandler = ({ body }, response) => {
`Data OS not recognized; got [${dataOS}].`,
);
assert(
patternInteger.test(dataCPUCores),
REP_INTEGER.test(dataCPUCores),
`Data CPU cores can only contain digits; got [${dataCPUCores}].`,
);
assert(
patternInteger.test(dataRAM),
REP_INTEGER.test(dataRAM),
`Data RAM can only contain digits; got [${dataRAM}].`,
);
assert(
patterUUID.test(dataStorageGroupUUID),
REP_UUID.test(dataStorageGroupUUID),
`Data storage group UUID must be a valid UUID; got [${dataStorageGroupUUID}].`,
);
assert(
patternInteger.test(dataStorageSize),
REP_INTEGER.test(dataStorageSize),
`Data storage size can only contain digits; got [${dataStorageSize}].`,
);
assert(
patterUUID.test(dataInstallISO),
REP_UUID.test(dataInstallISO),
`Data install ISO must be a valid UUID; got [${dataInstallISO}].`,
);
assert(
dataDriverISO === 'none' || patterUUID.test(dataDriverISO),
dataDriverISO === 'none' || REP_UUID.test(dataDriverISO),
`Data driver ISO must be a valid UUID when provided; got [${dataDriverISO}].`,
);
assert(
patterUUID.test(dataAnvilUUID),
REP_UUID.test(dataAnvilUUID),
`Data anvil UUID must be a valid UUID; got [${dataAnvilUUID}].`,
);
} catch (assertError) {

Loading…
Cancel
Save