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/Search.ts | |
parent | 2318e3a55592cf7888006bd1afe43deec396d394 (diff) |
Validate on top-level settings and use pre-compiled ajv
Diffstat (limited to 'src/shared/settings/Search.ts')
-rw-r--r-- | src/shared/settings/Search.ts | 34 |
1 files changed, 8 insertions, 26 deletions
diff --git a/src/shared/settings/Search.ts b/src/shared/settings/Search.ts index bdbe4a8..7d7e555 100644 --- a/src/shared/settings/Search.ts +++ b/src/shared/settings/Search.ts @@ -1,22 +1,3 @@ -import Validator from './Validator'; - -const Schema = { - type: 'object', - properties: { - default: { type: 'string' }, - engines: { - type: 'object', - propertyNames: { - pattern: '^[A-Za-z_][A-Za-z0-9_]+$', - }, - patternProperties: { - '.*': { type: 'string' }, - }, - }, - }, - required: ['default'], -}; - type Entries = { [name: string]: string }; export type SearchJSON = { @@ -31,10 +12,11 @@ export default class Search { ) { } - static fromJSON(json: unknown): Search { - let obj = new Validator<SearchJSON>(Schema).validate(json); - - for (let [name, url] of Object.entries(obj.engines)) { + static fromJSON(json: SearchJSON): Search { + for (let [name, url] of Object.entries(json.engines)) { + if (!(/^[a-zA-Z0-9]+$/).test(name)) { + throw new TypeError('Search engine\'s name must be [a-zA-Z0-9]+'); + } let matches = url.match(/{}/g); if (matches === null) { throw new TypeError(`No {}-placeholders in URL of "${name}"`); @@ -42,11 +24,11 @@ export default class Search { throw new TypeError(`Multiple {}-placeholders in URL of "${name}"`); } } - if (!Object.keys(obj.engines).includes(obj.default)) { - throw new TypeError(`Default engine "${obj.default}" not found`); + if (!Object.keys(json.engines).includes(json.default)) { + throw new TypeError(`Default engine "${json.default}" not found`); } - return new Search(obj.default, obj.engines); + return new Search(json.default, json.engines); } toJSON(): SearchJSON { |