In C# we've DataAnnotation to add meta attributes on properties. I need this functionality in TypeScript for a ldap model class. Decorators should set the LDAP attribute which is internally used in the LDAP directory
export class LdapUser {
@ldapAttribute('sn')
sureName: string;
}
export function ldapAttribute(attributeName: string): (target: any, propertyKey: string) => void {
return function (target: any, propertyKey: string) {
Reflect.defineMetadata("ldapAttribute", attributeName, target, propertyKey);
}
}
But to fetch the ldapAttribute
decorator value I need to pass object and attribute name as raw strings like this:
let user = new LdapUser();
let sureNameAttribute = Reflect.getMetadata("ldapAttribute", user, "sureName"); // sn
It works, but that seems bad practice, since it would result in a runtime instead of compiler error when sureName
attribute is renamed in LdapUser
without applying this to the Reflect.getMetadata()
call. And also intellisense is missing. So I'm searching for a solution like this:
let sureNameAttribute = Reflect.getMetadata("ldapAttribute", user.sureName);
Problem here is that I need some sort of reflection to divide user.SureName
in the attribute name (here sureName
) and class object (here user
). I already did something like this in C# using reflection, but have no clue how to do this in TS.
Aucun commentaire:
Enregistrer un commentaire