Skip to content

Instantly share code, notes, and snippets.

@karenpayneoregon
Last active January 30, 2024 23:42
Show Gist options
  • Save karenpayneoregon/8640e5b632c410ea7ce0eb3db9732f74 to your computer and use it in GitHub Desktop.
Save karenpayneoregon/8640e5b632c410ea7ce0eb3db9732f74 to your computer and use it in GitHub Desktop.

Using FluentValidation NuGet package, create a custom validator to validate that Master.Children contain one of the names in Master.ValidNames

Example which goes not validate.

internal partial class Program
{
    static void Main(string[] args)
    {
        Master master = new()
        {
            FamilyName = "Smith",
            ValidNames = ["Jim", "Mary", "Karen"], 
            Children = [new Child { Name = "Mick" }]
        };

        CustomValidator validator = new();
        ValidationResult result = validator.Validate(master);

        Console.WriteLine();
        
        if (result.IsValid)
        {
            ...
        }
        else
        {
            ...
        }
    }
}
public class Child
{
public string Name;
public int Age;
}
Master master = new()
{
FamilyName = "Smith",
ValidNames = ["Jim", "Mary", "Karen"],
Children = [new Child { Name = "Mick" }]
};
public class Master
{
public int Id { get; set; }
public string FamilyName { get; set; }
public List<string> ValidNames;
public List<Child> Children;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment