aboutsummaryrefslogtreecommitdiff
path: root/src/shared/settings/Validator.ts
diff options
context:
space:
mode:
authorShin'ya Ueoka <ueokande@i-beam.org>2019-12-03 14:29:36 +0900
committerShin'ya Ueoka <ueokande@i-beam.org>2019-12-03 14:29:36 +0900
commit3e2ebb7797f6e12777b6da943765ff172bd179a9 (patch)
tree0d3191698d45f342e3752e1be94c53c0d554be77 /src/shared/settings/Validator.ts
parentd8556a9b1e2e71f0efbce24b5a6ac42bb419268d (diff)
Validate json settings with ajv
Diffstat (limited to 'src/shared/settings/Validator.ts')
-rw-r--r--src/shared/settings/Validator.ts20
1 files changed, 20 insertions, 0 deletions
diff --git a/src/shared/settings/Validator.ts b/src/shared/settings/Validator.ts
new file mode 100644
index 0000000..6aac07f
--- /dev/null
+++ b/src/shared/settings/Validator.ts
@@ -0,0 +1,20 @@
+import Ajv from 'ajv';
+
+export default class Validator<T> {
+ constructor(
+ private schema: object | boolean,
+ ) {
+ }
+
+ validate(data: any): T {
+ let ajv = new Ajv();
+ let valid = ajv.validate(this.schema, data);
+ if (!valid) {
+ let message = ajv.errors!!
+ .map(err => `'${err.dataPath}' of ${err.keyword} ${err.message}`)
+ .join('; ');
+ throw new TypeError(message);
+ }
+ return data as T;
+ }
+}