Skip to content

Commit

Permalink
Validate user inputs CLI (#150)
Browse files Browse the repository at this point in the history
* Validate user inputs

* Fix lint
  • Loading branch information
felipe.fuerback authored Apr 11, 2024
1 parent d0840f5 commit 8009c86
Showing 1 changed file with 47 additions and 1 deletion.
48 changes: 47 additions & 1 deletion chain-cli/src/commands/network-up/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,53 @@ function updatedFabloConfigWithEntry(
}

function customValidation(flags: any): void {

Check warning on line 235 in chain-cli/src/commands/network-up/index.ts

View workflow job for this annotation

GitHub Actions / CI

Unexpected any. Specify a different type
const { channel, channelType, chaincodeName, chaincodeDir } = flags;
const { channel, channelType, chaincodeName, chaincodeDir, envConfig } = flags;
console.log(flags);

/*
Check if the flags does not have special characters like &, |, ;, :, etc. Only -, _ and . and are allowed
Check the maximum length of the flag is 64 characters
*/
const specialChars = /[&\\#,+()$~%'":;*?<>@{}|]/;
const maxLength = 64;

// Transform envConfig to array to use the same validation
const envConfigArray = [envConfig];

const invalidFlags = [channel, channelType, chaincodeName, chaincodeDir, envConfigArray].reduce(
(acc: string[], arr: string[]) => [
...acc,
...arr.filter((flag: string) => {
if (flag.length > maxLength) {
throw new Error(`Error: Flag ${flag} is too long. Maximum length is ${maxLength} characters.`);
}
console.log(flag);
console.log(specialChars.test(flag));
if (specialChars.test(flag)) {
throw new Error(`Error: Flag ${flag} contains special characters. Only - and _ are allowed.`);
}
return false;
})
],
[]
);
if (invalidFlags.length) {
throw new Error(`Error: Found invalid flags: ${invalidFlags.join(", ")}`);
}

/*
Check if chaincodeDir and envConfig are valid paths
*/
if (chaincodeDir) {
chaincodeDir.forEach((dir: string) => {
if (!fs.existsSync(dir)) {
throw new Error(`Error: Chaincode directory ${dir} does not exist.`);
}
});
}
if (envConfig && !fs.existsSync(envConfig)) {
throw new Error(`Error: Env config file ${envConfig} does not exist.`);
}

/*
The same number of parameters for chaincode, channelTyle, chaincode and chaincodeDir is required
Expand Down

0 comments on commit 8009c86

Please sign in to comment.