diff options
Diffstat (limited to 'src/shared/settings/Validator.ts')
-rw-r--r-- | src/shared/settings/Validator.ts | 20 |
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; + } +} |