Skip to content

Commit

Permalink
Camel case property names
Browse files Browse the repository at this point in the history
Support added to optionally return camel case property names.
  • Loading branch information
bhuang61 committed Mar 19, 2015
1 parent bb64771 commit b993ee0
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 15 deletions.
10 changes: 6 additions & 4 deletions AATestAPI/AATestAPI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,6 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Intertech.Validation.AA">
<HintPath>..\packages\Intertech.Validation.AA.1.0.0\lib\Intertech.Validation.AA.dll</HintPath>
</Reference>
<Reference Include="Microsoft.CSharp" />
<Reference Include="Microsoft.Web.Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<Private>True</Private>
Expand Down Expand Up @@ -117,7 +114,12 @@
<Reference Include="System.Net.Http.WebRequest">
</Reference>
</ItemGroup>
<ItemGroup />
<ItemGroup>
<ProjectReference Include="..\Intertech.Validation\Intertech.Validation.csproj">
<Project>{991aa9c3-7192-400b-abc2-66d3d98ec859}</Project>
<Name>Intertech.Validation</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Compile Include="App_Start\FilterConfig.cs" />
<Compile Include="App_Start\RouteConfig.cs" />
Expand Down
2 changes: 1 addition & 1 deletion AATestAPI/Controllers/ValidationController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public IHttpActionResult GetValidations(string dtoObjectName, string jsonObjectN
{
var valHelper = new ValidationHelper();
object jsonObject = valHelper.GetValidations(dtoObjectName, jsonObjectName,
"AATestAPI.Models", "AATestAPI");
"AATestAPI.Models", false, "AATestAPI");

return Ok(jsonObject);
}
Expand Down
50 changes: 44 additions & 6 deletions Intertech.Validation.Test/ValidationHelperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ namespace Intertech.Validation.Test
[TestClass]
public class ValidationHelperTests
{
private object _validations;
private object _validations;
private object _validationsCC;
private object _emptyValidations;

[TestInitialize]
Expand All @@ -37,6 +38,29 @@ public void Init()
vals.Append("} }");
_validations = JObject.Parse(vals.ToString());


var valsCC = new StringBuilder("{ validations: { model: { ");
valsCC.Append("name: { \"ng-minlength\": 3, \"ng-minlength-msg\": \"" + string.Format(DataAnnotationConstants.DefaultMinLengthErrorMsg, "Name", "3") + "\", \"required\": true, \"required-msg\": \"" + string.Format(DataAnnotationConstants.DefaultRequiredErrorMsg, "Name") + "\" }");
valsCC.Append(", creditCard: { \"ng-pattern\": \"/" + RegexConstants.GetRegularExpressionForJson(RegexConstants.CreditCard) + "/\", \"ng-pattern-msg\": \"" + string.Format(DataAnnotationConstants.DefaultCreditCardErrorMsg, "CreditCard") + "\" }");
valsCC.Append(", email: { \"ng-pattern\": \"/" + RegexConstants.GetRegularExpressionForJson(RegexConstants.Email) + "/\", \"ng-pattern-msg\": \"" + string.Format(DataAnnotationConstants.DefaultEmailErrorMsg, "Email") + "\" }");
valsCC.Append(", email2: { \"ng-pattern\": \"/" + RegexConstants.GetRegularExpressionForJson(RegexConstants.Email) + "/\", \"ng-pattern-msg\": \"" + ErrorMessages.Email + "\" }");
valsCC.Append(", street: { \"ng-maxlength\": 40, \"ng-maxlength-msg\": \"" + string.Format(DataAnnotationConstants.DefaultMaxLengthErrorMsg, "Street", "40") + "\" }");
valsCC.Append(", phone: { \"ng-pattern\": \"/" + RegexConstants.GetRegularExpressionForJson(RegexConstants.Phone) + "/\", \"ng-pattern-msg\": \"" + string.Format(DataAnnotationConstants.DefaultPhoneErrorMsg, "Phone") + "\" }");
valsCC.Append(", phone2: { \"ng-pattern\": \"/" + RegexConstants.GetRegularExpressionForJson(RegexConstants.Phone) + "/\", \"ng-pattern-msg\": \"" + ErrorMessages.Phone + "\" }");
valsCC.Append(", favoriteNumber: { \"min\": 1, \"min-msg\": \"" + string.Format(DataAnnotationConstants.DefaultRangeErrorMsg, "FavoriteNumber", "1", "100") + "\", \"max\": 100, \"max-msg\": \"" + string.Format(DataAnnotationConstants.DefaultRangeErrorMsg, "FavoriteNumber", "1", "100") + "\" }");
valsCC.Append(", integerString: { \"ng-pattern\": \"/" + RegexConstants.GetRegularExpressionForJson(RegexConstants.Integer) + "/\", \"ng-pattern-msg\": \"" + string.Format(DataAnnotationConstants.DefaultRegexErrorMsg, "IntegerString") + "\" }");
valsCC.Append(", decimalString: { \"ng-pattern\": \"/" + RegexConstants.GetRegularExpressionForJson(RegexConstants.Decimal) + "/\", \"ng-pattern-msg\": \"" + ErrorMessages.Regex + "\" }");
valsCC.Append(", nickName: { \"ng-maxlength\": 30, \"ng-maxlength-msg\": \"" + string.Format(DataAnnotationConstants.DefaultMaxLengthErrorMsg, "NickName", "30") + "\", \"ng-minlength\": 2, \"ng-minlength-msg\": \"" + string.Format(DataAnnotationConstants.DefaultMinLengthErrorMsg, "NickName", "2") + "\" }");
valsCC.Append(", website: { \"ng-pattern\": \"/" + RegexConstants.GetRegularExpressionForJson(RegexConstants.Url) + "/\", \"ng-pattern-msg\": \"" + string.Format(DataAnnotationConstants.DefaultUrlErrorMsg, "Website") + "\" }");
valsCC.Append(", length: { \"ng-minlength\": 5, \"ng-minlength-msg\": \"" + ErrorMessages.MinLength + "\", \"ng-maxlength\": 25, \"ng-maxlength-msg\": \"" + ErrorMessages.MaxLength + "\", \"required\": true, \"required-msg\": \"" + ErrorMessages.Required + "\" }");
valsCC.Append(", visa: { \"ng-pattern\": \"/" + RegexConstants.GetRegularExpressionForJson(RegexConstants.CreditCard) + "/\", \"ng-pattern-msg\": \"" + ErrorMessages.CreditCard + "\", \"ng-maxlength\": 30, \"ng-maxlength-msg\": \"" + ErrorMessages.VisaLength + "\", \"ng-minlength\": 12, \"ng-minlength-msg\": \"" + ErrorMessages.VisaLength + "\" }");
valsCC.Append(", url: { \"ng-pattern\": \"/" + RegexConstants.GetRegularExpressionForJson(RegexConstants.Url) + "/\", \"ng-pattern-msg\": \"" + ErrorMessages.Url + "\" }");
valsCC.Append(" }");
valsCC.Append("} }");
_validationsCC = JObject.Parse(valsCC.ToString());



_emptyValidations = JObject.Parse("{ validations: { model: { } } }");
}

