Skip to content

Commit

Permalink
Add validators for environment variables
Browse files Browse the repository at this point in the history
  • Loading branch information
leungkinghin-ct committed Aug 10, 2023
1 parent 5031a5e commit c9bb5c3
Show file tree
Hide file tree
Showing 7 changed files with 360 additions and 23 deletions.
14 changes: 7 additions & 7 deletions full-ingestion/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion full-ingestion/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@
"body-parser": "^1.20.1",
"dotenv": "^16.0.3",
"express": "4.18.2",
"validator": "^13.7.0"
"validator": "^13.11.0"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ function loadConfig() {
}

export const config = {
applicationId: loadConfig().applicationId,
searchApiKey: loadConfig().searchApiKey,
index: loadConfig().index,
applicationId: loadConfig()?.applicationId,
searchApiKey: loadConfig()?.searchApiKey,
index: loadConfig()?.index,
};
141 changes: 141 additions & 0 deletions full-ingestion/src/middlewares/validators/helpers.validators.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
import validator from 'validator';

/**
* File used to create helpers to validate the fields
*/

const required =
(fn) =>
(value, ...args) =>
!(value === undefined || value === null) && fn(...[String(value), ...args]);

export const standardString = (path, message, overrideConfig = {}) => [
path,
[
[
required(validator.isLength),
message,
[{ min: 2, max: 20, ...overrideConfig }],
],
],
];

export const standardEmail = (path, message) => [
path,
[[required(validator.isEmail), message]],
];

export const standardNaturalNumber = (path, message) => [
path,
[
[
required((value) =>
validator.isNumeric(String(value), { no_symbols: true })
),
message,
],
],
];

export const standardKey = (path, message) => [
path,
[
[
required(
(value) =>
validator.isLength(String(value), { min: 2 }) &&
/^[a-zA-Z0-9-_]+$/.test(value)
),

message,
],
],
];

export const standardUrl = (path, message, overrideOptions = {}) => [
path,
[
[
required(validator.isURL),
message,
[
{
require_protocol: true,
require_valid_protocol: true,
protocols: ['http', 'https'],
require_host: true,
require_port: false,
allow_protocol_relative_urls: false,
allow_fragments: false,
allow_query_components: true,
validate_length: true,
...overrideOptions,
},
],
],
],
];

export const getValidateMessages = (validatorConfigs, item) =>
validatorConfigs.flatMap(([path, validators]) => {
return validators.reduce((acc, [validatorFn, message, args = []]) => {
const valueToValidate = path.reduce((val, property) => {
return val[property];
}, item);
if (!validatorFn(...[valueToValidate, ...args])) {
return acc.concat(message);
}
return acc;
}, []);
});

export const optional =
(fn) =>
(...args) => {
const [path, validators] = fn(...args);
return [
path,
validators.map(([fn, message, validatorArgs]) => [
(value, ...args) =>
value === undefined ? true : fn(...[value, ...args]),
message,
validatorArgs,
]),
];
};

export const array =
(fn) =>
(...args) => {
const [path, validators] = fn(...args);
return [
path,
validators.map(([fn, message, validatorArgs]) => [
(value, ...args) =>
Array.isArray(value) &&
value.every((value) => fn(...[value, ...args])),
message,
validatorArgs,
]),
];
};

export const region = (path, message) => [
path,
[
[
required(
required((value) =>
validator.isIn(value, [
'us-central1.gcp',
'us-east-2.aws',
'europe-west1.gcp',
'eu-central-1.aws',
'australia-southeast1.gcp',
])
)
),
message,
],
],
];
24 changes: 12 additions & 12 deletions full-ingestion/src/utils/config.utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// import CustomError from '../errors/custom.error.js';
// import envValidators from '../validators/env.validators.js';
// import { getValidateMessages } from '../validators/helpers.validators.js';
import CustomError from '../errors/custom.error.js';
import envValidators from '../validators/env-var.validators.js';
import { getValidateMessages } from '../validators/helpers.validators.js';

/**
* Read the configuration env vars
Expand All @@ -18,15 +18,15 @@ export const readConfiguration = () => {
region: process.env.CTP_REGION,
};

// const validationErrors = getValidateMessages(envValidators, envVars);
//
// if (validationErrors.length) {
// throw new CustomError(
// 'InvalidEnvironmentVariablesError',
// 'Invalid Environment Variables please check your .env file',
// validationErrors
// );
// }
const validationErrors = getValidateMessages(envValidators, envVars);

if (validationErrors.length) {
throw new CustomError(
'InvalidEnvironmentVariablesError',
'Invalid Environment Variables please check your .env file',
validationErrors
);
}

return envVars;
};
55 changes: 55 additions & 0 deletions full-ingestion/src/validators/env-var.validators.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import {
optional,
standardString,
standardKey,
region,
} from './helpers.validators.js';

/**
* Create here your own validators
*/
const envValidators = [
standardString(
['clientId'],
{
code: 'InValidClientId',
message: 'Client id should be 24 characters.',
referencedBy: 'environmentVariables',
},
{ min: 24, max: 24 }
),

standardString(
['clientSecret'],
{
code: 'InvalidClientSecret',
message: 'Client secret should be 32 characters.',
referencedBy: 'environmentVariables',
},
{ min: 32, max: 32 }
),

standardKey(['projectKey'], {
code: 'InvalidProjectKey',
message: 'Project key should be a valid string.',
referencedBy: 'environmentVariables',
}),

optional(standardString)(
['scope'],
{
code: 'InvalidScope',
message: 'Scope should be at least 2 characters long.',
referencedBy: 'environmentVariables',
},
{ min: 2, max: undefined }
),

region(['region'], {
code: 'InvalidRegion',
message: 'Not a valid region.',
referencedBy: 'environmentVariables',
}),
];

export default envValidators;
Loading

0 comments on commit c9bb5c3

Please sign in to comment.