Skip to content

Commit

Permalink
Merge pull request #34 from m-loubani/mloubani/add-test-to-fix-valida…
Browse files Browse the repository at this point in the history
…tion-inner-dto-problem

Added unit test to show error while validating object
  • Loading branch information
pruiz committed Jan 11, 2019
2 parents 77a44b6 + 626a51a commit fa62dbf
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 4 deletions.
39 changes: 39 additions & 0 deletions HermaFx.DataAnnotations.Tests/ExtendedValidatorTests.cs
Expand Up @@ -14,13 +14,19 @@ public class InnerDto
{
[Required]
public string Field { get; set; }

[MaxLength(10)]
public string Field2 { get; set; }
}

public class OutterDto
{
[Required, MinLength(5)]
public string AString { get; set; }

[MaxLength(10)]
public string BString { get; set; }

[ValidateObject]
public InnerDto Inner { get; set; }

Expand Down Expand Up @@ -131,5 +137,38 @@ public void InvalidObjectFailsRequiredOnlyValidation2()
Assert.That(result, Has.Exactly(1).Property("MemberNames").Contains("InnerList[0].Field"));
}


[Test]
public void InvalidObjectFailsMaxLengthInnerDto()
{
var dto = BuildDto(true);

dto.BString = "123456789012345"; // Try to fire MaxLengthAttribute..
dto.Inner.Field = null; // Try to fire RequiredAttribute..
dto.Inner.Field2 = "123456789012345"; // Try to fire MaxLengthAttribute..

var result = ExtendedValidator.Validate(dto);

Assert.IsNotNull(result);
Assert.That(result, Has.Length.EqualTo(3));
Assert.That(result, Has.Exactly(1).Property("MemberNames").Contains("BString"));
Assert.That(result, Has.Exactly(1).Property("MemberNames").Contains("Inner.Field"));
Assert.That(result, Has.Exactly(1).Property("MemberNames").Contains("Inner.Field2"));
}

[Test]
public void InvalidObjectFailsMaxLengthInnerDto2()
{
var dto = BuildDto(true);

dto.Inner.Field = "hello";
dto.Inner.Field2 = "123456789012345"; // Try to fire MaxLengthAttribute..

var result = ExtendedValidator.Validate(dto);

Assert.IsNotNull(result);
Assert.That(result, Has.Length.EqualTo(1));
Assert.That(result, Has.Exactly(1).Property("MemberNames").Contains("Inner.Field2"));
}
}
}
36 changes: 32 additions & 4 deletions HermaFx.DataAnnotations/ExtendedValidator.cs
Expand Up @@ -88,13 +88,41 @@ private static IEnumerable<ValidationResult> ValidateProperties(object value, Va
{
var validateAllProperties = ShouldValidateAllProperties(context);

if (validateAllProperties)
if (property.GetCustomAttribute<ValidateObjectAttribute>() != null)
{
Validator.TryValidateObject(value2, newctx, results, validateAllProperties);
results.AddRange(ValidateRecursing(value2, newctx));
}
else if (property.GetCustomAttribute<ValidateObjectAttribute>() != null)
else if (validateAllProperties)
{
results.AddRange(ValidateRecursing(value2, newctx));
Validator.TryValidateObject(value2, newctx, results, validateAllProperties);

// We need another fake context in order to provide a valid 'MemberName'
// to dotnet's internal validation infrastructure. (pruiz)
var ctx2 = new ValidationContext(value, context.Items)
{
DisplayName = newctx.DisplayName,
MemberName = property.Name
};
var results2 = new List<ValidationResult>();
Validator.TryValidateProperty(value2, ctx2, results2);

// Convert results so they confom to our MemberName conventions.
if (!context.MemberName.IsNullOrWhiteSpace())
{
foreach (var result in results2)
{
var mnames = (result.MemberNames ?? Enumerable.Empty<string>());
mnames = mnames.Select(x => string.Join(".", context.MemberName, x)).ToArray();

// FIXME: Handle AggregateValidationResults too?

results.Add(new ValidationResult(result.ErrorMessage, mnames));
}
}
else
{
results.AddRange(results2);
}
}
}
}
Expand Down

0 comments on commit fa62dbf

Please sign in to comment.