ligo/tools/webide/packages/server/src/schemas/migration.ts
2020-02-13 17:19:16 -08:00

29 lines
662 B
TypeScript

import joi from '@hapi/joi';
export abstract class Migration {
protected abstract schema: joi.ObjectSchema;
protected abstract previous: Migration | null;
protected abstract migrate(data: any): any;
validate(data: any): joi.ValidationResult {
return this.schema.validate(data);
}
forward(data: any): any {
const { error, value } = this.validate(data);
if (error) {
if (this.previous) {
return this.migrate(this.previous.forward(data));
}
throw new Error(
`Unable to migrate ${JSON.stringify(
data
)}. Reached the end of the migration chain.`
);
}
return value;
}
}