parent
2318e3a555
commit
776977e0dc
11 changed files with 377 additions and 467 deletions
@ -1,20 +0,0 @@ |
||||
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; |
||||
} |
||||
} |
Reference in new issue