Skip to content

Commit b6c4950

Browse files
authored
Consolidate use of input box validation (#3049)
* use positive integer validator for set nax table head depth * move other input validation into input box file * update naming
1 parent 1ece29b commit b6c4950

File tree

3 files changed

+45
-24
lines changed

3 files changed

+45
-24
lines changed

extension/src/experiments/model/filterBy/quickPick.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { FilterDefinition, getFilterId, isDateOperator, Operator } from '.'
22
import { definedAndNonEmpty } from '../../../util/array'
3-
import { getIsoDate, isFreeTextDate } from '../../../util/date'
4-
import { getInput, getValidInput } from '../../../vscode/inputBox'
3+
import { getInput, getValidDateInput } from '../../../vscode/inputBox'
54
import { quickPickManyValues, quickPickValue } from '../../../vscode/quickPick'
65
import { Title } from '../../../vscode/title'
76
import { Toast } from '../../../vscode/toast'
@@ -92,14 +91,7 @@ export const OPERATORS = [
9291

9392
const getValue = (operator: Operator): Thenable<string | undefined> => {
9493
if (isDateOperator(operator)) {
95-
return getValidInput(
96-
Title.ENTER_FILTER_VALUE,
97-
(text?: string): null | string =>
98-
isFreeTextDate(text)
99-
? null
100-
: 'please enter a valid date of the form yyyy-mm-dd',
101-
{ value: getIsoDate() }
102-
)
94+
return getValidDateInput(Title.ENTER_FILTER_VALUE)
10395
}
10496
return getInput(Title.ENTER_FILTER_VALUE)
10597
}

extension/src/experiments/webview/messages.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { splitColumnPath } from '../columns/paths'
1818
import { ExperimentsModel } from '../model'
1919
import { SortDefinition } from '../model/sortBy'
2020
import { CheckpointsModel } from '../checkpoints/model'
21-
import { getValidInput } from '../../vscode/inputBox'
21+
import { getPositiveIntegerInput } from '../../vscode/inputBox'
2222
import { Title } from '../../vscode/title'
2323
import { ConfigKey, setConfigValue } from '../../vscode/config'
2424

@@ -174,12 +174,10 @@ export class WebviewMessages {
174174
}
175175

176176
private async setMaxTableHeadDepth() {
177-
const newValue = await getValidInput(
177+
const newValue = await getPositiveIntegerInput(
178178
Title.SET_EXPERIMENTS_HEADER_HEIGHT,
179-
val => {
180-
return Number.isNaN(Number(val)) ? 'Input needs to be a number' : ''
181-
},
182-
{ prompt: 'Use 0 for infinite height.' }
179+
{ prompt: 'Use 0 for infinite height.', value: '0' },
180+
true
183181
)
184182

185183
if (!newValue) {

extension/src/vscode/inputBox.ts

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,58 @@
11
import { window } from 'vscode'
22
import { Title } from './title'
33
import { isValidStringInteger } from '../util/number'
4+
import { getIsoDate, isFreeTextDate } from '../util/date'
45

5-
export const getInput = (title: Title, value?: string) =>
6+
export const getInput = (
7+
title: Title,
8+
value?: string
9+
): Thenable<string | undefined> =>
610
window.showInputBox({
711
title,
812
value
913
})
1014

11-
export const getValidInput = (
15+
const getValidInput = (
1216
title: Title,
1317
validateInput: (text?: string) => null | string,
1418
options?: { prompt?: string; value?: string }
15-
) =>
19+
): Thenable<string | undefined> =>
1620
window.showInputBox({
1721
prompt: options?.prompt,
1822
title,
1923
validateInput,
2024
value: options?.value
2125
})
2226

27+
const isPositiveInteger = (
28+
input: string | undefined,
29+
includeZero: boolean | undefined
30+
): boolean => {
31+
if (!isValidStringInteger(input)) {
32+
return false
33+
}
34+
35+
const number = Number(input)
36+
37+
if (!includeZero) {
38+
return number > 0
39+
}
40+
return number >= 0
41+
}
42+
2343
export const getPositiveIntegerInput = async (
2444
title: Title,
25-
options: { prompt: string; value: string }
26-
) => {
45+
options: { prompt: string; value: string },
46+
includeZero?: boolean
47+
): Promise<string | undefined> => {
2748
const input = await getValidInput(
2849
title,
29-
val => {
30-
if (isValidStringInteger(val) && Number(val) > 0) {
50+
input => {
51+
if (isPositiveInteger(input, includeZero)) {
3152
return ''
3253
}
3354

34-
return 'Input needs to be a positive integer'
55+
return `please enter a positive integer${includeZero ? ' or 0' : ''}`
3556
},
3657
options
3758
)
@@ -41,3 +62,13 @@ export const getPositiveIntegerInput = async (
4162
}
4263
return Number.parseInt(input).toString()
4364
}
65+
66+
export const getValidDateInput = (title: Title): Thenable<string | undefined> =>
67+
getValidInput(
68+
title,
69+
(text?: string): null | string =>
70+
isFreeTextDate(text)
71+
? null
72+
: 'please enter a valid date of the form yyyy-mm-dd',
73+
{ value: getIsoDate() }
74+
)

0 commit comments

Comments
 (0)