Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

utils: Introduce validationUtils namespace & character length validation logic #1656

Merged
merged 17 commits into from
Jan 29, 2024

Conversation

MicroFish91
Copy link
Contributor

@MicroFish91 MicroFish91 commented Jan 2, 2024

Beginning of a series of PRs to add a validation utility to simplify common input string validation scenarios for use in our Azure Wizard inputBox steps.

Example of use:
image

See: microsoft/vscode-azurecontainerapps#582

Let me know if you think something like this would be useful :)

@MicroFish91 MicroFish91 requested a review from a team as a code owner January 2, 2024 18:38
* @property `lowerLimitIncl`: The minimum size of the range (inclusive)
* @property `upperLimitIncl`: The maximum size of the range (inclusive)
*/
export interface RangeConstraints {
Copy link
Contributor Author

@MicroFish91 MicroFish91 Jan 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kept this named more generically because I was planning to reuse this interface again later for another validation method covering numeric range

@bwateratmsft bwateratmsft changed the title Introduce validationUtils namespace & character length validation logic utils: Introduce validationUtils namespace & character length validation logic Jan 8, 2024
export function hasValidCharLength(value: string, rc?: RangeConstraints): boolean {
const lowerLimitIncl = (!rc?.lowerLimitIncl || rc.lowerLimitIncl < 1) ? 1 : rc.lowerLimitIncl;
const upperLimitIncl = (!rc?.upperLimitIncl || rc.upperLimitIncl > Number.MAX_SAFE_INTEGER) ? Number.MAX_SAFE_INTEGER : rc.upperLimitIncl;
return lowerLimitIncl <= upperLimitIncl && value.length >= lowerLimitIncl && value.length <= upperLimitIncl;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like that this should just throw an error: lowerLimitIncl <= upperLimitIncl. If the upperLimit is lower than the lowerLimit, somebody created the RangeConstraints backwards and that'd be kind of difficult to understand what's going wrong.

utils/src/utils/validationUtils.ts Show resolved Hide resolved
@MicroFish91
Copy link
Contributor Author

Added a message condition for when the lower and upper limits are equal*

@MicroFish91 MicroFish91 force-pushed the mwf/validation-utils-1 branch from 5c15fb3 to 57c3b52 Compare January 26, 2024 18:48
@MicroFish91 MicroFish91 force-pushed the mwf/validation-utils-1 branch from 57c3b52 to 81fa32b Compare January 26, 2024 22:32
@MicroFish91 MicroFish91 merged commit f8bc1c2 into main Jan 29, 2024
4 checks passed
@MicroFish91 MicroFish91 deleted the mwf/validation-utils-1 branch January 29, 2024 18:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants