I'm currently in the process of developing a validation algorithm for a sophisticated data model using Typescript. To simplify things, I've created a class model that looks like this:
export abstract class Validatable {
protected errors: string[];
isValid(): boolean {
let result = true;
for each property (which may or may not be an array) in this instance which extends Validatable {
if (Array(property)) {
if (!property.every(item=>item.isValid()) {
return false;
}
} else {
if (!property.isValid()) {
return false;
}
}
}
return this.errors.length === 0;
}
}
export class B extends Validatable{
name: string;
}
export class A extends Validatable{
name: string;
Bs: B[];
}
I'm facing difficulties trying to determine how to implement the looping over properties (line 'for each property). I am unsure whether it is even achievable.
The concept behind this approach is to invoke isValid on an object of class A, which will subsequently trigger isValid on all its individual properties instead of duplicating similar code across multiple classes.