Skip to content

Commit

Permalink
optimize validate logic
Browse files Browse the repository at this point in the history
  • Loading branch information
tinohager committed Jan 14, 2025
1 parent d712b46 commit 7b1e23e
Showing 1 changed file with 52 additions and 2 deletions.
54 changes: 52 additions & 2 deletions src/Nager.EmailAuthentication/DmarcRecordParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,15 @@ public static bool TryParse(
{
"rua", new MappingHandler
{
Map = value => dataFragment.AggregateReportUri = value
Map = value => dataFragment.AggregateReportUri = value,
Validate = ValidateAddresses
}
},
{
"ruf", new MappingHandler
{
Map = value => dataFragment.ForensicReportUri = value
Map = value => dataFragment.ForensicReportUri = value,
Validate = ValidateAddresses
}
},
{
Expand Down Expand Up @@ -459,5 +461,53 @@ private static ParseError[] ValidateAlignmentMode(ValidateRequest validateReques

return [.. errors];
}

private static ParseError[] ValidateAddresses(ValidateRequest validateRequest)
{
var errors = new List<ParseError>();

if (string.IsNullOrEmpty(validateRequest.Value))
{
errors.Add(new ParseError
{
Severity = ErrorSeverity.Error,
ErrorMessage = $"{validateRequest.Field} is empty"
});

return [.. errors];
}

var dmarcUris = validateRequest.Value.Split(',');

foreach (var dmarcUri in dmarcUris)
{
if (!DmarcEmailDetail.TryParse(dmarcUri.Trim(), out var dmarcEmailDetail))
{
errors.Add(new ParseError
{
Severity = ErrorSeverity.Error,
ErrorMessage = $"{validateRequest.Field} wrong dmarc uri {dmarcUri}"
});

continue;
}

if (dmarcEmailDetail == null)
{
continue;
}

if (!dmarcEmailDetail.IsValidEmailAddress)
{
errors.Add(new ParseError
{
Severity = ErrorSeverity.Error,
ErrorMessage = $"{validateRequest.Field} wrong email address {dmarcEmailDetail.EmailAddress}"
});
}
}

return [.. errors];
}
}
}

0 comments on commit 7b1e23e

Please sign in to comment.