swaf/src/db/ModelComponent.ts

32 lines
901 B
TypeScript
Raw Normal View History

2020-07-24 12:13:28 +02:00
import Model from "./Model";
import Validator from "./Validator";
export default abstract class ModelComponent<T extends Model> {
protected readonly _model: T;
private readonly _validators: { [key: string]: Validator<any> } = {};
[key: string]: any;
public constructor(model: T) {
this._model = model;
}
public init(): void {
for (const property of this._properties) {
if (!property.startsWith('_')) {
(this._model as Model)[property] = this[property];
}
}
}
2020-07-25 10:28:50 +02:00
protected setValidation<V>(propertyName: keyof this): Validator<V> {
const validator = new Validator<V>();
2020-07-24 12:13:28 +02:00
this._validators[propertyName as string] = validator;
return validator;
}
private get _properties(): string[] {
return Object.getOwnPropertyNames(this).filter(p => !p.startsWith('_'));
}
}