From b0d5261a0aa3151d20c4d17cc779ec0fac42fee4 Mon Sep 17 00:00:00 2001 From: Tobias Klika Date: Tue, 8 Sep 2020 14:33:31 +0200 Subject: [PATCH] create base class for validators --- zero.Core/Api/CountriesApi.cs | 4 +-- zero.Core/Validation/CountryValidator.cs | 4 +-- zero.Core/Validation/ZeroValidator.cs | 34 +++++++++++++++++++++++ zero.Debug/Controllers/TestController.cs | 4 +++ zero.Web.UI/App/components/forms/form.vue | 4 +++ zero.Web/Defaults/ZeroBackofficePlugin.cs | 4 +-- 6 files changed, 47 insertions(+), 7 deletions(-) create mode 100644 zero.Core/Validation/ZeroValidator.cs diff --git a/zero.Core/Api/CountriesApi.cs b/zero.Core/Api/CountriesApi.cs index 3f7bcead..1adf2bcf 100644 --- a/zero.Core/Api/CountriesApi.cs +++ b/zero.Core/Api/CountriesApi.cs @@ -16,10 +16,10 @@ namespace zero.Core.Api IValidator Validator; - public CountriesApi(IBackofficeStore store) : base(store) + public CountriesApi(IBackofficeStore store, IValidator validator) : base(store) { Scope.IncludeShared = true; - Validator = null; + Validator = validator; } diff --git a/zero.Core/Validation/CountryValidator.cs b/zero.Core/Validation/CountryValidator.cs index 3e621b26..de72a105 100644 --- a/zero.Core/Validation/CountryValidator.cs +++ b/zero.Core/Validation/CountryValidator.cs @@ -1,11 +1,9 @@ using FluentValidation; using zero.Core.Entities; -using zero.Core.Entities.Setup; -using zero.Core.Extensions; namespace zero.Core.Validation { - public class CountryValidator : AbstractValidator + public class CountryValidator : ZeroValidator { public CountryValidator() { diff --git a/zero.Core/Validation/ZeroValidator.cs b/zero.Core/Validation/ZeroValidator.cs new file mode 100644 index 00000000..54ebfbfe --- /dev/null +++ b/zero.Core/Validation/ZeroValidator.cs @@ -0,0 +1,34 @@ +using System; +using FluentValidation; +using FluentValidation.Results; +using System.Threading; +using System.Threading.Tasks; + +namespace zero.Core.Validation +{ + public abstract class ZeroValidator : AbstractValidator, IValidator where TImplementation : TInterface + { + public ZeroValidator() + { + + } + + public ValidationResult Validate(TInterface instance) + { + if (!(instance is TImplementation)) + { + throw new ArgumentException($"Parameter {nameof(instance)} has to be of type {typeof(TImplementation)}."); + } + return base.Validate((TImplementation)instance); + } + + public Task ValidateAsync(TInterface instance, CancellationToken cancellation = default) + { + if (!(instance is TImplementation)) + { + throw new ArgumentException($"Parameter {nameof(instance)} has to be of type {typeof(TImplementation)}."); + } + return base.ValidateAsync((TImplementation)instance, cancellation); + } + } +} diff --git a/zero.Debug/Controllers/TestController.cs b/zero.Debug/Controllers/TestController.cs index 3a08071a..276c9bef 100644 --- a/zero.Debug/Controllers/TestController.cs +++ b/zero.Debug/Controllers/TestController.cs @@ -2,6 +2,7 @@ using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Infrastructure; using Microsoft.Extensions.DependencyInjection; +using Newtonsoft.Json; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; @@ -49,6 +50,9 @@ namespace zero.Debug.Controllers { count = items.Count, items + }, new JsonSerializerSettings() + { + TypeNameHandling = TypeNameHandling.None }); } diff --git a/zero.Web.UI/App/components/forms/form.vue b/zero.Web.UI/App/components/forms/form.vue index e29359d5..79434a17 100644 --- a/zero.Web.UI/App/components/forms/form.vue +++ b/zero.Web.UI/App/components/forms/form.vue @@ -313,6 +313,10 @@ handledGroups.push(field); component.set(errorGroup); } + else + { + component.clear(); + } } else { diff --git a/zero.Web/Defaults/ZeroBackofficePlugin.cs b/zero.Web/Defaults/ZeroBackofficePlugin.cs index 4e46715e..b433672f 100644 --- a/zero.Web/Defaults/ZeroBackofficePlugin.cs +++ b/zero.Web/Defaults/ZeroBackofficePlugin.cs @@ -5,6 +5,7 @@ using zero.Core.Entities; using zero.Core.Extensions; using zero.Core.Options; using zero.Core.Plugins; +using zero.Core.Validation; using zero.Web.Mapper; using zero.Web.Sections; @@ -15,7 +16,7 @@ namespace zero.Web.Defaults public void ConfigureServices(IServiceCollection services) { services.AddAll(typeof(IValidator<>), ServiceLifetime.Scoped); - services.AddAll(typeof(IValidator), ServiceLifetime.Scoped); + //services.AddAll(typeof(IValidator), ServiceLifetime.Scoped); services.AddTransient(); //services.AddTransient, ApplicationValidator>(); @@ -28,7 +29,6 @@ namespace zero.Web.Defaults services.AddTransient(); services.AddTransient(); - services.AddTransient(); services.AddTransient(); services.AddTransient();