Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- export function validateArgs(constructor: Function) {
- // copy the methods so that we avoid self referencing.
- const original: Function = Object.assign({}, constructor);
- // iterate over all the static methods in the class
- for (const i in constructor) {
- // wrap them in a method that throws an error if args are not valid
- constructor[i] = function(...args: any[]) {
- if(areArgsValid(args)) {
- return original[i].apply(this, args);
- }
- throw `Args aren't valid`
- };
- }
- }
- @validateArgs
- class Validator {
- static myThingIsValid(arg1) {
- return !!arg1;
- }
- myOtherThingIsValid(arg1) {
- return !!arg1;
- }
- }
- for (const i in constructor) { ... }
- Object.getOwnPropertyNames(constructor)
- .filter(prop => typeof prop === 'function')
- .forEach(val => ...)
- [
- ...Object.getOwnPropertyNames(constructor),
- ...Object.getOwnPropertyNames(constructor.prototype)
- ]
- .filter(prop => typeof prop === 'function')
- .forEach(val => ...)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement