mercredi 3 janvier 2018

How to get all properties (including undefined) using reflection in Typescript?

I have:

let requiredSymbobl = Symbol('required');
let function required() { return Reflect.metadata(requiredSymbol, null); }

class Person {
  @required();
  name: string;
}


let p = new Person(); //In reality comes from POST data

//Validate every key with required meta data.
for(let key in p) {
  if(typeof Reflect.getMetadata(requiredSymbol, p, key) !== 'undefined') {
    //Validate 
  }
}

The problem is if the client doesn't POST an object with 'name'property defined, it won't be iterated in the loop. Is there a way to use static reflection here to check all keys even if they aren't defined?





Aucun commentaire:

Enregistrer un commentaire