Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Validation/src/ValidatablePropertyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ protected ValidatablePropertyInfo(
public virtual async Task ValidateAsync(object? value, ValidateContext context, CancellationToken cancellationToken)
{
var property = DeclaringType.GetProperty(Name) ?? throw new InvalidOperationException($"Property '{Name}' not found on type '{DeclaringType.Name}'.");
if (property.GetMethod?.IsStatic is true)
{
return;
}

var propertyValue = property.GetValue(value);
var validationAttributes = GetValidationAttributes();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,69 @@ [new RequiredAttribute()]),
Assert.Equal(0, context.CurrentDepth);
}

[Fact]
public async Task Validate_IgnoresStaticProperties()
{
var typeInfo = new TestValidatableTypeInfo(
typeof(StaticRecursiveModel),
[
CreatePropertyInfo(typeof(StaticRecursiveModel), typeof(StaticRecursiveModel), "TheAnswer", "TheAnswer",
[])
]);

var validationOptions = new TestValidationOptions(new Dictionary<Type, ValidatableTypeInfo>
{
{ typeof(StaticRecursiveModel), typeInfo }
})
{
MaxDepth = 2
};

var model = new StaticRecursiveModel();
var context = new ValidateContext
{
ValidationOptions = validationOptions,
ValidationErrors = [],
ValidationContext = new ValidationContext(model)
};

await typeInfo.ValidateAsync(model, context, default);

Assert.Empty(context.ValidationErrors);
Assert.Equal(0, context.CurrentDepth);
}

[Fact]
public async Task Validate_ThrowsForRecursiveInstanceProperties()
{
var typeInfo = new TestValidatableTypeInfo(
typeof(InstanceRecursiveModel),
[
CreatePropertyInfo(typeof(InstanceRecursiveModel), typeof(InstanceRecursiveModel), "Next", "Next",
[])
]);

var validationOptions = new TestValidationOptions(new Dictionary<Type, ValidatableTypeInfo>
{
{ typeof(InstanceRecursiveModel), typeInfo }
})
{
MaxDepth = 2
};

var model = new InstanceRecursiveModel();
var context = new ValidateContext
{
ValidationOptions = validationOptions,
ValidationErrors = [],
ValidationContext = new ValidationContext(model)
};

var exception = await Assert.ThrowsAsync<InvalidOperationException>(
async () => await typeInfo.ValidateAsync(model, context, default));
Assert.StartsWith("Maximum validation depth of 2 exceeded at 'Next.Next' in 'InstanceRecursiveModel'.", exception.Message, StringComparison.Ordinal);
}

[Fact]
public async Task Validate_HandlesCustomValidationAttributes()
{
Expand Down Expand Up @@ -950,6 +1013,16 @@ private class OrderItem
public int Quantity { get; set; }
}

private class StaticRecursiveModel
{
public static StaticRecursiveModel TheAnswer => new();
}

private class InstanceRecursiveModel
{
public InstanceRecursiveModel Next => new();
}

private class TreeNode
{
public string Name { get; set; } = string.Empty;
Expand Down
Loading