All Projects → enif-lee → Grpc Dotnet Validator

enif-lee / Grpc Dotnet Validator

Licence: mit
Simple request message validator for grpc.aspnet

Projects that are alternatives of or similar to Grpc Dotnet Validator

Formhelper
ASP.NET Core - Transform server-side validations to client-side without writing any javascript code. (Compatible with Fluent Validation)
Stars: ✭ 155 (+520%)
Mutual labels:  validation, validator, aspnetcore
Validate
⚔ Go package for data validation and filtering. support Map, Struct, Form data. Go通用的数据验证与过滤库,使用简单,内置大部分常用验证、过滤器,支持自定义验证器、自定义消息、字段翻译。
Stars: ✭ 378 (+1412%)
Mutual labels:  validation, validator
Approvejs
A simple JavaScript validation library that doesn't interfere
Stars: ✭ 336 (+1244%)
Mutual labels:  validation, validator
Cti Stix Validator
OASIS TC Open Repository: Validator for STIX 2.0 JSON normative requirements and best practices
Stars: ✭ 24 (-4%)
Mutual labels:  validation, validator
Validate
A simple jQuery plugin to validate forms.
Stars: ✭ 298 (+1092%)
Mutual labels:  validation, validator
Swagger Cli
Swagger 2.0 and OpenAPI 3.0 command-line tool
Stars: ✭ 321 (+1184%)
Mutual labels:  validation, validator
Indicative
Indicative is a simple yet powerful data validator for Node.js and browsers. It makes it so simple to write async validations on nested set of data.
Stars: ✭ 412 (+1548%)
Mutual labels:  validation, validator
thai-citizen-id-validator
🦉 Validate Thai Citizen ID with 0 dependencies 🇹🇭
Stars: ✭ 35 (+40%)
Mutual labels:  validation, validator
Express Validator
An express.js middleware for validator.js.
Stars: ✭ 5,236 (+20844%)
Mutual labels:  validation, validator
Nice Validator
Simple, smart and pleasant validation solution.
Stars: ✭ 587 (+2248%)
Mutual labels:  validation, validator
Validation
The most awesome validation engine ever created for PHP
Stars: ✭ 5,484 (+21836%)
Mutual labels:  validation, validator
js-form-validator
Javascript form validation. Pure JS. No jQuery
Stars: ✭ 38 (+52%)
Mutual labels:  validation, validator
excel validator
Python script to validate data in Excel files
Stars: ✭ 14 (-44%)
Mutual labels:  validation, validator
Validator.js
String validation
Stars: ✭ 18,842 (+75268%)
Mutual labels:  validation, validator
validation
Validation on Laravel 5.X|6.X|7.X|8.X
Stars: ✭ 26 (+4%)
Mutual labels:  validation, validator
Graphql Constraint Directive
Validate GraphQL fields
Stars: ✭ 401 (+1504%)
Mutual labels:  validation, validator
Swagger Parser
Swagger 2.0 and OpenAPI 3.0 parser/validator
Stars: ✭ 710 (+2740%)
Mutual labels:  validation, validator
NZ-Bank-Account-Validator
A small, zero dependency NZ bank account validation library that runs everywhere.
Stars: ✭ 15 (-40%)
Mutual labels:  validation, validator
checker
Golang parameter validation, which can replace go-playground/validator, includes ncluding Cross Field, Map, Slice and Array diving, provides readable,flexible, configurable validation.
Stars: ✭ 62 (+148%)
Mutual labels:  validation, validator
Validator.js
⁉️轻量级的 JavaScript 表单验证,字符串验证。没有依赖,支持 UMD ,~3kb。
Stars: ✭ 486 (+1844%)
Mutual labels:  validation, validator

grpc-dotnet-validator

Request message validator middleware for Grpc.AspNetCore

Nuget

Feature

  • Support async validation for unary, streaming call
  • Support IoC LifeStyle scopes and dependency injection
  • Profile for validators
  • Scan validators and profiles from assembly

How to use.

This package is integrated with Fluent Validation. If you want to know how build your own validation rules, please checkout Fluent Validation Docs

Add custom message validator