Expand Down Expand Up @@ -66,21 +90,35 @@ public void ValidationHelper_GetValidations_Success_Test()
var valHelper = new ValidationHelper();

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

// Assert
Assert.IsNotNull(vals);
AssertJsonEqual(_validations, vals);
}

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

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

// Assert
Assert.IsNotNull(vals);
AssertJsonEqual(_validationsCC, vals);
}

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

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

// Assert
Assert.IsNotNull(vals);
Expand All @@ -95,7 +133,7 @@ public void ValidationHelper_GetValidations_DTONotFound_Test()
var valHelper = new ValidationHelper();

// Act
var vals = valHelper.GetValidations("blah", "model", null, "Intertech.Validation.Test");
var vals = valHelper.GetValidations("blah", "model", null, false, "Intertech.Validation.Test");

// Assert
Assert.IsNull(vals);
Expand All @@ -109,7 +147,7 @@ public void ValidationHelper_GetValidations_AssemblyNotFound_Test()
var valHelper = new ValidationHelper();

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

// Assert
Assert.IsNull(vals);
Expand Down
45 changes: 41 additions & 4 deletions Intertech.Validation/ValidationHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Intertech.Validation.Converters;
using System.Globalization;

namespace Intertech.Validation
{
Expand Down Expand Up @@ -50,11 +51,11 @@ public ValidationHelper()
/// <param name="jsonObjectName"></param>
/// <param name="assemblyNames">Names of assemblies to check</param>
/// <returns></returns>
public object GetValidations(string dtoObjectName, string jsonObjectName, string alternateNamespace, params string[] assemblyNames)
public object GetValidations(string dtoObjectName, string jsonObjectName, string alternateNamespace, bool useCamelCaseForProperties, params string[] assemblyNames)
{
var jsonString = new StringBuilder("{ validations: {");

GetValidationsForDto(dtoObjectName, jsonObjectName, jsonString, false, alternateNamespace, assemblyNames);
GetValidationsForDto(dtoObjectName, jsonObjectName, jsonString, false, alternateNamespace, useCamelCaseForProperties, assemblyNames);

jsonString.Append("} }");

Expand All @@ -80,7 +81,7 @@ where type.GetInterfaces().Any(t => t == typeof(IValidationConverter))
return registrations.AsEnumerable<Type>();
}

private void GetValidationsForDto(string dtoObjectName, string jsonObjectName, StringBuilder jsonString, bool isContainedDto, string alternateNamespace, params string[] assemblyNames)
private void GetValidationsForDto(string dtoObjectName, string jsonObjectName, StringBuilder jsonString, bool isContainedDto, string alternateNamespace, bool useCamelCaseForProperties, params string[] assemblyNames)
{
if (isContainedDto)
{
Expand Down Expand Up @@ -121,7 +122,14 @@ private void GetValidationsForDto(string dtoObjectName, string jsonObjectName, S
if (!isFirstAttr)
{
var sep = isFirstProp ? string.Empty : ",";
jsonString.Append(sep + prop.Name + ": { ");
if (useCamelCaseForProperties)
{
jsonString.Append(sep + CamelCaseProperty(prop.Name) + ": { ");
}
else
{
jsonString.Append(sep + prop.Name + ": { ");
}
jsonString.Append(attrStr.ToString());
jsonString.Append("}");

Expand Down Expand Up @@ -188,6 +196,35 @@ private Type GetTypeFromAssembly(Assembly assembly, string dtoObjectName, string
return type;
}

private string CamelCaseProperty(string input)
{
if (string.IsNullOrEmpty(input) || !char.IsUpper(input[0]))
{
return input;
}

var sb = new StringBuilder();

for (var i = 0; i < input.Length; ++i)
{
var flag = i + 1 < input.Length;
if (i == 0 || !flag || char.IsUpper(input[i + 1]))
{
var ch = char.ToLower(input[i], CultureInfo.InvariantCulture);
sb.Append(ch);
}
else
{
sb.Append(input.Substring(i));
break;
}
}

return sb.ToString();
}



#endregion Private Methods
}
}

0 comments on commit b993ee0

Please sign in to comment.