public UpdateTownValidator(IMediator mediator) { var uniqueIndexValidator = new UniqueIndexValidator <Town>(mediator, t => t.Postcode.ToString(), t => t.Name); RuleFor(a => a.Dto.Name).NotEmpty().DependentRules(() => RuleFor(a => a.Dto.Postcode).NotEmpty().DependentRules(() => RuleFor(a => a.Dto).CustomAsync(uniqueIndexValidator.ValidateExisting)) ); }
public AddTownValidator(IMediator mediator) { var uniqueIndexValidator = new UniqueIndexValidator <Town>(mediator, t => t.Postcode.ToString(), t => t.Name); //RuleFor(a => a.Dto.Name).NotEmpty().DependentRules( // () => // { // RuleFor(a => a.Dto.Name).CustomAsync(new UniqueIndexValidator<Town>(mediator, t => t.Name).Validate); // }); RuleFor(a => a.Dto.Name).NotEmpty().DependentRules(() => RuleFor(a => a.Dto.Postcode).NotEmpty().DependentRules(() => RuleFor(a => a.Dto).CustomAsync(uniqueIndexValidator.Validate)) ); }
public AddWorkshopParticipant(IMediator mediator) { this.mediator = mediator; var uniqueIndexValidator = new UniqueIndexValidator <WorkshopParticipant>(mediator, t => t.StudentId.ToString(), t => t.WorkshopId.ToString()); RuleFor(a => a.Dto.StudentId) .ForeignKeyExists <AddCommand <WorkshopParticipant>, Student>(mediator) //check that Student exists .DependentRules(() => RuleFor(a => a.Dto.WorkshopId) .Cascade(CascadeMode.Stop) .ForeignKeyExists <AddCommand <WorkshopParticipant>, Workshop> (mediator) //check that Workshop exists .MustAsync(HaveFreePlaces).WithMessage("Workshop does not have free places") //check free places .DependentRules(() => RuleFor(a => a.Dto).CustomAsync(uniqueIndexValidator.Validate) //check unique index ) ); }