Exemplo n.º 1
0
        public CreateListValidator(IListService listService)
        {
            RuleFor(dto => dto.UserId)
            .NotEmpty().WithMessage("Unauthorized")
            .Must((dto, userId) => !listService.Exists(dto.Name, userId)).WithMessage("AlreadyExists")
            .Must((dto, userId) => listService.Count(userId) < 50).WithMessage("Lists.ListLimitReached");

            RuleFor(dto => dto.Name)
            .NotEmpty().WithMessage("Lists.ModifyList.NameIsRequired")
            .MaximumLength(50).WithMessage("Lists.ModifyList.NameMaxLength");

            RuleFor(dto => dto.Icon)
            .NotEmpty().WithMessage("Lists.ModifyList.IconIsRequired")
            .Must(icon => ListIcons.Contains(icon)).WithMessage("Lists.ModifyList.InvalidIcon");

            RuleFor(dto => dto.TasksText).Must(tasksText =>
            {
                if (string.IsNullOrEmpty(tasksText))
                {
                    return(true);
                }
                var tasks = tasksText.Split("\n").Where(x => !string.IsNullOrWhiteSpace(x));
                return(tasks.Any());
            }).WithMessage("Lists.CreateList.NoTasks").Must(tasksText =>
            {
                if (string.IsNullOrEmpty(tasksText))
                {
                    return(true);
                }
                var tasks      = tasksText.Split("\n").Where(x => !string.IsNullOrWhiteSpace(x));
                var duplicates = tasks.GroupBy(x => x).Where(g => g.Count() > 1).Select(y => y.Key).ToList();
                return(!duplicates.Any());
            }).WithMessage("Lists.CreateList.DuplicateTasks").Must(tasksText =>
            {
                if (string.IsNullOrEmpty(tasksText))
                {
                    return(true);
                }
                var tasks = tasksText.Split("\n").Where(x => !string.IsNullOrWhiteSpace(x));
                return(tasks.Count() < 250);
            }).WithMessage("TasksPerListLimitReached").Must(tasksText =>
            {
                if (string.IsNullOrEmpty(tasksText))
                {
                    return(true);
                }
                var tasks = tasksText.Split("\n").Where(x => !string.IsNullOrWhiteSpace(x) && x.Length > 50);
                return(!tasks.Any());
            }).WithMessage("Lists.CreateList.TasksNameMaxLength");
        }
Exemplo n.º 2
0
        public UpdateListValidator(IListService listService)
        {
            RuleFor(dto => dto.UserId)
            .NotEmpty().WithMessage("Unauthorized")
            .Must((dto, userId) => listService.UserOwnsOrSharesAsAdmin(dto.Id, userId)).WithMessage("Unauthorized")
            .Must((dto, userId) => !listService.UserOwnsOrSharesAsAdmin(dto.Id, dto.Name, userId)).WithMessage("AlreadyExists")
            .Must((dto, userId) => !listService.Exists(dto.Id, dto.Name, userId)).WithMessage("AlreadyExists");

            RuleFor(dto => dto.Name)
            .NotEmpty().WithMessage("Lists.ModifyList.NameIsRequired")
            .MaximumLength(50).WithMessage("Lists.ModifyList.NameMaxLength");

            RuleFor(dto => dto.Icon)
            .NotEmpty().WithMessage("Lists.ModifyList.IconIsRequired")
            .Must(icon => ListIcons.Contains(icon)).WithMessage("Lists.ModifyList.InvalidIcon");
        }