Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

latest notifications validation logic #107

Merged
merged 1 commit into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,31 @@ public class EmailNotificationOrderRequestValidator : AbstractValidator<EmailNot
public EmailNotificationOrderRequestValidator()
{
RuleFor(order => order.Recipients)
.NotEmpty()
.WithMessage("One or more recipient is required.")
.Must(recipients => recipients.TrueForAll(a =>
{
return
(!string.IsNullOrWhiteSpace(a.EmailAddress) && IsValidEmail(a.EmailAddress)) ||
(!string.IsNullOrWhiteSpace(a.OrganizationNumber) ^ !string.IsNullOrWhiteSpace(a.NationalIdentityNumber));
}))
.WithMessage("Either a valid email address, organization number, or national identity number must be provided for each recipient.");
.NotEmpty()
.WithMessage("One or more recipient is required.");

RuleForEach(order => order.Recipients)
.ChildRules(recipient =>
{
recipient.RuleFor(r => r)
.Must(r => !string.IsNullOrEmpty(r.EmailAddress) || !string.IsNullOrEmpty(r.OrganizationNumber) || !string.IsNullOrEmpty(r.NationalIdentityNumber))
.WithMessage("Either a valid email address, organization number, or national identity number must be provided for each recipient.");

recipient.RuleFor(r => r.EmailAddress)
.Must(email => IsValidEmail(email))
.When(r => !string.IsNullOrEmpty(r.EmailAddress))
.WithMessage("Invalid email address format.");

recipient.RuleFor(a => a.NationalIdentityNumber)
.Must(nin => nin?.Length == ValidationConstants.NationalIdentityNumberLength && nin.All(char.IsDigit))
.When(r => !string.IsNullOrEmpty(r.NationalIdentityNumber))
.WithMessage($"National identity number must be {ValidationConstants.NationalIdentityNumberLength} digits long.");

recipient.RuleFor(a => a.OrganizationNumber)
.Must(on => on?.Length == ValidationConstants.OrganizationNumberLength && on.All(char.IsDigit))
.When(r => !string.IsNullOrEmpty(r.OrganizationNumber))
.WithMessage($"Organization number must be {ValidationConstants.OrganizationNumberLength} digits long.");
});

RuleFor(order => order.RequestedSendTime)
.Must(sendTime => sendTime.Kind != DateTimeKind.Unspecified)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,37 @@ public class SmsNotificationOrderRequestValidator : AbstractValidator<SmsNotific
public SmsNotificationOrderRequestValidator()
{
RuleFor(order => order.Recipients)
.NotEmpty()
.WithMessage("One or more recipient is required.")
.Must(recipients => recipients.TrueForAll(a =>
{
return
(!string.IsNullOrWhiteSpace(a.MobileNumber) && MobileNumberHelper.IsValidMobileNumber(a.MobileNumber)) ||
(!string.IsNullOrWhiteSpace(a.OrganizationNumber) ^ !string.IsNullOrWhiteSpace(a.NationalIdentityNumber));
}))
.WithMessage("Either a valid mobile number starting with country code, organization number, or national identity number must be provided for each recipient.");
.NotEmpty()
.WithMessage("One or more recipient is required.");

RuleForEach(order => order.Recipients)
.ChildRules(recipient =>
{
recipient.RuleFor(r => r)
.Must(r => !string.IsNullOrEmpty(r.MobileNumber) || !string.IsNullOrEmpty(r.OrganizationNumber) || !string.IsNullOrEmpty(r.NationalIdentityNumber))
.WithMessage("Either a valid mobile number starting with country code, organization number, or national identity number must be provided for each recipient.");

recipient.RuleFor(r => r.MobileNumber)
.Must(mobileNumber => MobileNumberHelper.IsValidMobileNumber(mobileNumber))
.When(r => !string.IsNullOrEmpty(r.MobileNumber))
.WithMessage("Invalid mobile number format.");

recipient.RuleFor(a => a.NationalIdentityNumber)
.Must(nin => nin?.Length == ValidationConstants.NationalIdentityNumberLength && nin.All(char.IsDigit))
.When(r => !string.IsNullOrEmpty(r.NationalIdentityNumber))
.WithMessage($"National identity number must be {ValidationConstants.NationalIdentityNumberLength} digits long.");

recipient.RuleFor(a => a.OrganizationNumber)
.Must(on => on?.Length == ValidationConstants.OrganizationNumberLength && on.All(char.IsDigit))
.When(r => !string.IsNullOrEmpty(r.OrganizationNumber))
.WithMessage($"Organization number must be {ValidationConstants.OrganizationNumberLength} digits long.");
});

RuleFor(order => order.RequestedSendTime)
.Must(sendTime => sendTime.Kind != DateTimeKind.Unspecified)
.WithMessage("The requested send time value must have specified a time zone.")
.Must(sendTime => sendTime >= DateTime.UtcNow.AddMinutes(-5))
.WithMessage("Send time must be in the future. Leave blank to send immediately.");
.Must(sendTime => sendTime.Kind != DateTimeKind.Unspecified)
.WithMessage("The requested send time value must have specified a time zone.")
.Must(sendTime => sendTime >= DateTime.UtcNow.AddMinutes(-5))
.WithMessage("Send time must be in the future. Leave blank to send immediately.");

RuleFor(order => order.Body).NotEmpty();
}
Expand Down
18 changes: 18 additions & 0 deletions src/Notifications/API/Validators/ValidationConstants.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace Altinn.Notifications.Validators
{
/// <summary>
/// Class comprised of all shared validation constants
/// </summary>
public static class ValidationConstants
{
/// <summary>
/// Required lenth for a national identity number
/// </summary>
public const int NationalIdentityNumberLength = 11;

/// <summary>
/// Required length for an organization number
/// </summary>
public const int OrganizationNumberLength = 9;
}
}
Loading