From 47af4e5e0dc247d76f6b35554c95399394e873ec Mon Sep 17 00:00:00 2001 From: Tobias Klika Date: Sun, 23 Jan 2022 19:53:59 +0100 Subject: [PATCH] validation for integrations --- .../Integrations/IntegrationStore.cs | 68 ++++++++++--------- 1 file changed, 37 insertions(+), 31 deletions(-) diff --git a/zero.Core/Configuration/Integrations/IntegrationStore.cs b/zero.Core/Configuration/Integrations/IntegrationStore.cs index 88a664c6..d00248ca 100644 --- a/zero.Core/Configuration/Integrations/IntegrationStore.cs +++ b/zero.Core/Configuration/Integrations/IntegrationStore.cs @@ -1,4 +1,5 @@ -using FluentValidation.Results; +using FluentValidation; +using FluentValidation.Results; using Raven.Client.Documents; using Raven.Client.Documents.Linq; @@ -109,43 +110,48 @@ public class IntegrationStore : IIntegrationStore // - protected virtual async Task Validate(Integration model) + protected virtual async Task Validate(Integration model, bool create = false) { - await Task.Delay(0); - return new ValidationResult(); - //ZeroValidator validator = new(); - //ValidationRules(validator); - //return await validator.ValidateAsync(model); - //return base.Validate(model); + ValidationResult result = new(); + + if (model == null) + { + result.Errors.Add(new ValidationFailure("__nofield", "@integration.errors.notfound")); + } + else + { + IntegrationType type = IntegrationTypes.GetByAlias(model.Flavor); + + if (type == null) + { + result.Errors.Add(new ValidationFailure("__nofield", "@integration.errors.typenotfound")); + } + + if (create && await Operations.Any(q => q.Where(x => x.Flavor == model.Flavor))) + { + result.Errors.Add(new ValidationFailure("__nofield", "@integration.errors.multiplenotallowed")); + } + + if (!create && await Operations.Any(q => q.Where(x => x.Flavor == model.Flavor && x.Id != model.Id))) + { + result.Errors.Add(new ValidationFailure("__nofield", "@integration.errors.alreadycreated")); + } + + if (type != null && type.Validator != null) + { + ValidationResult innerResult = await type.Validator.ValidateAsync(new ValidationContext(model)); + result.Errors.AddRange(innerResult.Errors); + } + } + + return result; } /// protected virtual async Task> Save(Integration model, bool create = false) { - if (model == null) - { - return Result.Fail("@integration.errors.notfound"); - } - - IntegrationType type = IntegrationTypes.GetByAlias(model.Flavor); - - if (type == null) - { - return Result.Fail("@integration.errors.typenotfound"); - } - - if (create && await Operations.Any(q => q.Where(x => x.Flavor == model.Flavor))) - { - return Result.Fail("@integration.errors.multiplenotallowed"); - } - - if (!create && await Operations.Any(q => q.Where(x => x.Flavor == model.Flavor && x.Id != model.Id))) - { - return Result.Fail("@integration.errors.alreadycreated"); - } - - return await Operations.Create(model, async (x, ctx) => await Validate(x)); + return await Operations.Create(model, async (x, ctx) => await Validate(x, create)); } }