上QQ阅读APP看书,第一时间看更新
Validating the name
The name validation is the simplest piece of validation we are going to write. This validation assumes that we have a minimum of one letter for the first name and two letters for the last name:
export class PersonValidation implements IValidation {
private readonly firstNameValidator : MinLengthValidator = new MinLengthValidator(1);
private readonly lastNameValidator : MinLengthValidator = new MinLengthValidator(2);
public Validate(state: IPersonState, errors: string[]): void {
if (!this.firstNameValidator.IsValid(state.FirstName)) {
errors.push("The first name is a minimum of 1 character");
}
if (!this.lastNameValidator.IsValid(state.FirstName)) {
errors.push("The last name is a minimum of 2 characters");
}
}
}