diff --git a/src/Tests/ViewModel/ViewModelValidatorTests.cs b/src/Tests/ViewModel/ViewModelValidatorTests.cs index b0f1cdb4a8..f73d109472 100644 --- a/src/Tests/ViewModel/ViewModelValidatorTests.cs +++ b/src/Tests/ViewModel/ViewModelValidatorTests.cs @@ -12,6 +12,7 @@ using DotVVM.Framework.Testing; using Microsoft.Extensions.DependencyInjection; using DotVVM.Framework.Hosting; +using Microsoft.Extensions.Logging; namespace DotVVM.Framework.Tests.ViewModel { @@ -41,15 +42,8 @@ public void ViewModelValidator_SimpleObject() [TestMethod] public void ViewModelValidator_ObjectWithCollection() { - var testViewModel = new TestViewModel() - { - Children = new List() - { - new TestViewModel2() { Code = "012" }, - new TestViewModel2() { Code = "ABC", Id = 13 }, - new TestViewModel2() { Code = "345", Id = 15 } - }, - Child = new TestViewModel2() { Code = "123" } + var testViewModel = new TestViewModel { + Children = new List { new() { Code = "012" }, new() { Code = "ABC", Id = 13 }, new() { Code = "345", Id = 15 } }, Child = new TestViewModel2 { Code = "123" } }; var validator = CreateValidator(); var expander = CreateErrorPathExpander(); @@ -105,15 +99,7 @@ public void ViewModelValidator_WithValidationTarget_Property() [TestMethod] public void ViewModelValidator_WithValidationTarget_ArrayElement() { - var testViewModel = new TestViewModel() - { - Children = new List() - { - new TestViewModel2() { Code = "5" }, - new TestViewModel2() { Code = "6" }, - new TestViewModel2() { Code = "7" }, - } - }; + var testViewModel = new TestViewModel { Children = new List { new() { Code = "5" }, new() { Code = "6" }, new() { Code = "7" } } }; var validator = CreateValidator(); var expander = CreateErrorPathExpander(); var modelState = new ModelState() { ValidationTarget = testViewModel.Children[1] }; @@ -184,10 +170,7 @@ public void ViewModelValidator_Child_IValidatableObject() [TestMethod] public void ViewModelValidator_CollectionOfIValidatableObjects() { - var testViewModel = new TestViewModel6() - { - Children = new List() {new TestViewModel5Child() {IsChecked = true}} - }; + var testViewModel = new TestViewModel6 { Children = new List { new() { IsChecked = true } } }; var validator = CreateValidator(); var expander = CreateErrorPathExpander(); var modelState = new ModelState() { ValidationTarget = testViewModel }; @@ -221,19 +204,9 @@ public void ViewModelValidator_ValidationErrorFactoryWithValidationContextSuppli [TestMethod] public void ViewModelValidator_CustomModelStateErrors() { - var testViewModel = new TestViewModel() - { - Context = new TestDotvvmRequestContext - { - Configuration = DotvvmTestHelper.DefaultConfig, - ModelState = new ModelState(), - RequestType = DotvvmRequestType.Command - }, - Child = new TestViewModel2() - { - Id = 11, - Code = "Code", - }, + var testViewModel = new TestViewModel { + Context = new TestDotvvmRequestContext { Configuration = DotvvmTestHelper.DefaultConfig, ModelState = new ModelState(), RequestType = DotvvmRequestType.Command }, + Child = new TestViewModel2 { Id = 11, Code = "Code" } }; var validator = CreateValidator(); var expander = CreateErrorPathExpander(); @@ -257,19 +230,9 @@ public void ViewModelValidator_CustomModelStateErrors() [ExpectedException(typeof(ArgumentException))] public void ViewModelValidator_CustomModelStateErrors_OldFormatThrows() { - var testViewModel = new TestViewModel() - { - Context = new TestDotvvmRequestContext - { - Configuration = DotvvmTestHelper.DefaultConfig, - ModelState = new ModelState(), - RequestType = DotvvmRequestType.Command - }, - Child = new TestViewModel2() - { - Id = 11, - Code = "Code", - }, + var testViewModel = new TestViewModel { + Context = new TestDotvvmRequestContext { Configuration = DotvvmTestHelper.DefaultConfig, ModelState = new ModelState(), RequestType = DotvvmRequestType.Command }, + Child = new TestViewModel2 { Id = 11, Code = "Code" } }; var validator = CreateValidator(); var expander = CreateErrorPathExpander(); @@ -286,25 +249,10 @@ public void ViewModelValidator_CustomModelStateErrors_OldFormatThrows() [TestMethod] public void ViewModelValidator_CustomModelStateErrors_OutsideValidationTarget() { - var testViewModel = new TestViewModel() - { - Context = new TestDotvvmRequestContext - { - Configuration = DotvvmTestHelper.DefaultConfig, - ModelState = new ModelState(), - RequestType = DotvvmRequestType.Command - }, - Child = new TestViewModel2() - { - Id = 11, - Code = "Code", - }, - Children = new List() - { - new TestViewModel2() { Code = "5" }, - new TestViewModel2() { Code = "6" }, - new TestViewModel2() { Code = "7" }, - } + var testViewModel = new TestViewModel { + Context = new TestDotvvmRequestContext { Configuration = DotvvmTestHelper.DefaultConfig, ModelState = new ModelState(), RequestType = DotvvmRequestType.Command }, + Child = new TestViewModel2 { Id = 11, Code = "Code" }, + Children = new List { new() { Code = "5" }, new() { Code = "6" }, new() { Code = "7" } } }; var validator = CreateValidator(); var expander = CreateErrorPathExpander(); @@ -320,37 +268,16 @@ public void ViewModelValidator_CustomModelStateErrors_OutsideValidationTarget() modelState.ErrorsInternal.AddRange(errors); expander.Expand(modelState, testViewModel); - XAssert.Equal(new string [] { - "/", - "/Child", - "/Children/0/Code", - "/Children/0/Id", - "/Children/2" - }, modelState.Errors.Select(e => e.PropertyPath).OrderBy(p => p)); + XAssert.Equal(new[] { "/", "/Child", "/Children/0/Code", "/Children/0/Id", "/Children/2" }, modelState.Errors.Select(e => e.PropertyPath).OrderBy(p => p)); } [TestMethod] public void ViewModelValidator_CustomModelStateErrors_ArbitraryTargetObjectAndLambda() { - var testViewModel = new TestViewModel() - { - Context = new TestDotvvmRequestContext - { - Configuration = DotvvmTestHelper.DefaultConfig, - ModelState = new ModelState(), - RequestType = DotvvmRequestType.Command - }, - Child = new TestViewModel2() - { - Id = 11, - Code = "Code", - }, - Children = new List() - { - new TestViewModel2() { Code = "5" }, - new TestViewModel2() { Code = "6" }, - new TestViewModel2() { Code = "7" }, - } + var testViewModel = new TestViewModel { + Context = new TestDotvvmRequestContext { Configuration = DotvvmTestHelper.DefaultConfig, ModelState = new ModelState(), RequestType = DotvvmRequestType.Command }, + Child = new TestViewModel2 { Id = 11, Code = "Code" }, + Children = new List { new() { Code = "5" }, new() { Code = "6" }, new() { Code = "7" } } }; var context = testViewModel.Context; var validator = CreateValidator(); @@ -382,25 +309,10 @@ public void ViewModelValidator_CustomModelStateErrors_ArbitraryTargetObjectAndLa public void ViewModelValidator_ObjectWithAttachedErrorReferencedMultipleTimes() { var innerViewModel = new TestViewModel2() { Code = "123" }; - var testViewModel = new TestViewModel() - { - Context = new TestDotvvmRequestContext - { - Configuration = DotvvmTestHelper.DefaultConfig, - ModelState = new ModelState(), - RequestType = DotvvmRequestType.Command - }, - Child = new TestViewModel2() - { - Id = 11, - Code = "Code", - }, - Children = new List() - { - innerViewModel, - new TestViewModel2() { Code = "6" }, - innerViewModel, - } + var testViewModel = new TestViewModel { + Context = new TestDotvvmRequestContext { Configuration = DotvvmTestHelper.DefaultConfig, ModelState = new ModelState(), RequestType = DotvvmRequestType.Command }, + Child = new TestViewModel2 { Id = 11, Code = "Code" }, + Children = new List { innerViewModel, new() { Code = "6" }, innerViewModel } }; var validator = CreateValidator(); var expander = CreateErrorPathExpander(); @@ -422,14 +334,8 @@ public void ViewModelValidator_ObjectWithAttachedErrorReferencedMultipleTimes() [ExpectedException(typeof(ArgumentException))] public void ViewModelValidator_AttemptToPassOldPaths(string path) { - var testViewModel = new TestViewModel() - { - Context = new TestDotvvmRequestContext - { - Configuration = DotvvmTestHelper.DefaultConfig, - ModelState = new ModelState(), - RequestType = DotvvmRequestType.Command - }, + var testViewModel = new TestViewModel { + Context = new TestDotvvmRequestContext { Configuration = DotvvmTestHelper.DefaultConfig, ModelState = new ModelState(), RequestType = DotvvmRequestType.Command } }; var validator = CreateValidator(); var expander = CreateErrorPathExpander(); @@ -446,6 +352,22 @@ public void ViewModelValidator_AttemptToPassOldPaths(string path) } + [TestMethod] + public void ViewModelValidator_ServiceProvider() + { + var testViewModel = new TestViewModel8(); + var validator = CreateValidator(); + var expander = CreateErrorPathExpander(); + var modelState = new ModelState { ValidationTarget = testViewModel }; + + var errors = validator.ValidateViewModel(testViewModel).OrderBy(n => n.PropertyPath); + modelState.ErrorsInternal.AddRange(errors); + expander.Expand(modelState, testViewModel); + var results = modelState.Errors.OrderBy(n => n.PropertyPath).ToList(); + + Assert.AreEqual(0, results.Count); + } + public class TestViewModel : DotvvmViewModelBase { [Required] @@ -497,7 +419,7 @@ protected override ValidationResult IsValid(object value, ValidationContext vali var entity = (TestViewModel4Child)validationContext.ObjectInstance; if (entity.IsChecked && string.IsNullOrEmpty(entity.ConditionalRequired)) { - return new ValidationResult("Value is required when the field is checked!", new[] { validationContext.MemberName }); + return new ValidationResult("Value is required when the field is checked!", new[] { validationContext.MemberName }); } return base.IsValid(value, validationContext); @@ -542,6 +464,25 @@ public IEnumerable Validate(ValidationContext validationContex } } } - } + + public class TestViewModel8 + { + [ServiceProviderTest] + public int? Id { get; set; } + + public class ServiceProviderTestAttribute : ValidationAttribute + { + protected override ValidationResult IsValid(object value, ValidationContext validationContext) + { + if (validationContext.GetService() == null) + { + return new ValidationResult("Service provider is not available"); + } + + return ValidationResult.Success; + } + } + } + } }