Created
March 9, 2017 18:32
-
-
Save dataneek/bf0095d568b0385e939ba22c39cd832b to your computer and use it in GitHub Desktop.
FluentValidation with Child Collections
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
useful when validating dynamic forms