Skip to content

Commit 95782b2

Browse files
committed
Adding GetAll method to retrieve all the error messages in translation error section
1 parent 62beee9 commit 95782b2

2 files changed

Lines changed: 64 additions & 1 deletion

File tree

src/TranslationErrorSection.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
namespace Orbyss.Components.Json.Models;
1+
using System.Data;
2+
using System.Reflection;
3+
4+
namespace Orbyss.Components.Json.Models;
25

36
public sealed record TranslationErrorSection(
47
string? Custom,
@@ -68,6 +71,21 @@ public string GetDefault()
6871
return GetValueOrDefault(Custom, DefaultJsonFormValidationMessages.Default);
6972
}
7073

74+
public IEnumerable<string> GetAll()
75+
{
76+
var properties = GetType()
77+
.GetProperties(BindingFlags.Instance | BindingFlags.Public)
78+
.Select(x => $"{x.GetValue(this)}")
79+
.Where(x => !string.IsNullOrWhiteSpace(x));
80+
81+
if(!properties.Any())
82+
{
83+
return [GetDefault()];
84+
}
85+
86+
return properties;
87+
}
88+
7189
private string GetValueOrDefault(string? value, string defaultValue)
7290
{
7391
if (!string.IsNullOrWhiteSpace(value))
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace Orbyss.Components.Json.Models.Tests;
6+
7+
[TestFixture]
8+
public sealed class TranslationErrorSectionTests
9+
{
10+
[Test]
11+
public void When_GetAll_Then_Returns_AllSpecifiedProperties()
12+
{
13+
// Arrange
14+
var section = new TranslationErrorSection(
15+
Const: "Const Message",
16+
Minimum: "Minimum Message",
17+
Maximum: "Maximum Message",
18+
MinimumLength: "Min Length Message",
19+
MaximumLength: "Max Length Message",
20+
MinimumItems: "Min Items Message",
21+
MaximumItems: "Max Items Message",
22+
Required: "Required Message",
23+
Custom: "Custom Message",
24+
Contains: "Contains Message",
25+
Pattern: "Pattern Message"
26+
);
27+
28+
// Act
29+
var result = section.GetAll();
30+
31+
// Assert
32+
Assert.That(result.Count(), Is.EqualTo(11));
33+
Assert.That(result, Does.Contain("Const Message"));
34+
Assert.That(result, Does.Contain("Minimum Message"));
35+
Assert.That(result, Does.Contain("Maximum Message"));
36+
Assert.That(result, Does.Contain("Min Length Message"));
37+
Assert.That(result, Does.Contain("Max Length Message"));
38+
Assert.That(result, Does.Contain("Min Items Message"));
39+
Assert.That(result, Does.Contain("Max Items Message"));
40+
Assert.That(result, Does.Contain("Required Message"));
41+
Assert.That(result, Does.Contain("Custom Message"));
42+
Assert.That(result, Does.Contain("Contains Message"));
43+
Assert.That(result, Does.Contain("Pattern Message"));
44+
}
45+
}

0 commit comments

Comments
 (0)