create base class for validators

This commit is contained in:
2020-09-08 14:33:31 +02:00
parent 2246ac9ee0
commit b0d5261a0a
6 changed files with 47 additions and 7 deletions
+2 -2
View File
@@ -16,10 +16,10 @@ namespace zero.Core.Api
IValidator<ICountry> Validator;
public CountriesApi(IBackofficeStore store) : base(store)
public CountriesApi(IBackofficeStore store, IValidator<ICountry> validator) : base(store)
{
Scope.IncludeShared = true;
Validator = null;
Validator = validator;
}
+1 -3
View File
@@ -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<Country>
public class CountryValidator : ZeroValidator<ICountry, Country>
{
public CountryValidator()
{
+34
View File
@@ -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<TInterface, TImplementation> : AbstractValidator<TImplementation>, IValidator<TInterface> 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<ValidationResult> 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);
}
}
}
+4
View File
@@ -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
});
}
@@ -313,6 +313,10 @@
handledGroups.push(field);
component.set(errorGroup);
}
else
{
component.clear();
}
}
else
{
+2 -2
View File
@@ -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<IApplication, Application>();
//services.AddTransient<IValidator<IApplication>, ApplicationValidator>();
@@ -28,7 +29,6 @@ namespace zero.Web.Defaults
services.AddTransient<IMediaFolder, MediaFolder>();
services.AddTransient<IPreview, Preview>();
services.AddTransient<IApplicationsApi, ApplicationsApi>();
services.AddTransient<ICountriesApi, CountriesApi>();
services.AddTransient<ILanguagesApi, LanguagesApi>();