| | 1 | | using NostrSure.Domain.Entities; |
| | 2 | | using NostrSure.Domain.Validation; |
| | 3 | |
|
| | 4 | | namespace NostrSure.Domain.Services; |
| | 5 | |
|
| | 6 | | /// <summary> |
| | 7 | | /// Validates tag structure and content according to NIP-01 rules |
| | 8 | | /// </summary> |
| | 9 | | public sealed class EventTagValidator : IEventTagValidator |
| | 10 | | { |
| | 11 | | public ValidationResult ValidateTags(NostrEvent evt) |
| 109 | 12 | | { |
| 109 | 13 | | if (evt.Tags == null) |
| 1 | 14 | | { |
| 1 | 15 | | return ValidationResult.Failure("Tags are null", "NULL_TAGS"); |
| | 16 | | } |
| | 17 | |
|
| 330 | 18 | | foreach (var tag in evt.Tags) |
| 4 | 19 | | { |
| 4 | 20 | | if (tag == null) |
| 0 | 21 | | { |
| 0 | 22 | | return ValidationResult.Failure("Tag is null", "NULL_TAG"); |
| | 23 | | } |
| | 24 | |
|
| 4 | 25 | | if (string.IsNullOrWhiteSpace(tag.Name)) |
| 0 | 26 | | { |
| 0 | 27 | | return ValidationResult.Failure("Tag name is empty or null", "EMPTY_TAG_NAME"); |
| | 28 | | } |
| | 29 | |
|
| | 30 | | // Check if tag is valid according to NIP-01 rules |
| 4 | 31 | | if (!tag.IsValid()) |
| 2 | 32 | | { |
| 2 | 33 | | return ValidationResult.Failure( |
| 2 | 34 | | $"Invalid tag: {tag.Name}", |
| 2 | 35 | | "INVALID_TAG_FORMAT"); |
| | 36 | | } |
| | 37 | |
|
| | 38 | | // Check for empty values in tag |
| 2 | 39 | | if (tag.Values.Any(string.IsNullOrWhiteSpace)) |
| 0 | 40 | | { |
| 0 | 41 | | return ValidationResult.Failure( |
| 0 | 42 | | "Tag contains empty value", |
| 0 | 43 | | "EMPTY_TAG_VALUE"); |
| | 44 | | } |
| 2 | 45 | | } |
| | 46 | |
|
| 106 | 47 | | return ValidationResult.Success(); |
| 109 | 48 | | } |
| | 49 | | } |