I am on angular 20 and typescript, i have datamodels e.g.
export type SchoolVM {
name?: string;
address: AddressVM;
classes: Array<ClassVM>;
....
}
export type AddressVM {
line1: string;
line2: string;
town: string;
.....
}
export type ClassVM {
name: string;
studentsCount: number;
location: string;
}
There are a lot more models and properties but i'm keeping things simple intentionally. I have fluentvalidation-ts npm package for adding validation to the models. here's how the validation classes look like, the idea is to reuse the validation class where ever needed.
// assume i have the imports
export class SchoolValidator extends Validator<SchoolVM> {
protected readonly addressValidator = new AddressValidator();
protected readonly classValidator = new ClassValidator();
constructor() {
super();
this.ruleFor('name').notNull().notEmpty().minLength(2);
this.ruleFor('address').setValidator(() => this.addressValidator);
// **QUESTION: Is the following correct?**
this.ruleForEach('classes').setValidator(() => this.ClassValidator);
}
}
export class ClassValidator extends Validator<ClassVM> {
constructor() {
super();
this.ruleFor('name').notNull().notEmpty().minLength(2);
this.ruleFor('studentCount').notNull();
this.ruleFor('location').notNull().notEmpty().minLength(2);
}
}
The address and other validators follow the same pattern as the SchoolValidator and ClassValidator. all are complex objects, some have nested objects like class has students and students also have address. so validator reusability is important.
There is no mention in the documentation (apologies if i missed it but i looked carefully) how to approach a scenario where we have a complex object array for the ruleForEach... Is the following correct or do you suggest another approach?
this.ruleForEach('classes').setValidator(() => this.classValidator);
Thanks in advance.
I am on angular 20 and typescript, i have datamodels e.g.
There are a lot more models and properties but i'm keeping things simple intentionally. I have fluentvalidation-ts npm package for adding validation to the models. here's how the validation classes look like, the idea is to reuse the validation class where ever needed.
The address and other validators follow the same pattern as the SchoolValidator and ClassValidator. all are complex objects, some have nested objects like class has students and students also have address. so validator reusability is important.
There is no mention in the documentation (apologies if i missed it but i looked carefully) how to approach a scenario where we have a complex object array for the ruleForEach... Is the following correct or do you suggest another approach?
Thanks in advance.