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 UpdateContactRequestValidator(IStringLocalizer <UpdateContactRequestValidator> localiser, IContactQueryRepository contactQueryRepository ) { _contactQueryRepository = contactQueryRepository; UpdateContactRequest updateContactRequest; RuleFor(contact => contact.Email) .Custom((email, context) => { if (string.IsNullOrEmpty(email)) { return; } if (email.Length > 120) { context.AddFailure(string.Format(localiser[ResourceMessageName.MaxLengthError].Value, nameof(updateContactRequest.Email).ToCamelCase(), 120)); } var regex = new Regex(@"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$"); var match = regex.Match(email); if (!match.Success) { context.AddFailure(string.Format(localiser[ResourceMessageName.MustBeValidEmailAddress] .Value)); } }); RuleFor(contact => contact.DisplayName) .Custom((displayName, context) => { if (string.IsNullOrEmpty(displayName)) { return; } if (displayName.Length > 120) { context.AddFailure(string.Format(localiser[ResourceMessageName.MaxLengthError].Value, nameof(updateContactRequest.DisplayName).ToCamelCase(), 120)); } }); RuleFor(contact => contact.UserName) .NotEmpty() .WithMessage( string.Format(localiser[ResourceMessageName.MustBeDefined].Value, nameof(updateContactRequest.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(updateContactRequest.UserName), 30)); RuleFor(contact => contact) .Custom((contact, context) => { if (string.IsNullOrEmpty(contact.UserName)) { return; } var result = contactQueryRepository.CheckContactExists(contact.UserName).Result; if (!result) { context.AddFailure(new ValidationFailure("Contact", string.Format(localiser[ResourceMessageName.DoesNotExist].Value, "Contact", contact.UserName))); } }); }
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))); } }); }