Created
May 13, 2025 11:59
-
-
Save Alxandr/857b0f36a6f0c05244bdee8cb77a23fa to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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