Skip to content

Commit

Permalink
use regex to validate system codes instead of making HTTP calls to bi…
Browse files Browse the repository at this point in the history
…z ops system codes
  • Loading branch information
kiyaGu authored and notlee committed Apr 26, 2024
1 parent 7c43167 commit ec779d1
Showing 1 changed file with 8 additions and 28 deletions.
36 changes: 8 additions & 28 deletions lib/middleware/v3/parseSystemCodeParameter.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

const UserError = require('../../utils/usererror');
const axios = require('axios').default;

module.exports = {
parseSystemCodeParameter: parseSystemCodeParameter
Expand Down Expand Up @@ -33,40 +32,21 @@ async function parseSystemCodeParameter(systemCode) {
throw new UserError('The system_code query parameter can not be empty.');
}

if (await isValidSystemCode(systemCode)) {
if (isValidSystemCode(systemCode)) {
return systemCode;
} else {
throw new UserError('The system_code query parameter must be a valid Biz-Ops System Code.');
}
};

/**
* Confirm whether a system-code exists within Biz-Ops.
* Confirm whether a system-code matches the format of a Biz-Ops system code.
*
* @param {string} code The system-code to check exists in Biz-Ops.
* @returns {Promise<boolean>} Returns true if the system-code exists in Biz-Ops and otherwise returns false.
* @param {string} code The system-code to check for validity.
* @returns {boolean} Returns true if the system-code is a valid Biz Ops system-code and otherwise returns false.
*/
async function isValidSystemCode(code) {
try {
const response = await axios({
url: 'https://system-codes.in.ft.com/v1/check',
method: 'get', // default
params: {
systemCode: code
},
timeout: 1000 * 10,
responseType: 'json'
});

if (response.status === 200 && response.data.exists !== true) {
return false;
}

return true;
} catch (error) {
// If the request fails for any reason, we assume the code was a valid Biz-Ops system code.
// This ensures that if the https://system-codes.in.ft.com/ system is offline we can still
// serve responses to our users.
return true;
}
function isValidSystemCode(code) {
// The pattern is taken from Biz-ops-schema which sets the valid system code pattern.
// https://github.com/Financial-Times/biz-ops-schema/blob/9babf227d2cd10bdae33821a2cd10b55c8e95856/schema/string-patterns.yaml#L3
return /^(?=.{2,64}$)[a-z0-9]+(?:-[a-z0-9]+)*$/.test(code);
}

0 comments on commit ec779d1

Please sign in to comment.