ProtoValidate 0.3.1

dotnet add package ProtoValidate --version 0.3.1                
NuGet\Install-Package ProtoValidate -Version 0.3.1                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="ProtoValidate" Version="0.3.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ProtoValidate --version 0.3.1                
#r "nuget: ProtoValidate, 0.3.1"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install ProtoValidate as a Cake Addin
#addin nuget:?package=ProtoValidate&version=0.3.1

// Install ProtoValidate as a Cake Tool
#tool nuget:?package=ProtoValidate&version=0.3.1                

protovalidate-net

protovalidate-net is the C# implementation of protovalidate, designed to validate Protobuf messages at runtime based on user-defined validation constraints. Powered by Google's Common Expression Language (CEL), it provides a flexible and efficient foundation for defining and evaluating custom validation rules. The primary goal of protovalidate is to help developers ensure data consistency and integrity across the network without requiring generated code.

The protovalidate project

Head over to the core protovalidate repository for:

Other protovalidate runtime implementations include:

Installation

To install the package, use nuget:

nuget install ProtoValidate

Usage

This example shows how to use ProtoValidate-net with dependency injection Register ProtoValidate in your service collection and configure the options.

using ProtoValidate;

// register ProtoValidate with no options
serviceCollection.AddProtoValidate();

// OR register ProtoValidate with options
serviceCollection.AddProtoValidate(options => {       
    // This setting is used to configure if it loads your validation descriptors upon creation of the validator.
    // True will load on creation
    // False will defer loading the validator until first run of the validation logic for that type.
    options.PreLoadDescriptors = true;

    // This setting will cause a compilation exception to be thrown if the message type you are validating hasn't been pre-loaded using the file descriptor list.
    options.DisableLazy = true;

    //register your file descriptors generated by Google.Protobuf library for your compiled .proto files
    options.FileDescriptors = new List<FileDescriptor>() {
        //your list of Protobuf File Descriptors here
    }
});

Example on how to validate a message using the IValidator instance from the service provider

// define your Protobuf message that needs validation
var myMessageToValidate = new MyMessageThatNeedsValidation() {...};

// resolve IValidator from your service provider
// you can resolve it directly like this, or from the constructor using dependency injection
var validator = serviceProvider.GetRequiredService<ProtoValidate.IValidator>();

// flag to indicate if the validator should return on the first error (true) or validate all the fields and return all the errors in the message (false).
var failFast = true;

//validate the message
var violations = validator.Validate(myMessageToValidate, failFast);

//the violations contains the validation errors.
var hasViolations = violations.Violations.Count > 0;

Example on how to create a validator and validate a message without using dependency injection.


var validatorOptions = new ProtoValidate.ValidatorOptions() {
    // This setting is used to configure if it loads your validation descriptors upon creation of the validator.
    // True will load on creation
    // False will defer loading the validator until first run of the validation logic for that type.
    PreLoadDescriptors = true,

    // This setting will cause a compilation exception to be thrown if the message type you are validating hasn't been pre-loaded using the file descriptor list.
    DisableLazy = true,

    //register your file descriptors generated by Google.Protobuf library for your compiled .proto files
    FileDescriptors = new List<FileDescriptor>() {
        //your list of Protobuf File Descriptors here
    }
};

//Instantiate the validator.  You should cache the validator for reuse.
var validator = new ProtoValidate.Validator(validatorOptions);

// flag to indicate if the validator should return on the first error (true) or validate all the fields and return all the errors in the message (false).
var failFast = true;

// define your Protobuf message that needs validation
var myMessageToValidate = new MyMessageThatNeedsValidation() {...};

//validate the message
var violations = validator.Validate(myMessageToValidate, failFast);

//the violations contains the validation errors.
var hasViolations = violations.Violations.Count > 0;

Implementing validation constraints

Validation constraints are defined directly within .proto files. Documentation for adding constraints can be found in the protovalidate project README and its comprehensive docs.

syntax = "proto3";

package my.package;

import "google/protobuf/timestamp.proto";
import "buf/validate/validate.proto";

message Transaction {
  uint64 id = 1 [(buf.validate.field).uint64.gt = 999];
  google.protobuf.Timestamp purchase_date = 2;
  google.protobuf.Timestamp delivery_date = 3;

  string price = 4 [(buf.validate.field).cel = {
    id: "transaction.price",
    message: "price must be positive and include a valid currency symbol ($ or £)",
    expression: "(this.startswith('$') or this.startswith('£')) and float(this[1:]) > 0"
  }];

  option (buf.validate.message).cel = {
    id: "transaction.delivery_date",
    message: "delivery date must be after purchase date",
    expression: "this.delivery_date > this.purchase_date"
  };
}

Ecosystem

Offered under the Apache 2 license.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (4)

Showing the top 4 NuGet packages that depend on ProtoValidate:

Package Downloads
ADuke.ServiceHub.AKSMiddleware

Package Description

ServiceHub.ApiV1

Package Description

ServiceHub.AKSMiddleware

Package Description

ServiceHub.MyGreeterV3.ApiV1

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.3.1 2,559 11/5/2024
0.1.5 1,661 8/16/2024
0.1.4 765 8/12/2024
0.1.3 11,668 12/1/2023
0.1.2 165 11/22/2023
0.1.1 146 11/22/2023
0.1.0 134 11/20/2023