Skip to content

Commit

Permalink
Add OcpiValidate override with arbitrary OcpiVersion
Browse files Browse the repository at this point in the history
  • Loading branch information
YuriyDurov committed Jan 11, 2024
1 parent ed2b9dc commit 7ebbab7
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 8 deletions.
21 changes: 21 additions & 0 deletions src/OCPI.Net.Controllers/Models/OcpiControllerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Microsoft.Extensions.DependencyInjection;
using OCPI.Contracts;
using OCPI.Services;
using OCPI.Validation;

namespace OCPI;

Expand All @@ -24,6 +25,26 @@ public OkObjectResult OcpiOk([ActionResultObjectValue] object? value)
return base.Ok(new OcpiResponse(value));
}

[NonAction]
public void OcpiValidate<T>(T value, OcpiVersion forOcpiVersion)
{
if (value is null) return;

var validationContext = GetRequiredService<OcpiValidationContext>();
validationContext.OcpiVersion = forOcpiVersion;

var validator = GetRequiredService<OcpiValidator<T>>();
var validationResult = validator.Validate(value);

if (!validationResult.IsValid)
{
var exception = OcpiException.InvalidParameters();
var errors = validationResult.Errors.Select(x => x.ErrorMessage);
exception.Payload.AddData(new { errors });
throw exception;
}
}

[NonAction]
public void OcpiValidate<T>(T value)
{
Expand Down
17 changes: 11 additions & 6 deletions src/OCPI.Net.Validation/Extensions/AddOcpiValidationExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using OCPI.Contracts;
using OCPI.Validation;

namespace OCPI;

Expand All @@ -16,6 +17,14 @@ public static WebApplicationBuilder AddOcpiValidation(this WebApplicationBuilder
builder.Services.AddHttpContextAccessor();
builder.Services.AddTransient(x => x.GetRequiredService<IHttpContextAccessor>()!.HttpContext!);

builder.Services.AddScoped(x =>
{
var httpContext = x.GetRequiredService<HttpContext>();
var request = httpContext.Request;

return new OcpiValidationContext(request);
});

var validatorTypes = typeof(IOcpiValidatorsAssemblyPointer)
.Assembly.DefinedTypes
.Where(x => !x.IsAbstract)
Expand All @@ -28,12 +37,8 @@ public static WebApplicationBuilder AddOcpiValidation(this WebApplicationBuilder
var resultingType = typeof(OcpiValidator<>).MakeGenericType(modelType);
builder.Services.AddScoped(resultingType, x =>
{
var httpContext = x.GetRequiredService<HttpContext>();
var request = httpContext.Request;

var ocpiVersion = request.GetCurrentOcpiVersion();
var actionType = request.Method.ToActionType();
var instance = Activator.CreateInstance(type, actionType, ocpiVersion);
var validationContext = x.GetRequiredService<OcpiValidationContext>();
var instance = Activator.CreateInstance(type, validationContext.ActionType, validationContext.OcpiVersion);
return instance!;
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,24 @@ namespace OCPI;

internal static class GetCurrentOcpiVersionExtension
{
public static OcpiVersion GetCurrentOcpiVersion(this HttpRequest request)
public static OcpiVersion? GetCurrentOcpiVersion(this HttpRequest request)
{
var path = request.Path.Value!.TrimStart('/');
var versionLength = path.IndexOf('/');
if (versionLength == -1) versionLength = path.Length;

var versionString = path[..versionLength];

var result = versionString.ToEnum<OcpiVersion>();
OcpiVersion? result = null;

try
{
result = versionString.ToEnum<OcpiVersion>();
}
catch (Exception)
{
}

return result;
}
}
20 changes: 20 additions & 0 deletions src/OCPI.Net.Validation/Models/OcpiValidationContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using FluentValidation;
using Microsoft.AspNetCore.Http;

namespace OCPI.Validation;

public class OcpiValidationContext
{
public ActionType ActionType { get; set; }
public OcpiVersion? OcpiVersion { get; set; }

public OcpiValidationContext(ActionType actionType, OcpiVersion? ocpiVersion)
{
ActionType = actionType;
OcpiVersion = ocpiVersion;
}

public OcpiValidationContext(HttpRequest request)
: this(request.Method.ToActionType(), request.GetCurrentOcpiVersion())
{ }
}

0 comments on commit 7ebbab7

Please sign in to comment.