Skip to content

Commit 28c7cc2

Browse files
author
Schmidely Stéphane
committed
Add global Export validator
1 parent 233ddc9 commit 28c7cc2

5 files changed

Lines changed: 52 additions & 11 deletions

File tree

src/utils/commands/export/constants.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ class Constants {
6060
interactive: flags.boolean({
6161
char: 'i',
6262
description: 'interactive mode',
63-
multiple: false,
6463
default: false,
6564
required: false
6665
})
Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,55 @@
11
import DateValidator from './validators/date-validator'
22
import FormatValidator from './validators/format-validator'
33
import ProjectValidator from './validators/project-validator'
4+
import SystemRequirementValidator from './validators/system-requirement-validator'
5+
6+
export default class ExportValidator {
7+
private executables = [
8+
{ name: 'Taskwarrior', cmd: 'task' },
9+
{ name: 'Timewarrior', cmd: 'timew' }
10+
]
11+
12+
/**
13+
* Check that Taskwarrior and Timewarrior are installed
14+
*/
15+
public checkRequirements(): Array<string> {
16+
return this.executables.reduce((acc: Array<any>, executable) => {
17+
const installed = new SystemRequirementValidator().isValid(executable)
18+
return installed ? acc : acc.concat([executable])
19+
}, [])
20+
}
21+
22+
/**
23+
* Check if all params are valid
24+
*/
25+
public checkParams(params: any, availableFlagsValues: any) {
26+
return Object.keys(params).reduce((acc: Array<any>, key) => {
27+
const value = params[key]
28+
let valid: boolean | string = true
29+
switch (key) {
30+
case 'format':
31+
valid = new FormatValidator().isValid(value, availableFlagsValues.formats)
32+
break
33+
case 'project':
34+
valid = new ProjectValidator().isValid(value, availableFlagsValues.projects)
35+
break
36+
case 'from':
37+
valid = new DateValidator().isValid(value, {})
38+
break
39+
case 'to':
40+
valid = new DateValidator().isValid(value, params)
41+
break
42+
default:
43+
valid = `You should not be here with ${key}`
44+
}
45+
return valid !== true ? acc.concat([valid]) : acc
46+
}, [])
47+
}
48+
}
449

550
export {
651
DateValidator,
752
FormatValidator,
8-
ProjectValidator
53+
ProjectValidator,
54+
SystemRequirementValidator
955
}

src/utils/commands/export/validators/format-validator.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,11 @@ import { Validator } from './validator'
66
*/
77
export default class FormatValidator implements Validator {
88
public isValid(format: string, availableFormats: Array<string>) {
9-
let valid: boolean | any = availableFormats.some(availableFormat => {
9+
let valid: boolean | string = availableFormats.some(availableFormat => {
1010
return format === availableFormat
1111
})
1212
if (!valid) {
13-
valid = `Unknow format '${format}' `
14-
valid += '(csv and json are currently supported)'
13+
valid = `Unknow format '${format}' (csv and json are currently supported)`
1514
}
1615
return valid
1716
}

src/utils/commands/export/validators/project-validator.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,9 @@ import { Validator } from './validator'
55
*/
66
export default class ProjectValidator implements Validator {
77
public isValid(project: string, availableProjects: Array<string>) {
8-
let valid: boolean | any = availableProjects.some(availableProject => {
8+
let valid: boolean | string = availableProjects.some(availableProject => {
99
return project === availableProject
1010
})
11-
if (!valid) {
12-
valid = `Unknown project '${project}'`
13-
}
14-
return valid
11+
return valid || `Unknown project '${project}'`
1512
}
1613
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
export interface Validator {
2-
isValid(value: string, arrayOfValues: Array<any> | null): boolean | string
2+
isValid(value: string | any, arrayOfValues: Array<any> | null): boolean | string | Array<string>
33
}

0 commit comments

Comments
 (0)