Skip to content

Instantly share code, notes, and snippets.

@Alxandr
Created May 13, 2025 11:59
Show Gist options
  • Save Alxandr/857b0f36a6f0c05244bdee8cb77a23fa to your computer and use it in GitHub Desktop.
Save Alxandr/857b0f36a6f0c05244bdee8cb77a23fa to your computer and use it in GitHub Desktop.
public record Root
: IValidatableModel
{
public Child? Child { get; init; }
public List<Child>? Children { get; init; }
public void Validate(ref ValidationContext context)
{
context.Validate(Child, "child");
context.Check(Children is not null, StdValidationErrors.Required, "children");
if (Children is not null)
{
context.Check(Children.Count > 0, TestValidationErrors.Empty, "children");
context.ValidateItems(Children, "children", static (ref ValidationContext ctx, Child child) =>
{
child.Validate(ref ctx);
});
}
}
}
public record Child
: IValidatableModel
{
public string? Required { get; init; }
public void Validate(ref ValidationContext context)
{
context.Check(!string.IsNullOrEmpty(Required), StdValidationErrors.Required, "required");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment