Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support to validate list items. #39

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using FubuMVC.Core.Ajax;
using System.Collections.Generic;
using FubuMVC.Core.Ajax;
using FubuValidation;

namespace FubuMVC.Validation.IntegrationTesting.Ajax
Expand All @@ -9,11 +10,30 @@ public AjaxContinuation post_ajax(AjaxRequest request)
{
return AjaxContinuation.Successful();
}

public AjaxContinuation post_ajax_collection(AjaxCollectionRequest request)
{
return AjaxContinuation.Successful();
}

}

public class AjaxRequest
{
[Required]
public string Name { get; set; }
}

public class AjaxCollectionRequest
{
public List<CollectionItem> Collection { get; set; }
}

public class CollectionItem
{
[Required]
public string Name { get; set; }
}


}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System.Diagnostics;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using FubuCore;
using FubuCore.Reflection;
using FubuMVC.Core.Ajax;
using FubuTestingSupport;
Expand All @@ -11,11 +13,16 @@ namespace FubuMVC.Validation.IntegrationTesting.Ajax
public class IntegratedAjaxTester : ValidationHarness
{
private AjaxRequest theRequest;
private AjaxCollectionRequest theCollectionRequest;

[SetUp]
public void SetUp()
{
theRequest = new AjaxRequest();
theCollectionRequest = new AjaxCollectionRequest
{
Collection = new List<CollectionItem> {new CollectionItem(), new CollectionItem()}
};
}

protected override void configure(Core.FubuRegistry registry)
Expand All @@ -39,7 +46,27 @@ private JsonResponse theContinuation
Debug.WriteLine(response.ReadAsText());
throw;
}


}
}


private JsonResponse theCollectionContinuation
{
get
{
var response = endpoints.PostJson(theCollectionRequest);

try
{
return response.ReadAsJson<JsonResponse>();
}
catch
{
Debug.WriteLine(response.ReadAsText());
throw;
}

}
}

Expand All @@ -49,6 +76,12 @@ public void validation_passes()
theRequest.Name = "Josh";
theContinuation.success.ShouldBeTrue();
}
[Test]
public void collection_validation_passes()
{
theCollectionRequest.Collection.Each((x, i) => x.Name = "Item{0}".ToFormat(i + 1));
theCollectionContinuation.success.ShouldBeTrue();
}

[Test]
public void validation_error_for_name()
Expand All @@ -58,6 +91,16 @@ public void validation_error_for_name()

