public ChangeUserUseCasesValidator(FotobloggerContext context)
        {
            RuleFor(u => u.UserId)
            .NotEmpty();

            var useCaseIdsInDB = context.UseCases.Select(uc => uc.Id).ToList();

            When(r => r.AllowedUseCases != null && r.AllowedUseCases.Any(), () =>
                 RuleFor(r => r.AllowedUseCases)
                 .Must(AllowedUseCases => AllowedUseCases.ToList().TrueForAll(id => useCaseIdsInDB.Contains(id)))
                 .WithMessage($"Some use case(s) you are tring to assign to a role don't exist in database.")
                 );
        }
Exemplo n.º 2
0
        public CreateRoleValidator(FotobloggerContext context)
        {
            RuleFor(r => r.Name)
            .NotEmpty()
            .MaximumLength(50)
            .Must(Name => !context.Roles.Any(r => r.Name == Name))
            .WithMessage("Role with that name already exists.");

            RuleFor(r => r.AllowedUseCases)
            .NotEmpty();

            RuleFor(r => r.RoleType)
            .NotEmpty()
            .IsInEnum();

            var useCaseIdsInDB = context.UseCases.Select(uc => uc.Id).ToList();

            When(r => r.AllowedUseCases != null && r.AllowedUseCases.Any(), () =>
                 RuleFor(r => r.AllowedUseCases)
                 .Must(AllowedUseCases => AllowedUseCases.ToList().TrueForAll(id => useCaseIdsInDB.Contains(id)))
                 .WithMessage($"Some use case(s) you are tring to assign to a role don't exist in database.")
                 );
        }