0
votes

Created this validation:



 public ActivityValidator(IUserAccessor userAccessor)
        {
            var userId = userAccessor.GetUserIdFromAccessToken();

            RuleSet("CompleteHappeningAsync", () =>
                {
                    RuleFor(a => a.ActivityTypeId).IsHappening()
                    .WithState(x => new BadRequest("Aktivnost nije Događaj"))
                        .DependentRules(() =>
                        {
                            RuleFor(a => a.EndDate).LessThanOrEqualTo(DateTimeOffset.Now)
                            .WithState(x => new BadRequest("Događaj se još nije završio"))
                            .DependentRules(() =>
                            {
                                RuleFor(a => a.EndDate).GreaterThan(DateTimeOffset.Now.AddDays(-7))
                                .WithState(x => new BadRequest("Prošlo je nedelju dana od završetka Događaja"))
                                .DependentRules(() =>
                                {
                                    RuleFor(a => a.User.Id).Equal(userId)
                                    .WithState(x => new BadRequest("Ne možete završiti tuđi događaj"))
                                    .DependentRules(() =>
                                    {
                                        RuleFor(a => a.HappeningMedias).Empty()
                                        .WithState(x => new BadRequest("Već ste završili događaj"));
                                    });
                                });
                            });
                        });
                });
        }

How can this be rewritten in recommanded way by using When?

CascadeMode is used for rules chained for one property, so this can't be used here.

RECOMANNDED WAY - image taken from official site