Faithlife.DataAnnotations provides helpers for working with DataAnnotations from System.ComponentModel.
This library includes:
- ValidatorUtility, which validates an object with
Validator.TryValidateObjectorValidator.ValidateObjectusingvalidateAllProperties: true. - ValidateObjectAttribute, which validates a property value that has its own data annotations.
- ValidateItemsAttribute, which validates each item in a collection property and can optionally allow null items.
Use ValidatorUtility when you want simple validation helpers for a single object.
var results = ValidatorUtility.GetValidationResults(model);
if (results.Count != 0)
{
throw new ValidationException(results[0].ErrorMessage);
}Use ValidateObjectAttribute and ValidateItemsAttribute on nested values that should be validated along with their containing object.
public sealed class UserRequest
{
[ValidateObject]
public AddressRequest? Address { get; set; }
[ValidateItems]
public IReadOnlyList<RoleRequest> Roles { get; set; } = [];
}See Contributing for setup and contribution guidelines. For a history of notable changes, see the Release Notes.