Created
October 10, 2019 13:03
-
-
Save cmddavid/3de6a1ccd0bb549a00c66f4fd59f7596 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Validator { | |
validateProperties?(props: (keyof Person)[]){ | |
props.forEach((prop:string) => { | |
if(!this[prop]){ | |
console.error(`Missing "${prop}" property in object`); | |
} | |
}) | |
} | |
} | |
enum PersonProperties { | |
NAME = 'name', | |
AGE = 'age', | |
EMAIL = 'email' | |
} | |
class Person extends Validator { | |
[PersonProperties.NAME]: string; | |
[PersonProperties.AGE]: number; | |
[PersonProperties.EMAIL]?: string; | |
constructor(obj: Person){ | |
super(); | |
for(const key in obj){ | |
this[key] = obj[key]; | |
} | |
}; | |
} | |
const testPerson = new Person({ name: 'Fred', age: 12 }); // mock server response data | |
testPerson.validateProperties([PersonProperties.NAME,PersonProperties.AGE,PersonProperties.EMAIL]); | |
// Result => Missing "email" property in object |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment