public ValidationResult Validate(string endPointAssessorOrganisationId)
        {
            var validationResult = new ValidationResult();

            var isValid = _organisationQueryRepository.CheckIfAlreadyExists(endPointAssessorOrganisationId).Result;

            if (isValid)
            {
                validationResult = new ValidationResult();
            }
            else
            {
                validationResult.Errors.Add(new ValidationFailure("Organisation",
                                                                  string.Format(_localiser[ResourceMessageName.DoesNotExist].Value, "Organisation", endPointAssessorOrganisationId)));
            }

            return(validationResult);
        }
        public UpdateOrganisationRequestValidator(IStringLocalizer <UpdateOrganisationRequestValidator> localiser,
                                                  IContactQueryRepository contactQueryRepository,
                                                  IOrganisationQueryRepository organisationQueryRepository
                                                  )
        {
            _contactQueryRepository      = contactQueryRepository;
            _organisationQueryRepository = organisationQueryRepository;

            // ReSharper disable once LocalNameCapturedOnly
            UpdateOrganisationRequest updateOrganisationRequest;

            RuleFor(organisation => organisation.EndPointAssessorName).NotEmpty().WithMessage(
                string.Format(localiser[ResourceMessageName.MustBeDefined].Value,
                              nameof(updateOrganisationRequest.EndPointAssessorName).ToCamelCase()));

            RuleFor(organisation => organisation.PrimaryContact)
            .Custom((primaryContact, context) =>
            {
                if (string.IsNullOrEmpty(primaryContact))
                {
                    return;
                }

                var result = contactQueryRepository.CheckContactExists(primaryContact).Result;
                if (!result)
                {
                    context.AddFailure(new ValidationFailure("PrimaryContact",
                                                             string.Format(localiser[ResourceMessageName.DoesNotExist].Value, "PrimaryContact", primaryContact)));
                }
            });

            RuleFor(organisation => organisation.EndPointAssessorOrganisationId)
            .Custom((endPointAssessorOrganisationId, context) =>
            {
                var result = organisationQueryRepository.CheckIfAlreadyExists(endPointAssessorOrganisationId).Result;
                if (!result)
                {
                    context.AddFailure(new ValidationFailure("Organisation",
                                                             string.Format(localiser[ResourceMessageName.DoesNotExist].Value, "Organisation", endPointAssessorOrganisationId)));
                }
            });
        }
        public CreateOrganisationRequestValidator(IStringLocalizer <CreateOrganisationRequestValidator> localiser,
                                                  IContactQueryRepository contactQueryRepository,
                                                  IOrganisationQueryRepository organisationQueryRepository
                                                  )
        {
            _contactQueryRepository = contactQueryRepository;

            // ReSharper disable once LocalNameCapturedOnly
            CreateOrganisationRequest createOrganisationRequest;

            RuleFor(organisation => organisation.EndPointAssessorOrganisationId)
            .NotEmpty()
            .WithMessage(
                string.Format(localiser[ResourceMessageName.MustBeDefined].Value, nameof(createOrganisationRequest.EndPointAssessorOrganisationId).ToCamelCase()))
            .MaximumLength(12)
            // Please note we have to string.Format this due to limitation in Moq not handling Optional
            // Params
            .WithMessage(string.Format(localiser[ResourceMessageName.MaxLengthError].Value,
                                       nameof(createOrganisationRequest.EndPointAssessorOrganisationId), 12));

            RuleFor(organisation => organisation.EndPointAssessorName).NotEmpty().WithMessage(
                string.Format(localiser[ResourceMessageName.MustBeDefined].Value,
                              nameof(createOrganisationRequest.EndPointAssessorName).ToCamelCase()));

            RuleFor(organisation => organisation.EndPointAssessorUkprn).InclusiveBetween(10000000, 99999999)
            .WithMessage(localiser[ResourceMessageName.InvalidUkprn].Value);

            RuleFor(organisation => organisation.EndPointAssessorOrganisationId)
            .Custom((endPointAssessorOrganisationId, context) =>
            {
                var result = organisationQueryRepository.CheckIfAlreadyExists(endPointAssessorOrganisationId).Result;
                if (result)
                {
                    context.AddFailure(new ValidationFailure("Organisation",
                                                             string.Format(localiser[ResourceMessageName.AlreadyExists].Value, "Organisation")));
                }
            });
        }
Exemplo n.º 4
0
        public CreateContactRequestValidator(IStringLocalizer <CreateContactRequestValidator> localiser,
                                             IOrganisationQueryRepository organisationQueryRepository,
                                             IContactQueryRepository contactQueryRepository
                                             )
        {
            // ReSharper disable once LocalNameCapturedOnly
            CreateContactRequest createContactRequest;

            RuleFor(contact => contact.Email).NotEmpty().WithMessage(
                string.Format(localiser[ResourceMessageName.MustBeDefined].Value,
                              nameof(createContactRequest.Email).ToCamelCase()))
            .MaximumLength(120)
            // Please note we have to string.Format this due to limitation in Moq not handling Optional
            // Params
            .WithMessage(string.Format(localiser[ResourceMessageName.MaxLengthError].Value,
                                       nameof(createContactRequest.Email), 120))
            .EmailAddress()
            .WithMessage(string.Format(localiser[ResourceMessageName.MustBeValidEmailAddress].Value));

            RuleFor(contact => contact.DisplayName).NotEmpty().WithMessage(
                string.Format(localiser[ResourceMessageName.MustBeDefined].Value, nameof(createContactRequest.DisplayName).ToCamelCase()))
            .MaximumLength(120)
            // Please note we have to string.Format this due to limitation in Moq not handling Optional
            // Params
            .WithMessage(string.Format(localiser[ResourceMessageName.MaxLengthError].Value,
                                       nameof(createContactRequest.DisplayName), 120));

            RuleFor(contact => contact.Username)
            .NotEmpty()
            .WithMessage(
                string.Format(localiser[ResourceMessageName.MustBeDefined].Value, nameof(createContactRequest.Username).ToCamelCase()))
            .MaximumLength(30)
            // Please note we have to string.Format this due to limitation in Moq not handling Optional
            // Params
            .WithMessage(string.Format(localiser[ResourceMessageName.MaxLengthError].Value,
                                       nameof(createContactRequest.Username), 30));

            RuleFor(contact => contact)
            .Custom((contact, context) =>
            {
                var result = contactQueryRepository.CheckContactExists(contact.Username).Result;
                if (result)
                {
                    context.AddFailure(new ValidationFailure("Contact",
                                                             string.Format(localiser[ResourceMessageName.AlreadyExists].Value, "Contact")));
                }
            });

            RuleFor(contact => contact.EndPointAssessorOrganisationId)
            .Custom((endPointAssessorOrganisationId, context) =>
            {
                if (string.IsNullOrEmpty(endPointAssessorOrganisationId))
                {
                    context.AddFailure(new ValidationFailure(nameof(endPointAssessorOrganisationId),
                                                             string.Format(localiser[ResourceMessageName.MustBeDefined].Value,
                                                                           nameof(createContactRequest.EndPointAssessorOrganisationId))));
                    return;
                }

                var result = organisationQueryRepository.CheckIfAlreadyExists(endPointAssessorOrganisationId).Result;
                if (!result)
                {
                    context.AddFailure(new ValidationFailure("Organisation",
                                                             string.Format(localiser[ResourceMessageName.DoesNotExist].Value, "Organisation", endPointAssessorOrganisationId)));
                }
            });
        }