RePolicies 1.0.0

dotnet add package RePolicies --version 1.0.0
                    
NuGet\Install-Package RePolicies -Version 1.0.0
                    
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="RePolicies" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="RePolicies" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="RePolicies" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add RePolicies --version 1.0.0
                    
#r "nuget: RePolicies, 1.0.0"
                    
#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.
#:package RePolicies@1.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=RePolicies&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=RePolicies&version=1.0.0
                    
Install as a Cake Tool

RePolicies

A runtime-reconfigurable policy engine built on top of ReValidator.

ReValidator validates a request DTO through IValidator<T>. RePolicies reuses that engine, but the unit of evaluation becomes a Policy<TModel> — an envelope that carries the payload (TModel) together with the surrounding request context: headers, cookies, the ClaimsPrincipal, the URL and its route/query parameters.

A policy rule is therefore just IValidator<Policy<TModel>>. Because the rules are ReValidator DynamicReconfiguration entries under the hood, they are compiled expression trees (fast) and can be added or replaced at runtime without a redeploy.

Using ASP.NET Core? Add RePolicies.AspNetCore to build a Policy<TModel> from the current HttpContext and enforce policies via an endpoint filter or global middleware.


Install

dotnet add package RePolicies

Why Policy<TModel>?

A plain validator can only see the body. Authorization-style policies usually need more:

// "Large orders need the Manager role"
x => x.Model.Total <= 10000 || x.IsInRole("Manager")

// "Tenant header must be present"
x => x.HasHeader("X-Tenant-Id")

// "Caller must be authenticated"
x => x.IsAuthenticated

x is the Policy<TModel>. The helper methods (IsInRole, HasHeader, HasClaim, Query, Route, Cookie, ...) are instance members on Policy<TModel>, and x.Model reaches the payload. RePolicies registers each Policy<TModel> with ReValidator automatically, so these helpers are callable inside expressions with no extra setup.

Helpers available inside expressions

Member Meaning
Model The payload (TModel).
IsAuthenticated The principal has an authenticated identity.
IsInRole(role) The principal is in role.
HasClaim(type) / HasClaim(type, value) The principal has the claim.
Claim(type) First claim value of type, or "".
HasHeader(name) / Header(name) Header presence / value ("" if absent).
HasCookie(name) / Cookie(name) Cookie presence / value.
HasQuery(name) / Query(name) Query-parameter presence / value.
Route(name) Route value, or "".
Method, Path, Scheme, Host, Url Request line / URL info.

Getting started

using RePolicies.Abstractions;
using RePolicies.DependencyInjection;

// 1. Register ReValidator + the engine.
builder.Services.AddRePolicies();   // calls AddReValidator() for you

// 2. Configure initial policies for a model.
builder.Services
    .AddPolicy<CreateOrderRequest>(new PolicyDefinition
    {
        Name = "OrderAmountWithinLimit",
        Expression = "x => x.Model.Total <= 10000 || x.IsInRole(\"Manager\")",
        DenialReason = "Orders above 10,000 require the Manager role."
    })
    .AddPolicy<CreateOrderRequest>(new PolicyDefinition
    {
        Name = "PositiveQuantity",
        Expression = "x => x.Model.Quantity > 0",
        DenialReason = "Quantity must be greater than zero."
    });

Evaluate

public sealed class OrderService(IPolicyEngine engine)
{
    public PolicyEvaluationResult Check(CreateOrderRequest request, ClaimsPrincipal user)
    {
        var policy = new Policy<CreateOrderRequest>(request) { User = user };
        var result = engine.Evaluate(policy);

        // result.IsSatisfied / result.IsDenied
        // result.Failures -> { PolicyName, Reasons }
        return result;
    }
}

EnsureSatisfied(policy) is a throwing variant — it raises PolicyViolationException (carrying Failures) when any policy is denied.

Reconfigure at runtime

// Add or replace a policy without a restart.
reconfigurator.Configure<CreateOrderRequest>(new PolicyDefinition
{
    Name = "OrderAmountWithinLimit",
    Expression = "x => x.Model.Total <= 50000 || x.IsInRole(\"Manager\")",
    DenialReason = "Orders above 50,000 require the Manager role."
});

// Introspect what is currently configured.
IReadOnlyCollection<PolicyDefinition> current =
    reconfigurator.GetPolicies<CreateOrderRequest>();

Need helper types in expressions?

Register them with ReValidator first, then tell AddRePolicies not to re-register it:

builder.Services.AddReValidator(o => o.RegisterType<PolicyHelpers>());
builder.Services.AddRePolicies(registerReValidator: false);

Key types

Type Role
Policy<TModel> The evaluation envelope (payload + request context).
PolicyDefinition A named, reconfigurable rule (Name, Expression, DenialReason, Description).
IPolicyEngine Evaluate / EnsureSatisfied.
IPolicyReconfigurator Add / replace / introspect policies at runtime.
IPolicyStore The introspectable record of configured policies.
IReValidatorBridge The single seam onto ReValidator's reconfiguration API.

Note: ReValidator keys compiled rules by the model type's full name in process-global state, so all Policy<TModel> evaluations for a given TModel share one rule set across the process (not per DI container).


License

MIT.

Product Compatible and additional computed target framework versions.
.NET 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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on RePolicies:

Package Downloads
RePolicies.AspNetCore

ASP.NET Core integration for RePolicies: builds Policy<TModel> from HttpContext and enforces policies via an endpoint filter or global middleware.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0 132 6/24/2026