-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathvalidate.js
More file actions
45 lines (34 loc) · 1.6 KB
/
validate.js
File metadata and controls
45 lines (34 loc) · 1.6 KB
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
const versions = require("./versions.json");
// This file valides that the versions.json file is structured correctly.
// On the top level, there should be an object with two keys: "0" for android and "1" for ios.
// Each of these objects should have a number as the key (build number) and the value should be a string (human readable version).
// The string should be a valid semver version.
const platforms = {
android: 0,
ios: 1
}
function validateVersions(versions) {
// Check if versions object exists and has the required platforms
if (!versions || typeof versions !== 'object') return false;
if (!versions[platforms.android] || !versions[platforms.ios]) return false;
// Check if each platform contains valid version mappings
for (const platform of [platforms.android, platforms.ios]) {
const platformVersions = versions[platform];
// Check if platform versions is an object
if (typeof platformVersions !== 'object') return false;
// Check each version entry
for (const [buildNumber, version] of Object.entries(platformVersions)) {
// Build number should be a numeric string
if (!/^\d+$/.test(buildNumber)) return false;
// Version should be a string matching semver-like pattern
if (typeof version !== 'string') return false;
if (!/^\d+\.\d+\.\d+(?:[-][a-zA-Z\d]+)?$/.test(version)) return false;
}
}
return true;
}
// Validate versions on import
if (!validateVersions(versions)) {
throw new Error('Invalid versions.json structure');
}
console.log("versions.json is valid");