-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add validators for environment variables
- Loading branch information
1 parent
5031a5e
commit c9bb5c3
Showing
7 changed files
with
360 additions
and
23 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
141 changes: 141 additions & 0 deletions
141
full-ingestion/src/middlewares/validators/helpers.validators.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
], | ||
], | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Oops, something went wrong.