Skip to content

Commit

Permalink
Consumer Api: Improve tag validation on startup (#1059)
Browse files Browse the repository at this point in the history
* feat: Disallow first-level tag names to be "x" or "X"

* chore: Rename `nameParts` to `propertyPath`

---------

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
MH321Productions and mergify[bot] authored Feb 26, 2025
1 parent 5d87dfe commit 78137b0
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions Modules/Tags/src/Tags.Application/ApplicationOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,23 @@ public class ContainsValidTagsAttribute : ValidationAttribute
return ValidationResult.Success;
}

private ValidationResult? ValidateTag(IEnumerable<string> nameParts, TagInfo tag)
private ValidationResult? ValidateTag(IEnumerable<string> propertyPath, TagInfo tag)
{
var notSupportedLanguages = tag.DisplayNames.Keys.Except(_supportedLanguages).ToList();
var notImplementedLanguages = _supportedLanguages.Except(tag.DisplayNames.Keys).ToList();

if (notSupportedLanguages.Count != 0)
return new ValidationResult($"The languages \"{Enumerate(notSupportedLanguages)}\" are unsupported", [GetPathOfProperty(nameParts)]);
return new ValidationResult($"The languages \"{Enumerate(notSupportedLanguages)}\" are unsupported", [GetPathOfProperty(propertyPath)]);
if (notImplementedLanguages.Count != 0)
return new ValidationResult($"A display name for the language(s) \"{Enumerate(notImplementedLanguages)}\" is required.", [GetPathOfProperty(nameParts)]);
return new ValidationResult($"A display name for the language(s) \"{Enumerate(notImplementedLanguages)}\" is required.", [GetPathOfProperty(propertyPath)]);

var tagName = propertyPath.Last();
if (propertyPath.Count() == 2 && tagName.Equals("x", StringComparison.OrdinalIgnoreCase))
return new ValidationResult("A first-level tag may not be equal to \"x\" or \"X\".", [GetPathOfProperty(propertyPath)]);

foreach (var (childName, child) in tag.Children)
{
var result = ValidateTag(nameParts.Append(childName), child);
var result = ValidateTag(propertyPath.Append(childName), child);
if (result != ValidationResult.Success) return result;
}

Expand Down

0 comments on commit 78137b0

Please sign in to comment.