// Write own message validator
public class HelloRequestValidator : AbstractValidator<HelloRequest>
{
    public HelloRequestValidator()
    {
        RuleFor(request => request.Name).NotEmpty();
    }
}

public class Startup
{
    // ...
    public void ConfigureServices(IServiceCollection services)
    {
        // 1. Enable message validation feature.
        services.AddGrpc(options => options.EnableMessageValidation());

        // 2. Add custom validators for messages, default scope is scope.
        services.AddValidator(typeof(HelloRequestValidator));
        services.AddValidator<HelloRequestValidator>();
        services.AddValidator<HelloRequestValidator>(LifeStyle.Singleton);
    }
    // ...
}

Then, If the message is invalid, Grpc Validator return with InvalidArgument code and empty message object.

Add inline custom validator

if you don't want to create many validation class for simple validation rule in your project, you just use below inline validator feature like below example.

Note that, Inline validator always be registered singleton in your service collection. Because, There are no way for using other dependency.

public class Startup
{
    // ...
    public void ConfigureServices(IServiceCollection services)
    {
        // 1. Enable message validation feature.
        services.AddGrpc(options => options.EnableMessageValidation());

        // 2. Add inline validators for messages, scope is always singleton
        services.AddInlineValidator<HelloRequest>(rules => rules.RuleFor(request => request.Name).NotEmpty());
    }
    // ...
}

Profiling validators

If you don't want to make a mess your startup class by registering validators, you implement validator profile and use it.

public class SampleProfile : ValidatorProfileBase
{
    public SampleProfile()
    {
        CreateInlineValidator<SampleRequest>()
            .RuleFor(r => r.Data).NotNull().NotEmpty()
            .RuleFor(r => r.Name).NotNull();
        AddValidator<SampleRequestValidator>();
        AddValidator<SampleRequestValidator>(ServiceLifetime.Singleton);
    }
}

// Then in your Startup class
public void ConfigureServices(IServiceCollection services)
{
    services.AddGrpc(options => options.EnableMessageValidation());
    services.AddValidatorProfile<SampleProfile>();
}

Scan and register profiles/validators from assembly.

// Place somewhere validator or profile class.
public class HelloRequestValidator : AbstractValidator<HelloRequest>
{
    public HelloRequestValidator()
    {
        RuleFor(request => request.Name).NotEmpty();
    }
}

public class SampleProfile : ValidatorProfileBase
{
    public SampleProfile()
    {
        CreateInlineValidator<SampleRequest>()
            .RuleFor(r => r.Data).NotNull().NotEmpty()
            .RuleFor(r => r.Name).NotNull();
        AddValidator<SampleRequestValidator>();
        AddValidator<SampleRequestValidator>(ServiceLifetime.Singleton);
    }
}

// Then in your Startup class
public void ConfigureServices(IServiceCollection services)
{
    services.AddGrpc(options => options.EnableMessageValidation());

    // Scan profile and validators from calling assembly.
    services.AddValidatorsFromAssemblies(); 
    services.AddProfilesFromAssembly();

    // Scan profiles and validators from specific assembly.
    services.AddValidatorsFromAssemblies(typeof(InternalLibarary).Assembly); 
    services.AddProfilesFromAssembly(typeof(InternalLibarary).Assembly);
}

Customize validation failure message.

If you want to custom validation message handler for using your own error message system, Just implement IValidatorErrorMessageHandler and put it service collection.

public class CustomMessageHandler : IValidatorErrorMessageHandler
{
    public Task<string> HandleAsync(IList<ValidationFailure> failures)
    {
        return Task.FromResult("Validation Error!");
    }
}

public class Startup
{
    // ...
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddGrpc(options => options.EnableMessageValidation());

        // Just put at service collection your own custom message handler that implement IValidatorErrorMessageHnadler.
        // This should be placed before calling AddInlineValidator() or AddValidator();
        services.AddSingleton<IValidatorErrorMessageHanlder>(new CustomMessageHandler())
        services.AddInlineValidator<HelloRequest>(rules => rules.RuleFor(request => request.Name).NotEmpty());
    }
    // ...
}

How to test my validation

If you want to write integration tests. This test sample may help you.

Versioning

This pakage`s versioning is following version of Grpc.AspNetCore

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].