mirror of
https://github.com/aljazceru/btcpayserver.git
synced 2026-01-06 15:44:26 +01:00
Make sure model binder error are returning error 422, use DateTimeOffsetModelBinder
This commit is contained in:
46
BTCPayServer/ModelBinders/DateTimeOffsetModelBinder.cs
Normal file
46
BTCPayServer/ModelBinders/DateTimeOffsetModelBinder.cs
Normal file
@@ -0,0 +1,46 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Mvc.ModelBinding;
|
||||
using Microsoft.VisualBasic.CompilerServices;
|
||||
|
||||
namespace BTCPayServer.ModelBinders
|
||||
{
|
||||
public class DateTimeOffsetModelBinder : IModelBinder
|
||||
{
|
||||
public Task BindModelAsync(ModelBindingContext bindingContext)
|
||||
{
|
||||
if (!typeof(DateTimeOffset).GetTypeInfo().IsAssignableFrom(bindingContext.ModelType) &&
|
||||
!typeof(DateTimeOffset?).GetTypeInfo().IsAssignableFrom(bindingContext.ModelType))
|
||||
{
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
ValueProviderResult val = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
|
||||
if (val == null)
|
||||
{
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
string v = val.FirstValue as string;
|
||||
if (v == null)
|
||||
{
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var sec = long.Parse(v, CultureInfo.InvariantCulture);
|
||||
bindingContext.Result = ModelBindingResult.Success(NBitcoin.Utils.UnixTimeToDateTime(sec));
|
||||
}
|
||||
catch
|
||||
{
|
||||
bindingContext.Result = ModelBindingResult.Failed();
|
||||
bindingContext.ModelState.AddModelError(bindingContext.ModelName, "Invalid unix timestamp");
|
||||
}
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user