aboutsummaryrefslogtreecommitdiff
path: root/src/shared/settings/Search.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/shared/settings/Search.ts')
-rw-r--r--src/shared/settings/Search.ts34
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 {