Skip to content

Commit

Permalink
latest notifications validation logic (#107)
Browse files Browse the repository at this point in the history
  • Loading branch information
acn-sbuad authored Jun 18, 2024
1 parent 1e28413 commit 812906e
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 22 deletions.
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;
}
}

0 comments on commit 812906e

Please sign in to comment.