Skip to content

Instantly share code, notes, and snippets.

@dataneek
Created March 9, 2017 18:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dataneek/bf0095d568b0385e939ba22c39cd832b to your computer and use it in GitHub Desktop.
Save dataneek/bf0095d568b0385e939ba22c39cd832b to your computer and use it in GitHub Desktop.
FluentValidation with Child Collections
class Program
{
static void Main(string[] args)
{
var command = new CreateCommand
{
Items = new List<CreateCommand.Item>()
{
new CreateCommand.Item { Email = "test@test.com", NameFirst = "Test", NameLast = "Test"},
new CreateCommand.Item { Email = "testtest.com", NameFirst = "Test", NameLast = "Test"},
new CreateCommand.Item {}
}
};
var validator = new CreateCommandValidator();
var result = validator.Validate(command);
}
public class CreateCommand
{
public List<Item> Items { get; set; }
public class Item
{
public string Email { get; set; }
public string NameFirst { get; set; }
public string NameLast { get; set; }
}
}
public class CreateCommandValidator : AbstractValidator<CreateCommand>
{
public CreateCommandValidator()
{
RuleFor(t => t.Items).NotEmpty();
RuleFor(t => t.Items).SetCollectionValidator(new ItemValidator());
}
public class ItemValidator : AbstractValidator<CreateCommand.Item>
{
public ItemValidator()
{
RuleFor(t => t.Email).NotNull().EmailAddress();
}
}
}
}
@dataneek
Copy link
Author

dataneek commented Mar 9, 2017

useful when validating dynamic forms

@pilotgeraldb
Copy link

slick!! real nice.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment