Skip to content

Instantly share code, notes, and snippets.

@jorik041
Forked from karenpayneoregon/Child.cs
Created January 30, 2024 23:42
Show Gist options
  • Save jorik041/f14889a418f2de44ca94d844f67aaf9f to your computer and use it in GitHub Desktop.
Save jorik041/f14889a418f2de44ca94d844f67aaf9f 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