Skip to content

Commit fc076a7

Browse files
committed
- checkpoint: refactor configuration validation
1 parent 81d2887 commit fc076a7

5 files changed

Lines changed: 31 additions & 15 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ Please see [Release Roadmap](https://github.com/CodeShayk/TurboMapper/blob/maste
8686
- **Conditional Mapping**: Added When method to IMappingExpression for conditional property mapping
8787
- **Mapping Transformations**: Added MapWith method for transformation functions during mapping
8888
- **Comprehensive Type Conversions**: Enhanced ConvertValue with DateTime, TimeSpan, and other common type conversions
89-
- **Configuration Validation**: Added ValidateMapping and GetMappingErrors methods to IMapper for early validation
89+
- **Configuration Validation**: Added ValidateMapping method to IMapper that returns ValidationResult for early validation
9090
- **Improved Error Messages**: Better debugging information for conversion failures
9191

9292
## Contributing

src/TurboMapper/IMapper.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ namespace TurboMapper
66
public interface IMapper
77
{
88
TTarget Map<TSource, TTarget>(TSource source);
9+
910
IEnumerable<TDestination> Map<TSource, TDestination>(IEnumerable<TSource> source);
10-
bool ValidateMapping<TSource, TTarget>();
11-
string[] GetMappingErrors<TSource, TTarget>();
11+
12+
ValidationResult ValidateMapping<TSource, TTarget>();
1213
}
1314
}

src/TurboMapper/Impl/Mapper.cs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -201,13 +201,7 @@ private bool IsIgnoredInCustomMappings(string targetPropertyName, List<PropertyM
201201
// Custom converter system
202202
private readonly Dictionary<string, Delegate> _converters = new Dictionary<string, Delegate>();
203203

204-
public bool ValidateMapping<TSource, TTarget>()
205-
{
206-
var errors = GetMappingErrors<TSource, TTarget>();
207-
return errors.Length == 0;
208-
}
209-
210-
public string[] GetMappingErrors<TSource, TTarget>()
204+
public ValidationResult ValidateMapping<TSource, TTarget>()
211205
{
212206
var errors = new List<string>();
213207
var sourceType = typeof(TSource);
@@ -241,7 +235,8 @@ public string[] GetMappingErrors<TSource, TTarget>()
241235
}
242236
}
243237

244-
return errors.ToArray();
238+
var isValid = errors.Count == 0;
239+
return new ValidationResult(isValid, errors);
245240
}
246241

247242
private bool PropertyExists(Type type, string propertyPath)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System.Collections.Generic;
2+
3+
namespace TurboMapper
4+
{
5+
public class ValidationResult
6+
{
7+
public bool IsValid { get; set; }
8+
public IEnumerable<string> Errors { get; set; }
9+
10+
public ValidationResult()
11+
{
12+
Errors = new List<string>();
13+
}
14+
15+
public ValidationResult(bool isValid, IEnumerable<string> errors)
16+
{
17+
IsValid = isValid;
18+
Errors = errors ?? new List<string>();
19+
}
20+
}
21+
}

tests/TurboMapper.Tests/Release120_Tests.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -291,11 +291,10 @@ public void Task6_2_ConfigurationValidation()
291291
mapper.CreateMap<Person, PersonDto>(new List<PropertyMapping>());
292292

293293
// Validate the mapping
294-
var isValid = mapper.ValidateMapping<Person, PersonDto>();
295-
var errors = mapper.GetMappingErrors<Person, PersonDto>();
294+
var validationResult = mapper.ValidateMapping<Person, PersonDto>();
296295

297-
Assert.IsTrue(isValid, string.Join(", ", errors));
298-
Assert.AreEqual(0, errors.Length);
296+
Assert.IsTrue(validationResult.IsValid, string.Join(", ", validationResult.Errors));
297+
Assert.AreEqual(0, validationResult.Errors.Count());
299298
}
300299

301300
// Test models

0 commit comments

Comments
 (0)