Skip to content

Commit

Permalink
Add ErrorMessageResource handling in ValidationHelper.
Browse files Browse the repository at this point in the history
  • Loading branch information
richinator38 committed Jun 14, 2015
1 parent bb64771 commit b750763
Show file tree
Hide file tree
Showing 23 changed files with 470 additions and 103 deletions.
12 changes: 12 additions & 0 deletions Intertech.Validation.Test/Intertech.Validation.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@
<ItemGroup>
<Compile Include="TestDTO\Constants.cs" />
<Compile Include="TestDTO\NoValidations.cs" />
<Compile Include="TestDTO\TestResource.Designer.cs">
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>
<DependentUpon>TestResource.resx</DependentUpon>
</Compile>
<Compile Include="ValidationTestTests.cs" />
<Compile Include="TestDTO\ValidationTest.cs" />
<Compile Include="ValidationHelperTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand All @@ -72,6 +78,12 @@
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="TestDTO\TestResource.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>TestResource.Designer.cs</LastGenOutput>
</EmbeddedResource>
</ItemGroup>
<Choose>
<When Condition="'$(VisualStudioVersion)' == '10.0' And '$(IsCodedUITest)' == 'True'">
<ItemGroup>
Expand Down
72 changes: 72 additions & 0 deletions Intertech.Validation.Test/TestDTO/TestResource.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

123 changes: 123 additions & 0 deletions Intertech.Validation.Test/TestDTO/TestResource.resx
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<data name="NameRequiredResource" xml:space="preserve">
<value>Name is required (resource).</value>
</data>
</root>
2 changes: 1 addition & 1 deletion Intertech.Validation.Test/TestDTO/ValidationTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Intertech.Validation.Test.TestDTO
{
public class ValidationTest
{
[Required]
[Required(ErrorMessageResourceName = "NameRequiredResource", ErrorMessageResourceType = typeof(TestResource))]
[MinLength(3)]
public string Name { get; set; }

Expand Down
15 changes: 12 additions & 3 deletions Intertech.Validation.Test/ValidationHelperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Intertech.Validation.Constants;
using Intertech.Validation.Test.TestDTO;
using System.IO;
using System.Collections.Generic;

namespace Intertech.Validation.Test
{
Expand All @@ -13,12 +14,20 @@ public class ValidationHelperTests
{
private object _validations;
private object _emptyValidations;
private GetValidationsParms _parms;

[TestInitialize]
public void Init()
{
_parms = new GetValidationsParms("TestDTO.ValidationTest", "model")
{
DtoAssemblyNames = new List<string> { "Intertech.Validation.Test" },
ResourceAssemblyName = "Intertech.Validation.Test",
ResourceNamespace = "Intertech.Validation.Test.TestDTO"
};

var vals = new StringBuilder("{ validations: { model: { ");
vals.Append("Name: { \"ng-minlength\": 3, \"ng-minlength-msg\": \"" + string.Format(DataAnnotationConstants.DefaultMinLengthErrorMsg, "Name", "3") + "\", \"required\": true, \"required-msg\": \"" + string.Format(DataAnnotationConstants.DefaultRequiredErrorMsg, "Name") + "\" }");
vals.Append("Name: { \"ng-minlength\": 3, \"ng-minlength-msg\": \"" + string.Format(DataAnnotationConstants.DefaultMinLengthErrorMsg, "Name", "3") + "\", \"required\": true, \"required-msg\": \"" + TestResource.NameRequiredResource + "\" }");
vals.Append(", CreditCard: { \"ng-pattern\": \"/" + RegexConstants.GetRegularExpressionForJson(RegexConstants.CreditCard) + "/\", \"ng-pattern-msg\": \"" + string.Format(DataAnnotationConstants.DefaultCreditCardErrorMsg, "CreditCard") + "\" }");
vals.Append(", Email: { \"ng-pattern\": \"/" + RegexConstants.GetRegularExpressionForJson(RegexConstants.Email) + "/\", \"ng-pattern-msg\": \"" + string.Format(DataAnnotationConstants.DefaultEmailErrorMsg, "Email") + "\" }");
vals.Append(", Email2: { \"ng-pattern\": \"/" + RegexConstants.GetRegularExpressionForJson(RegexConstants.Email) + "/\", \"ng-pattern-msg\": \"" + ErrorMessages.Email + "\" }");
Expand Down Expand Up @@ -60,13 +69,13 @@ public void ValidationHelper_Constructor_Test()
}

[TestMethod]
public void ValidationHelper_GetValidations_Success_Test()
public void ValidationHelper_GetValidations_Success_ParmsObject_Test()
{
// Assemble
var valHelper = new ValidationHelper();

// Act
var vals = valHelper.GetValidations("TestDTO.ValidationTest", "model", null, "Intertech.Validation.Test");
var vals = valHelper.GetValidations(_parms);

// Assert
Assert.IsNotNull(vals);
Expand Down
40 changes: 40 additions & 0 deletions Intertech.Validation.Test/ValidationTestTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.ComponentModel.DataAnnotations;
using System.Collections.Generic;

namespace Intertech.Validation.Test.TestDTO
{
[TestClass]
public class ValidationTestTests
{
private ValidationTest _dto;

[TestInitialize]
public void Init()
{
_dto = new ValidationTest
{
Name = "Rich",
Length = "12345",
FavoriteNumber = 7
};
}

[TestMethod]
public void ValidationTest_Name_RequiredFail_Test()
{
// Assemble
_dto.Name = null;
var results = new List<ValidationResult>();

// Act
var actual = Validator.TryValidateObject(_dto, new ValidationContext(_dto), results, true);

// Assert
Assert.IsFalse(actual);
Assert.AreEqual(1, results.Count);
Assert.AreEqual<string>(TestResource.NameRequiredResource, results[0].ErrorMessage);
}
}
}
2 changes: 2 additions & 0 deletions Intertech.Validation/Constants/DataAnnotationConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ public class DataAnnotationConstants
public const string Display = "Display";
public const string MinimumLength = "MinimumLength";
public const string ErrorMessage = "ErrorMessage";
public const string ErrorMessageResourceName = "ErrorMessageResourceName";
public const string ErrorMessageResourceType = "ErrorMessageResourceType";
public const string DefaultEmailErrorMsg = "{0} is an invalid email address.";
public const string DefaultPhoneErrorMsg = "{0} is an invalid phone number.";
public const string DefaultCreditCardErrorMsg = "{0} is an invalid credit card number.";
Expand Down
Loading

0 comments on commit b750763

Please sign in to comment.