From 297b84a18b0c1657a317164f304c1806d3ba435e Mon Sep 17 00:00:00 2001 From: Kukks Date: Wed, 22 Apr 2020 14:42:30 +0200 Subject: [PATCH] add request name validation --- .../Controllers/GreenField/StoresController.cs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/BTCPayServer/Controllers/GreenField/StoresController.cs b/BTCPayServer/Controllers/GreenField/StoresController.cs index 48bf9be92..1c14c2d19 100644 --- a/BTCPayServer/Controllers/GreenField/StoresController.cs +++ b/BTCPayServer/Controllers/GreenField/StoresController.cs @@ -9,6 +9,7 @@ using BTCPayServer.Services.Stores; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.ModelBinding; namespace BTCPayServer.Controllers.GreenField { @@ -67,8 +68,8 @@ namespace BTCPayServer.Controllers.GreenField [Authorize(Policy = Policies.CanModifyStoreSettings, AuthenticationSchemes = AuthenticationSchemes.Greenfield)] public async Task> CreateStore(CreateStoreRequest request) { - if (request is null) - return BadRequest(); + if (request?.Name is null) + return BadRequest(CreateValidationProblem(nameof(request.Name), "Name is missing")); var store = await _storeRepository.CreateStore(_userManager.GetUserId(User), request.Name); return Ok(FromModel(store)); } @@ -81,5 +82,12 @@ namespace BTCPayServer.Controllers.GreenField Name = data.StoreName }; } + + private ValidationProblemDetails CreateValidationProblem(string propertyName, string errorMessage) + { + var modelState = new ModelStateDictionary(); + modelState.AddModelError(propertyName, errorMessage); + return new ValidationProblemDetails(modelState); + } } }