Skip to content

Commit

Permalink
fix: add new validateParams function (#180)
Browse files Browse the repository at this point in the history
This commit adds the "validateParams" function
to the node core.  This function will be used
by the operation code generated by the Node
SDK generator to validate the parameters found
within a "params" object.  It will check for
required parameters that are not present, and also
check to make sure that all parameters that ARE present
are valid (i.e. the param name is among the expected
valid parameter names for the operation).
  • Loading branch information
padamstx authored Dec 6, 2021
1 parent 3a912c6 commit 514d187
Show file tree
Hide file tree
Showing 5 changed files with 294 additions and 17,483 deletions.
2 changes: 1 addition & 1 deletion .secrets.baseline
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"files": "package-lock.json|^.secrets.baseline$",
"lines": null
},
"generated_at": "2021-11-12T14:35:43Z",
"generated_at": "2021-12-05T15:56:18Z",
"plugins_used": [
{
"name": "AWSKeyDetector"
Expand Down
5 changes: 5 additions & 0 deletions etc/ibm-cloud-sdk-core.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,11 @@ export interface UserOptions {
// @public (undocumented)
export function validateInput(options: any, requiredOptions: string[]): void;

// @public
export function validateParams(params: {
[key: string]: any;
}, requiredParams: string[], allParams: string[]): null | Error;

// @public
export class VpcInstanceAuthenticator extends TokenRequestBasedAuthenticator {
// Warning: (ae-forgotten-export) The symbol "Options" needs to be exported by the entry point index.d.ts
Expand Down
60 changes: 60 additions & 0 deletions lib/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,66 @@ export function getMissingParams(params: { [key: string]: any }, requires: strin
: null;
}

/**
* Validates that "params" contains a value for each key listed in "requiredParams",
* and that each key contained in "params" is a valid key listed in "allParams".
* In essence, we want params to contain only valid keys and we want params
* to contain at least the required keys.
*
* @param params - the "params" object passed into an operation containing method parameters.
* @param requiredParams - the names of required parameters.
* If null, then the "required params" check is bypassed.
* @param allParams - the names of all valid parameters.
* If null, then the "valid params" check is bypassed.
* @returns {Error|null}
*/
export function validateParams(
params: { [key: string]: any },
requiredParams: string[],
allParams: string[]
): null | Error {
let missing = [];
const invalid = [];

// If there are any required fields, then make sure they are present in "params".
if (requiredParams) {
if (!params) {
missing = requiredParams;
} else {
requiredParams.forEach((require) => {
if (isMissing(params[require])) {
missing.push(require);
}
});
}
}

// Make sure that each field specified in "params" is a valid param.
if (allParams && params) {
Object.keys(params).forEach((key) => {
if (!allParams.includes(key)) {
invalid.push(key);
}
});
}

// If no errors found, then bail now.
if (missing.length === 0 && invalid.length === 0) {
return null;
}

// Return an Error object identifying the errors we found.
let errorMsg = 'Parameter validation errors:';
if (missing.length > 0) {
errorMsg += `\n Missing required parameters: ${missing.join(', ')}`;
}
if (invalid.length > 0) {
errorMsg += `\n Found invalid parameters: ${invalid.join(', ')}`;
}

return new Error(errorMsg);
}

/**
* Returns true if value is determined to be "missing". Currently defining "missing"
* as `undefined`, `null`, or the empty string.
Expand Down
Loading

0 comments on commit 514d187

Please sign in to comment.