From 894644abee5e5d38fec3dcc1614bf13bc57efe13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ole=20J=C3=B8rgen=20Skogstad?= Date: Mon, 12 Feb 2024 14:44:43 +0100 Subject: [PATCH] fix: Check Content for null, use DependentRules, disallow empty localization values (#413) #375 --- .../Localizations/LocalizationDtoValidator.cs | 1 + .../Create/CreateDialogCommandValidator.cs | 20 ++++++++++++------- .../Update/UpdateDialogCommandValidator.cs | 20 ++++++++++++------- 3 files changed, 27 insertions(+), 14 deletions(-) diff --git a/src/Digdir.Domain.Dialogporten.Application/Features/V1/Common/Localizations/LocalizationDtoValidator.cs b/src/Digdir.Domain.Dialogporten.Application/Features/V1/Common/Localizations/LocalizationDtoValidator.cs index 91cad69a6..ba0368bc6 100644 --- a/src/Digdir.Domain.Dialogporten.Application/Features/V1/Common/Localizations/LocalizationDtoValidator.cs +++ b/src/Digdir.Domain.Dialogporten.Application/Features/V1/Common/Localizations/LocalizationDtoValidator.cs @@ -21,6 +21,7 @@ public LocalizationDtoValidator(int maximumLength = 255) RuleFor(x => x).NotNull(); RuleFor(x => x.Value) + .NotEmpty() .NotNull() .MaximumLength(maximumLength); diff --git a/src/Digdir.Domain.Dialogporten.Application/Features/V1/ServiceOwner/Dialogs/Commands/Create/CreateDialogCommandValidator.cs b/src/Digdir.Domain.Dialogporten.Application/Features/V1/ServiceOwner/Dialogs/Commands/Create/CreateDialogCommandValidator.cs index 32cdd0869..aa866fbea 100644 --- a/src/Digdir.Domain.Dialogporten.Application/Features/V1/ServiceOwner/Dialogs/Commands/Create/CreateDialogCommandValidator.cs +++ b/src/Digdir.Domain.Dialogporten.Application/Features/V1/ServiceOwner/Dialogs/Commands/Create/CreateDialogCommandValidator.cs @@ -63,13 +63,19 @@ public CreateDialogCommandValidator( .IsInEnum(); RuleFor(x => x.Content) - .UniqueBy(x => x.Type) - .Must(content => DialogContentType.RequiredTypes - .All(requiredContent => content - .Select(x => x.Type) - .Contains(requiredContent))) - .WithMessage($"Dialog must contain the following content: [{string.Join(", ", DialogContentType.RequiredTypes)}].") - .ForEach(x => x.SetValidator(contentValidator)); + .NotNull() + .DependentRules(() => + { + RuleFor(x => x.Content) + .UniqueBy(x => x.Type) + .Must(content => DialogContentType.RequiredTypes + .All(requiredContent => content + .Select(x => x.Type) + .Contains(requiredContent))) + .WithMessage("Dialog must contain the following content: " + + $"[{string.Join(", ", DialogContentType.RequiredTypes)}].") + .ForEach(x => x.SetValidator(contentValidator)); + }); RuleForEach(x => x.SearchTags) .SetValidator(searchTagValidator); diff --git a/src/Digdir.Domain.Dialogporten.Application/Features/V1/ServiceOwner/Dialogs/Commands/Update/UpdateDialogCommandValidator.cs b/src/Digdir.Domain.Dialogporten.Application/Features/V1/ServiceOwner/Dialogs/Commands/Update/UpdateDialogCommandValidator.cs index 43735b51d..2510b8533 100644 --- a/src/Digdir.Domain.Dialogporten.Application/Features/V1/ServiceOwner/Dialogs/Commands/Update/UpdateDialogCommandValidator.cs +++ b/src/Digdir.Domain.Dialogporten.Application/Features/V1/ServiceOwner/Dialogs/Commands/Update/UpdateDialogCommandValidator.cs @@ -56,13 +56,19 @@ public UpdateDialogDtoValidator( .IsInEnum(); RuleFor(x => x.Content) - .UniqueBy(x => x.Type) - .Must(content => DialogContentType.RequiredTypes - .All(requiredContent => content - .Select(x => x.Type) - .Contains(requiredContent))) - .WithMessage($"Dialog must contain the following content: [{string.Join(", ", DialogContentType.RequiredTypes)}].") - .ForEach(x => x.SetValidator(contentValidator)); + .NotNull() + .DependentRules(() => + { + RuleFor(x => x.Content) + .UniqueBy(x => x.Type) + .Must(content => DialogContentType.RequiredTypes + .All(requiredContent => content + .Select(x => x.Type) + .Contains(requiredContent))) + .WithMessage("Dialog must contain the following content: " + + $"[{string.Join(", ", DialogContentType.RequiredTypes)}].") + .ForEach(x => x.SetValidator(contentValidator)); + }); RuleFor(x => x.SearchTags) .UniqueBy(x => x.Value, StringComparer.InvariantCultureIgnoreCase)