-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathvalidator.ts
49 lines (41 loc) · 1.09 KB
/
validator.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import * as path from 'path'
import * as semver from 'semver'
function validateVersion(version: string): string {
if (
typeof version === 'string' &&
(version === 'latest' || semver.valid(version) === version)
) {
// valid
return version
}
throw new Error('Invalid version')
}
function validateSourcePath(sourcePath: string): string {
const normalized = path.normalize(sourcePath)
if (
path.isAbsolute(normalized) ||
typeof normalized !== 'string' ||
normalized.match(/[ ;:"'$]/)
) {
throw Error('Invalid sourcePath')
}
return normalized
}
function validateRulesets(rulesets: string): string {
if (typeof rulesets !== 'string' || rulesets.match(/[;:"'$"]/))
throw new Error('Invalid rulesets')
const normalized = rulesets.replace(/ /g, '')
return normalized
}
function validateDownloadUrl(url: string): string {
if (typeof url === 'string' && (url === '' || url.match(/^https?:\/\//)))
// valid
return url
throw new Error('Invalid downloadUrl')
}
export {
validateVersion,
validateSourcePath,
validateRulesets,
validateDownloadUrl
}