Skip to content

Repository files navigation

Faithlife.DataAnnotations

Faithlife.DataAnnotations provides helpers for working with DataAnnotations from System.ComponentModel.

NuGet

Overview

This library includes:

  • ValidatorUtility, which validates an object with Validator.TryValidateObject or Validator.ValidateObject using validateAllProperties: 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.

Usage

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; } = [];
}

Contributing

See Contributing for setup and contribution guidelines. For a history of notable changes, see the Release Notes.

About

Helpers for working with DataAnnotations from System.ComponentModel.

Resources

Contributing

Stars

0 stars

Watchers

21 watching

Forks

Used by

Contributors

Languages