swaf/src/db/ModelComponent.ts

32 lines
901 B
TypeScript

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];
}
}
}
protected setValidation<T>(propertyName: keyof this): Validator<T> {
const validator = new Validator<T>();
this._validators[propertyName as string] = validator;
return validator;
}
private get _properties(): string[] {
return Object.getOwnPropertyNames(this).filter(p => !p.startsWith('_'));
}
}