diff options
author | Shin'ya Ueoka <ueokande@i-beam.org> | 2019-12-03 18:11:32 +0900 |
---|---|---|
committer | Shin'ya Ueoka <ueokande@i-beam.org> | 2019-12-03 18:34:48 +0900 |
commit | 776977e0dcaf47a81054dcc81d76722ada71f9eb (patch) | |
tree | 0fbcdeda0be45d4416884a1b527c0e2013945979 /src/shared/settings/Blacklist.ts | |
parent | 2318e3a55592cf7888006bd1afe43deec396d394 (diff) |
Validate on top-level settings and use pre-compiled ajv
Diffstat (limited to 'src/shared/settings/Blacklist.ts')
-rw-r--r-- | src/shared/settings/Blacklist.ts | 33 |
1 files changed, 5 insertions, 28 deletions
diff --git a/src/shared/settings/Blacklist.ts b/src/shared/settings/Blacklist.ts index 201e7fc..1903a78 100644 --- a/src/shared/settings/Blacklist.ts +++ b/src/shared/settings/Blacklist.ts @@ -1,23 +1,4 @@ import Key from './Key'; -import Validator from './Validator'; - -const ItemSchema = { - anyOf: [ - { type: 'string' }, - { - type: 'object', - properties: { - url: { type: 'string' }, - keys: { - type: 'array', - items: { type: 'string', minLength: 1 }, - minItems: 1, - } - }, - required: ['url', 'keys'], - } - ], -}; export type BlacklistItemJSON = string | { url: string, @@ -54,11 +35,10 @@ export class BlacklistItem { this.keyEntities = this.keys.map(Key.fromMapKey); } - static fromJSON(json: unknown): BlacklistItem { - let obj = new Validator<BlacklistItemJSON>(ItemSchema).validate(json); - return typeof obj === 'string' - ? new BlacklistItem(obj, false, []) - : new BlacklistItem(obj.url, true, obj.keys); + static fromJSON(json: BlacklistItemJSON): BlacklistItem { + return typeof json === 'string' + ? new BlacklistItem(json, false, []) + : new BlacklistItem(json.url, true, json.keys); } toJSON(): BlacklistItemJSON { @@ -91,10 +71,7 @@ export default class Blacklist { ) { } - static fromJSON(json: unknown): Blacklist { - if (!Array.isArray(json)) { - throw new TypeError('blacklist is not an array'); - } + static fromJSON(json: BlacklistJSON): Blacklist { let items = json.map(o => BlacklistItem.fromJSON(o)); return new Blacklist(items); } |