diff options
| author | Oskar Nyberg <oskar@mullvad.net> | 2021-05-18 10:20:02 +0200 |
|---|---|---|
| committer | Oskar Nyberg <oskar@mullvad.net> | 2021-05-20 12:13:09 +0200 |
| commit | 4a604a8876f1a53839f4e375dc0068ad4d7a1418 (patch) | |
| tree | f80e31e08a6f2918bd5a6cdae24dc875617f76b5 | |
| parent | 3cb94f460c882e7e1e0a0712cc91edfaf2351fae (diff) | |
| download | mullvadvpn-4a604a8876f1a53839f4e375dc0068ad4d7a1418.tar.xz mullvadvpn-4a604a8876f1a53839f4e375dc0068ad4d7a1418.zip | |
Add length condition when parsing GUI settings array
| -rw-r--r-- | gui/src/main/gui-settings.ts | 35 |
1 files changed, 19 insertions, 16 deletions
diff --git a/gui/src/main/gui-settings.ts b/gui/src/main/gui-settings.ts index 753f767bde..7708facc84 100644 --- a/gui/src/main/gui-settings.ts +++ b/gui/src/main/gui-settings.ts @@ -125,25 +125,28 @@ export default class GuiSettings { private validateSettings(settings: Record<string, unknown>) { Object.entries(settingsSchema).forEach(([key, expectedType]) => { - if (/^Array<.*>/.test(expectedType)) { - const value = settings[key]; - if (!Array.isArray(value)) { - throw new Error(`Expected ${key} to be array but wasn't`); + if (key in settings) { + if (/^Array<.*>/.test(expectedType)) { + const value = settings[key]; + if (!Array.isArray(value)) { + throw new Error(`Expected ${key} to be array but wasn't`); + } else { + const expectedInnerType = expectedType.replace(/^Array</, '').replace(/>$/, ''); + const innerTypes: string[] = value.map((value) => typeof value); + if ( + innerTypes.length > 0 && + (innerTypes.some((value) => value !== innerTypes[0]) || + innerTypes[0] !== expectedInnerType) + ) { + throw new Error(`Expected ${key} to contain ${expectedInnerType}s`); + } + } } else { - const expectedInnerType = expectedType.replace(/^Array</, '').replace(/>$/, ''); - const innerTypes: string[] = value.map((value) => typeof value); - if ( - innerTypes.some((value) => value !== innerTypes[0]) || - innerTypes[0] !== expectedInnerType - ) { - throw new Error(`Expected ${key} to to contain ${expectedInnerType}s`); + const actualType = typeof settings[key]; + if (actualType !== expectedType) { + throw new Error(`Expected ${key} to be of type ${expectedType} but was ${actualType}`); } } - } else { - const actualType = typeof settings[key]; - if (key in settings && actualType !== expectedType) { - throw new Error(`Expected ${key} to be of type ${expectedType} but was ${actualType}`); - } } }); |
