generated from salesforcecli/plugin-template-sf
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: handle empty strings to prevent unintended conversion to
0
(#469)
- Loading branch information
Showing
4 changed files
with
114 additions
and
22 deletions.
There are no files selected for viewing
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
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,26 @@ | ||
/* | ||
* Copyright (c) 2023, salesforce.com, inc. | ||
* All rights reserved. | ||
* Licensed under the BSD 3-Clause license. | ||
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause | ||
*/ | ||
|
||
import { Messages } from '@salesforce/core'; | ||
|
||
/** | ||
* handle empty string as valid answer, make it mean undefined. | ||
* | ||
* relies on the i being validate to be convertible to an integer using the prompt `validate` function | ||
*/ | ||
export const toOptionalNumber = (i: string): number | undefined => (i.length === 0 ? undefined : parseInt(i, 10)); | ||
|
||
/** validation function for integers */ | ||
export const integerMinMaxValidation = (num: number, min?: number, max?: number): boolean | string => { | ||
Messages.importMessagesDirectoryFromMetaUrl(import.meta.url); | ||
const messages = Messages.loadMessages('@salesforce/plugin-dev', 'dev.generate.flag'); | ||
|
||
if (!Number.isInteger(num)) return messages.getMessage('error.InvalidInteger'); | ||
if (min !== undefined && num < min) return messages.getMessage('error.InvalidDefaultInteger'); | ||
if (max !== undefined && num > max) return messages.getMessage('error.InvalidDefaultInteger'); | ||
return true; | ||
}; |
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,74 @@ | ||
/* | ||
* Copyright (c) 2023, salesforce.com, inc. | ||
* All rights reserved. | ||
* Licensed under the BSD 3-Clause license. | ||
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause | ||
*/ | ||
|
||
import { expect } from 'chai'; | ||
import { integerMinMaxValidation, toOptionalNumber } from '../../../src/prompts/validations.js'; | ||
|
||
describe('integerMinMaxValidation', () => { | ||
it('good int', () => { | ||
expect(integerMinMaxValidation(1)).to.be.true; | ||
expect(integerMinMaxValidation(0)).to.be.true; | ||
expect(integerMinMaxValidation(-1)).to.be.true; | ||
expect(integerMinMaxValidation(Number.MAX_SAFE_INTEGER)).to.be.true; | ||
}); | ||
|
||
it('not int', () => { | ||
expect(integerMinMaxValidation(1.1)).to.be.a('string'); | ||
expect(integerMinMaxValidation(-1.1)).to.be.a('string'); | ||
expect(integerMinMaxValidation(NaN)).to.be.a('string'); | ||
}); | ||
|
||
it('min ok', () => { | ||
expect(integerMinMaxValidation(0, undefined)).to.be.true; | ||
expect(integerMinMaxValidation(1, undefined)).to.be.true; | ||
expect(integerMinMaxValidation(-1, undefined)).to.be.true; | ||
expect(integerMinMaxValidation(0, 0)).to.be.true; | ||
expect(integerMinMaxValidation(5, 1)).to.be.true; | ||
expect(integerMinMaxValidation(-1, -5)).to.be.true; | ||
expect(integerMinMaxValidation(-1, -1)).to.be.true; | ||
}); | ||
|
||
it('min not ok', () => { | ||
expect(integerMinMaxValidation(0, 1)).to.be.a('string'); | ||
expect(integerMinMaxValidation(1, 2)).to.be.a('string'); | ||
expect(integerMinMaxValidation(-1, 0)).to.be.a('string'); | ||
}); | ||
|
||
it('min max ok', () => { | ||
expect(integerMinMaxValidation(0, undefined, undefined)).to.be.true; | ||
expect(integerMinMaxValidation(1, undefined, 2)).to.be.true; | ||
expect(integerMinMaxValidation(-1, undefined, 0)).to.be.true; | ||
expect(integerMinMaxValidation(0, 0, 0)).to.be.true; | ||
expect(integerMinMaxValidation(2, 2, 2)).to.be.true; | ||
expect(integerMinMaxValidation(1, 0, 2)).to.be.true; | ||
}); | ||
|
||
it('min max not ok', () => { | ||
expect(integerMinMaxValidation(5, 1, 3)).to.be.a('string'); | ||
expect(integerMinMaxValidation(1, 2, 3)).to.be.a('string'); | ||
expect(integerMinMaxValidation(-1, 0)).to.be.a('string'); | ||
expect(integerMinMaxValidation(1, 2, 0)).to.be.a('string'); | ||
expect(integerMinMaxValidation(1, 2, 1)).to.be.a('string'); | ||
}); | ||
}); | ||
|
||
describe('toOptionalNumber', () => { | ||
it('empty string => undefined', () => { | ||
expect(toOptionalNumber('')).to.be.undefined; | ||
}); | ||
|
||
it('not a number', () => { | ||
expect(toOptionalNumber('foo')).to.be.NaN; | ||
}); | ||
|
||
it('number string', () => { | ||
expect(toOptionalNumber('1')).to.equal(1); | ||
expect(toOptionalNumber('0')).to.equal(0); | ||
expect(toOptionalNumber('-1')).to.equal(-1); | ||
expect(toOptionalNumber('9007199254740991')).to.equal(9007199254740991); | ||
}); | ||
}); |