diff options
author | Shin'ya Ueoka <ueokande@i-beam.org> | 2019-12-03 14:29:36 +0900 |
---|---|---|
committer | Shin'ya Ueoka <ueokande@i-beam.org> | 2019-12-03 14:29:36 +0900 |
commit | 3e2ebb7797f6e12777b6da943765ff172bd179a9 (patch) | |
tree | 0d3191698d45f342e3752e1be94c53c0d554be77 /src/shared/settings/Properties.ts | |
parent | d8556a9b1e2e71f0efbce24b5a6ac42bb419268d (diff) |
Validate json settings with ajv
Diffstat (limited to 'src/shared/settings/Properties.ts')
-rw-r--r-- | src/shared/settings/Properties.ts | 37 |
1 files changed, 20 insertions, 17 deletions
diff --git a/src/shared/settings/Properties.ts b/src/shared/settings/Properties.ts index 63ff991..9cdaffe 100644 --- a/src/shared/settings/Properties.ts +++ b/src/shared/settings/Properties.ts @@ -1,3 +1,20 @@ +import Validator from './Validator'; + +const Schema = { + type: 'object', + properties: { + hintchars: { + type: 'string', + }, + smoothscroll: { + type: 'boolean', + }, + complete: { + type: 'string', + }, + }, +}; + export type PropertiesJSON = { hintchars?: string; smoothscroll?: boolean; @@ -65,23 +82,9 @@ export default class Properties { this.complete = complete || defaultValues.complete; } - static fromJSON(json: any): Properties { - let defNames: Set<string> = new Set(defs.map(def => def.name)); - let unknownName = Object.keys(json).find(name => !defNames.has(name)); - if (unknownName) { - throw new TypeError(`Unknown property name: "${unknownName}"`); - } - - for (let def of defs) { - if (!Object.prototype.hasOwnProperty.call(json, def.name)) { - continue; - } - if (typeof json[def.name] !== def.type) { - throw new TypeError( - `property "${def.name}" is not ${def.type}`); - } - } - return new Properties(json); + static fromJSON(json: unknown): Properties { + let obj = new Validator<PropertiesJSON>(Schema).validate(json); + return new Properties(obj); } static types(): PropertyTypes { |