errors.ShouldHaveCount(1);
errors.Any(x => x.field == ReflectionHelper.GetAccessor<AjaxRequest>(r => r.Name).Name).ShouldBeTrue();
}
[Test]
public void validation_errors_for_collection_item_names()
{
theCollectionRequest.Collection.Each(x => x.Name = null);
var errors = theCollectionContinuation.errors;

errors.ShouldHaveCount(2);
errors.Any(x => x.field == ReflectionHelper.GetAccessor<AjaxCollectionRequest>(r => r.Collection[0].Name).Name).ShouldBeTrue();
errors.Any(x => x.field == ReflectionHelper.GetAccessor<AjaxCollectionRequest>(r => r.Collection[1].Name).Name).ShouldBeTrue();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@
<ItemGroup>
<Compile Include="Ajax\IntegratedAjaxEndpoint.cs" />
<Compile Include="Ajax\IntegratedAjaxTester.cs" />
<Compile Include="LoFi\IntegratedCollectionLoFiTester.cs" />
<Compile Include="LoFi\IntegratedLoFiCollectionEdpoint.cs" />
<Compile Include="LoFi\IntegratedLoFiEndpoint.cs" />
<Compile Include="LoFi\IntegratedLoFiTester.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using System.Collections.Generic;
using FubuCore;
using FubuMVC.Core.Endpoints;
using FubuTestingSupport;
using NUnit.Framework;

namespace FubuMVC.Validation.IntegrationTesting.LoFi
{
[TestFixture]
public class IntegratedCollectionLoFiTester : ValidationHarness
{
private LoFiCollectionInput theCollectionInput;

[SetUp]
public void SetUp()
{
theCollectionInput = new LoFiCollectionInput
{
Collection = new List<LoFiCollectionElement>
{
new LoFiCollectionElement(),
new LoFiCollectionElement(),
}
};
}

protected override void configure(Core.FubuRegistry registry)
{
registry.Actions.IncludeType<IntegratedLoFiCollectionEdpoint>();
registry.Import<FubuMvcValidation>();
registry.Policies.Add(x =>
{
x.Where.InputTypeIs<LoFiCollectionInput>();
x.Conneg.ApplyConneg();
});
}

private HttpResponse theCollectionResponse
{
get
{
// NOTE: PostAsForm does not serialize the Collection member correctly, it is just serializing it by calling to ToString
// posting the request as Xml does the trick
return endpoints.PostXml(theCollectionInput);
}
}

[Test]
public void output_from_collection_endpoint_if_validation_succeeds()
{
theCollectionInput.Collection.Each((x, i) => x.Name = "Item{0}".ToFormat(i + 1));
theCollectionResponse.ReadAsText().ShouldEqual("\"" + IntegratedLoFiCollectionEdpoint.SUCCESS + "\"");
}

[Test]
public void redirects_to_collection_get_if_validation_fails()
{
theCollectionInput.Collection.Each(x => x.Name = null);
theCollectionResponse.ReadAsText().ShouldEqual("\"" + IntegratedLoFiCollectionEdpoint.GET + "\"");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System.Collections.Generic;
using FubuValidation;

namespace FubuMVC.Validation.IntegrationTesting.LoFi
{
public class IntegratedLoFiCollectionEdpoint
{
public const string GET = "Collection input data";
public const string SUCCESS = "Success collection";

public string get_lofi_collection(LoFiCollectionInput input)
{
return GET;
}

public string post_lofi_collection(LoFiCollectionInput input)
{
return SUCCESS;
}
}

public class LoFiCollectionInput
{
public List<LoFiCollectionElement> Collection { get; set; }
}

public class LoFiCollectionElement
{
[Required]
public string Name { get; set; }
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@
<Compile Include="default_validation_services.cs" />
<Compile Include="FubuValidationServiceRegistryTester.cs" />
<Compile Include="HasInputTypeTester.cs" />
<Compile Include="ListFieldValidationSourceTester.cs" />
<Compile Include="ModelBindingErrorsTester.cs" />
<Compile Include="NotValidatedAttributeFilterTester.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ public void registers_the_default_field_rule_registry_as_singleton()
verifyDefaultType<IFieldRulesRegistry, FieldRulesRegistry>().IsSingleton.ShouldBeTrue();
}

[Test]
public void registers_the_list_field_validation_source()
{
theGraph.ServicesFor<IFieldValidationSource>().ShouldContain(x => x.Type == typeof(ListFieldValidationSource));
}

[Test]
public void adds_the_accessor_rules_field_source()
{
Expand Down
59 changes: 59 additions & 0 deletions src/FubuMVC.Validation.Tests/ListFieldValidationSourceTester.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using FubuCore.Reflection;
using FubuTestingSupport;
using FubuValidation.Fields;
using NUnit.Framework;

namespace FubuMVC.Validation.Tests
{
[TestFixture]
public class ListFieldValidationSourceTester : InteractionContext<ListFieldValidationSource>
{
[Test]
public void returns_an_empty_list_for_properties_that_are_not_of_ilist_generic_type()
{
ClassUnderTest.RulesFor(getProperty(x => x.GenericEnumerableProperty)).ShouldHaveCount(0);
ClassUnderTest.RulesFor(getProperty(x => x.RegularEnumerableProperty)).ShouldHaveCount(0);
ClassUnderTest.RulesFor(getProperty(x => x.RegularListProperty)).ShouldHaveCount(0);
ClassUnderTest.RulesFor(getProperty(x => x.CollectionProperty)).ShouldHaveCount(0);
ClassUnderTest.RulesFor(getProperty(x => x.StringProperty)).ShouldHaveCount(0);
ClassUnderTest.RulesFor(getProperty(x => x.ObjectProperty)).ShouldHaveCount(0);
ClassUnderTest.RulesFor(getProperty(x => x.RegularListProperty)).ShouldHaveCount(0);
}

[Test]
public void returns_a_ListValidationRule_for_properties_that_are_of_ilist_generic_type()
{
ClassUnderTest.RulesFor(getProperty(x => x.GenericListProperty)).ToArray()
.ShouldHaveCount(1).First()
.ShouldBeOfType<ListValidationRule>();

ClassUnderTest.RulesFor(getProperty(x => x.ArrayProperty)).ToArray()
.ShouldHaveCount(1).First()
.ShouldBeOfType<ListValidationRule>();
}

private PropertyInfo getProperty(Expression<Func<ListFieldValidationSourceTesterModel, object>> expression)
{
return ReflectionHelper.GetProperty(expression);
}
public class ListFieldValidationSourceTesterModel
{
public IList<Model> GenericListProperty { get; set; }
public Model[] ArrayProperty { get; set; }
public IEnumerable<Model> GenericEnumerableProperty { get; set; }
public IEnumerable RegularEnumerableProperty { get; set; }
public IList RegularListProperty { get; set; }
public ICollection<Model> CollectionProperty { get; set; }
public string StringProperty { get; set; }
public object ObjectProperty { get; set; }
public class Model { }

}
}
}
16 changes: 11 additions & 5 deletions src/FubuMVC.Validation/FubuMVC.Validation.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion></ProductVersion>
<ProductVersion>
</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{1D14C442-9F90-4B28-B4F9-79CD6E1B6E40}</ProjectGuid>
<ProjectTypeGuids>{349c5851-65df-11da-9384-00065b846f21};{fae04ec0-301f-11d3-bf4b-00c04f79efbc}</ProjectTypeGuids>
Expand All @@ -14,8 +15,10 @@
<AssemblyName>FubuMVC.Validation</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<UseIISExpress>false</UseIISExpress>
<FileUpgradeFlags></FileUpgradeFlags>
<UpgradeBackupLocation></UpgradeBackupLocation>
<FileUpgradeFlags>
</FileUpgradeFlags>
<UpgradeBackupLocation>
</UpgradeBackupLocation>
<OldToolsVersion>4.0</OldToolsVersion>
<IISExpressSSLPort />
<IISExpressAnonymousAuthentication />
Expand Down Expand Up @@ -88,6 +91,7 @@
<Compile Include="Diagnostics\ValidationDiagnosticsSource.cs" />
<Compile Include="FubuMvcValidationServices.cs" />
<Compile Include="FubuValidationServiceRegistry.cs" />
<Compile Include="ListFieldValidationSource.cs" />
<Compile Include="NotValidatedAttribute.cs" />
<Compile Include="NotValidatedAttributeFilter.cs" />
<Compile Include="RegisterRemoteRuleQuery.cs" />
Expand Down Expand Up @@ -193,10 +197,12 @@
<AutoAssignPort>True</AutoAssignPort>
<DevelopmentServerPort>18912</DevelopmentServerPort>
<DevelopmentServerVPath>/</DevelopmentServerVPath>
<IISUrl></IISUrl>
<IISUrl>
</IISUrl>
<NTLMAuthentication>False</NTLMAuthentication>
<UseCustomServer>False</UseCustomServer>
<CustomServerUrl></CustomServerUrl>
<CustomServerUrl>
</CustomServerUrl>
<SaveServerSettingsInUserFile>False</SaveServerSettingsInUserFile>
</WebProjectProperties>
</FlavorProperties>
Expand Down
1 change: 1 addition & 0 deletions src/FubuMVC.Validation/FubuValidationServiceRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public FubuValidationServiceRegistry()
SetServiceIfNone<IValidator, Validator>();

AddService<IFieldValidationSource, AccessorRulesFieldSource>();
AddService<IFieldValidationSource, ListFieldValidationSource>();

setSingleton<ValidationGraph, ValidationGraph>();
setSingleton<IFieldRulesRegistry, FieldRulesRegistry>();
Expand Down
23 changes: 23 additions & 0 deletions src/FubuMVC.Validation/ListFieldValidationSource.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System.Collections.Generic;
using System.Reflection;
using FubuCore;
using FubuValidation.Fields;

namespace FubuMVC.Validation
{
public class ListFieldValidationSource : IFieldValidationSource
{
public IEnumerable<IFieldValidationRule> RulesFor(PropertyInfo property)
{
if (property.PropertyType.Closes(typeof(IList<>)))
{
yield return new ListValidationRule();
}
}

public void AssertIsValid()
{

}
}
}
Loading