Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,39 @@ class Widget : IValidatableObject
}
```

### Use external validators

External validators can be resolved from the `IServiceProvider` passed to `MiniValidator`. Register one or more `IValidate<T>` services (or `IAsyncValidate<T>` services when calling `TryValidateAsync`):

```csharp
using System.ComponentModel.DataAnnotations;
using Microsoft.Extensions.DependencyInjection;
using MiniValidation;

var services = new ServiceCollection();
services.AddSingleton<IValidate<Widget>, WidgetValidator>();
using var serviceProvider = services.BuildServiceProvider();

var widget = new Widget { Name = "" };
var isValid = MiniValidator.TryValidate(widget, serviceProvider, out var errors);

class Widget
{
public string? Name { get; set; }
}

class WidgetValidator : IValidate<Widget>
{
public IEnumerable<ValidationResult> Validate(Widget target, ValidationContext validationContext)
{
if (string.IsNullOrWhiteSpace(target.Name))
{
yield return new ValidationResult("Name is required.", new[] { nameof(Widget.Name) });
}
}
}
```

### Console app

```csharp
Expand Down
20 changes: 20 additions & 0 deletions src/MiniValidation/IAsyncValidate.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Threading.Tasks;

namespace MiniValidation;

/// <summary>
/// Provides a way for an object to be validated asynchronously by an external validator.
/// </summary>
/// <typeparam name="TTarget">The type of object to validate.</typeparam>
public interface IAsyncValidate<in TTarget>
{
/// <summary>
/// Determines whether the specified object is valid.
/// </summary>
/// <param name="target">The object to validate.</param>
/// <param name="validationContext">The validation context.</param>
/// <returns>A collection that holds failed-validation information.</returns>
Task<IEnumerable<ValidationResult>> ValidateAsync(TTarget target, ValidationContext validationContext);
}
19 changes: 19 additions & 0 deletions src/MiniValidation/IValidate.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

namespace MiniValidation;

/// <summary>
/// Provides a way for an object to be validated by an external validator.
/// </summary>
/// <typeparam name="TTarget">The type of object to validate.</typeparam>
public interface IValidate<in TTarget>
{
/// <summary>
/// Determines whether the specified object is valid.
/// </summary>
/// <param name="target">The object to validate.</param>
/// <param name="validationContext">The validation context.</param>
/// <returns>A collection that holds failed-validation information.</returns>
IEnumerable<ValidationResult> Validate(TTarget target, ValidationContext validationContext);
}
Loading