Quantcast
Channel: Fluent Validation for .NET
Viewing all articles
Browse latest Browse all 1917

Commented Unassigned: RuleFor/SetCollectionValidator doesn't show up in IntelliSense for IList properties [7207]

$
0
0
Given types:
```
class User
{
public int Id { get; set; }
public string Name { get; set; }
}

class Role
{
public string Name { get; set; }
public IList<string> AllowedOperations { get; set; }

public IList<User> Users { get; set; }
}
```

and validators:
```
class UserValidator : AbstractValidator<User>
{
public UserValidator()
{
RuleFor(x => x.Id)
.NotEmpty();
}
}

class RoleValidator : AbstractValidator<Role>
{
public RoleValidator()
{
// SetCollectionValidator compiles but doesn't show up in IntelliSense
RuleFor(x => x.Users)
.SetCollectionValidator(new UserValidator());
}
}
```

Addition of following extension method to base AbstractValidator would solve this problem:
```
protected IRuleBuilderInitial<Role, IEnumerable<TProperty>> RuleFor<TProperty>(Expression<Func<Role, IEnumerable<TProperty>>> expression)
{
return base.RuleFor<IEnumerable<TProperty>>(expression);
}
```

This behavior can be confusing for inexperienced programmers, on my team I had problem with younger programmer who tried to use RuleForEach instead of RuleFor+SetCollectionValidator because 'it didn't show up in IntelliSense'. Please consider also improving documentation on RuleForEach method.
Comments: Sorry for late answer. I checked this on VS Community Update 4 without any plugins (fresh install), and on VS 2014 Pro and it _didn't_ show up, maybe you have ReSharper installed and it adds some "better IntelliSense". I tested this with FluentValidation version 5.5.0.0 nuget package from official repo (nuget.org). In attachment you may find entire solution that I use for testing, maybe this will help you spot this issue.

Viewing all articles
Browse latest Browse all 1917

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>