validation/
This commit is contained in:
1 parent
eb0dc429da
commit
6c7713cda4
4 files changed
+52
-90
No files matched your search
+26
-23
@@ -14,39 +14,42 @@ Item {
|
||||
watchChanges: true
|
||||
onFileChanged: configFile.reload()
|
||||
onLoaded: {
|
||||
let parsed = JSON.parse(configFile.text());
|
||||
if (!Array.isArray(parsed)) {
|
||||
throw new Error(`Invalid app config: expected a JSON array, got ${typeof parsed}`);
|
||||
const logPrefix = 'Invalid app config: ';
|
||||
const testIt = (cond, error) => {
|
||||
if (!cond) {
|
||||
throw new Error(`${logPrefix} ${error}`)
|
||||
}
|
||||
}
|
||||
for (const elm of parsed) {
|
||||
if (!(typeof elm.name == 'string')) {
|
||||
throw new Error(`Invalid app config: expected name to be a string, got ${typeof elm.name}`);
|
||||
}
|
||||
if (!(typeof elm.image == 'string')) {
|
||||
throw new Error(`Invalid app config: expected image to be a string, got ${typeof elm.image}`);
|
||||
}
|
||||
if (elm.isIcon !== undefined && typeof elm.isIcon !== 'boolean') {
|
||||
throw new Error(`Invalid app config: expected isIcon to be a boolean, got ${typeof elm.isIcon}`);
|
||||
const isOf = (typeName) => (name, elm, iffDefined = false) => {
|
||||
if (!iffDefined || (iffDefined && elm !== undefined)) {
|
||||
testIt(typeof elm === typeName, `expected ${name} to be a ${typeName}, got ${typeof elm}`)
|
||||
}
|
||||
}
|
||||
const isBoolean = isOf('boolean')
|
||||
const isString = isOf('string')
|
||||
let parsed = JSON.parse(configFile.text());
|
||||
let apps = parsed.apps;
|
||||
if (!Array.isArray(apps)) {
|
||||
throw new Error(`Invalid app config: expected a JSON array, got ${typeof apps}`);
|
||||
}
|
||||
isBoolean('autoStart', parsed.autoStart, true)
|
||||
for (const elm of apps) {
|
||||
isString('name', elm.name)
|
||||
isString('image', elm.image)
|
||||
isBoolean('isIcon', elm.isIcon, true)
|
||||
if (elm.dims !== undefined) {
|
||||
if (!elm.isIcon) {
|
||||
throw new Error(`Invalid app config: dims is only allowed when isIcon is true`);
|
||||
}
|
||||
testIt(elm.isIcon, 'dims is only allowed when isIcon is true')
|
||||
if (!Array.isArray(elm.dims) || elm.dims.length !== 2 || !elm.dims.every(n => Number.isInteger(n))) {
|
||||
throw new Error(`Invalid app config: expected dims to be a tuple of 2 ints`);
|
||||
throw new Error(`${logPrefix} expected dims to be a tuple of 2 ints`);
|
||||
}
|
||||
} else {
|
||||
elm.dims = [100, 100];
|
||||
}
|
||||
if (!Array.isArray(elm.exec) || elm.exec.length === 0) {
|
||||
throw new Error(`Invalid app config: expected exec to be a non-empty array, got ${typeof elm.exec}`);
|
||||
}
|
||||
if (!elm.exec.every(arg => typeof arg === 'string')) {
|
||||
throw new Error(`Invalid app config: expected all exec entries to be strings`);
|
||||
}
|
||||
testIt(Array.isArray(elm.exec) && elm.exec.length > 0, `expected exec to be a non-empty array, got ${elm.exec}`)
|
||||
testIt(elm.exec.every(arg => typeof arg === 'string'), 'expected all exec entries to be strings')
|
||||
}
|
||||
console.log("Loaded Config", configFile.__data);
|
||||
root.appConfig = parsed;
|
||||
root.appConfig = apps;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user