Skip to content

Commit

Permalink
fix: improved validation, error message, add test
Browse files Browse the repository at this point in the history
  • Loading branch information
WillieRuemmele committed Feb 4, 2021
1 parent 1455204 commit 63e6c4d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
2 changes: 1 addition & 1 deletion messages/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"InvalidApiVersion": "Specify a valid Salesforce API version, for example, 42.0",
"InvalidIsvDebuggerSid": "Specify a valid Debugger SID",
"InvalidIsvDebuggerUrl": "Specify a valid Debugger URL",
"InvalidNumberConfigValue": "Specify a valid number, for example, 150000",
"InvalidNumberConfigValue": "Specify a valid positive integer, for example, 150000",
"InvalidBooleanConfigValue": "The config value can only be set to true or false.",
"InvalidProjectWorkspace": "This directory does not contain a valid Salesforce DX project",
"SchemaValidationWarning": "The config file: %s is not schema valid\nDue to: %s",
Expand Down
4 changes: 3 additions & 1 deletion src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,9 @@ export class Config extends ConfigFile<ConfigFile.Options> {
{
key: Config.MAX_QUERY_LIMIT,
input: {
validator: (value) => !isNaN(Number(value)),
// the bit shift will remove the negative bit, and any decimal numbers
// then the parseFloat will handle converting it to a number from a string
validator: (value) => (value as number) >>> 0 === parseFloat(value as string),
get failedMessage() {
return Config.messages?.getMessage('InvalidNumberConfigValue');
},
Expand Down
29 changes: 29 additions & 0 deletions test/unit/config/configTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,35 @@ describe('Config', () => {
expect(err).to.have.property('name', 'InvalidConfigValue');
}
});

it('will throw an error when value is negative', async () => {
const config: Config = await Config.create(Config.getDefaultOptions(true));
try {
config.set('maxQueryLimit', '-123');
assert.fail('Expected an error to be thrown.');
} catch (err) {
expect(err).to.have.property('name', 'InvalidConfigValue');
}
});

it('will throw an error when value is negative decimal', async () => {
const config: Config = await Config.create(Config.getDefaultOptions(true));
try {
config.set('maxQueryLimit', '-123.456');
assert.fail('Expected an error to be thrown.');
} catch (err) {
expect(err).to.have.property('name', 'InvalidConfigValue');
}
});
it('will throw an error when value is negative integer', async () => {
const config: Config = await Config.create(Config.getDefaultOptions(true));
try {
config.set('maxQueryLimit', '-123');
assert.fail('Expected an error to be thrown.');
} catch (err) {
expect(err).to.have.property('name', 'InvalidConfigValue');
}
});
it('will set config value with stringified number', async () => {
const config: Config = await Config.create(Config.getDefaultOptions(true));
const res = config.set('maxQueryLimit', '123');
Expand Down

0 comments on commit 63e6c4d

Please sign in to comment.