LogoLogo
  • Dosaic
  • Hosting
    • WebHost
    • Generator
    • Abstractions
  • Plugins
    • Authorization
      • Abstractions
      • Keycloak
    • Endpoints
      • Abstractions
      • RestResourceEntity
    • Handlers
      • Abstractions
      • CQRS
    • Jobs
      • Hangfire
    • Management
      • Unleash
    • Mapping
      • Mapster
    • Messaging
      • Abstractions
      • MassTransit
    • Persistence
      • Abstractions
      • EntityFramework
      • InMemory
      • MongoDb
      • S3
      • VaultSharp
    • Validations
      • Abstractions
      • Attribute
  • Extensions
    • RestEase
    • Sqids
  • Testing
    • NUnit
Powered by GitBook
On this page
  • Installation
  • Appsettings.yml
  • Usage
  1. Plugins
  2. Validations

Attribute

Dosaic.Plugins.Validations.AttributeValidation is a plugin that allows other Dosaic components to validate object using attributes.

Installation

To install the nuget package follow these steps:

dotnet add package Dosaic.Plugins.Validations.AttributeValidation

or add as package reference to your .csproj

<PackageReference Include="Dosaic.Plugins.Validations.AttributeValidation" Version="" />

Appsettings.yml

You do not need to configure anything, because the implementation resolver, does this automatically at startup.

Usage

The validator is auto-registered using the Plugin technology. You can use it by adding the Validations attribute to your model and inject the IValidator in your usage.

However, there are following Validations attibutes:

  • Required

  • Expression

  • Array

    • Length

    • MinLength

    • MaxLength

  • Bool

    • True

    • False

  • Date

    • Before

    • After

    • Age

    • MinAge

    • MaxAge

  • Int

    • Range

    • Min

    • Max

    • Positive

    • Negative

  • String

    • Length

    • MinLength

    • MaxLength

    • Email

    • Url

    • Regex


Example:
```csharp
internal class DbModel {
    [Validations.Required]
    public string Id {get;set;}
    [Validations.String.MinLength(5), Validations.String.MaxLength(10), Validations.String.Regex(@"^[a-zA-Z]+$")]
    public string LongName {get;set;}
    [Validations.Expression("Value % 2 == 0")]
    public int SomeNumber {get;set;}
}

// USAGE:
IServiceProvider sp;
var validator = sp.GetRequiredService<IValidator>();
var dbModel = new DbModel { Id = "1", LongName = "LongName", SomeNumber = 2 };
var result = await validator.ValidateAsync(dbModel);
Console.WriteLine(result.IsValid); // true
var failModel = new DbModel { Id = "", LongName = "..", SomeNumber = 3 };
var result = await validator.ValidateAsync(dbModel);
Console.WriteLine(result.IsValid); // false
Console.WriteLine(result.Errors); // List of errors
PreviousAbstractionsNextRestEase

Last updated 3 months ago