Skip to content

Commit

Permalink
Merge pull request #8 from rasmuseeg/feature/custom-dictionary-keys
Browse files Browse the repository at this point in the history
Feature/custom dictionary keys
  • Loading branch information
rasmuseeg authored Oct 12, 2019
2 parents 759880e + 25d83ed commit 936ffd7
Show file tree
Hide file tree
Showing 89 changed files with 3,241 additions and 78 deletions.
8 changes: 7 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -258,4 +258,10 @@ paket-files/

# Python Tools for Visual Studio (PTVS)
__pycache__/
*.pyc
*.pyc

# UmbracoCms
umbraco/
umbraco_client/
App_Data/
App_Code
36 changes: 24 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,7 @@ public string Username { get; set; }

Example:
```c#
[UmbracoRequired]
[UmbracoEmailAddress]
[UmbracoDisplayName(nameof(Email))]
[DataType(DataType.EmailAddress)]
[UmbracoEmailAddress(DictionaryKey = "MyCustomKey")]
public string Email { get; set; }
```

Expand All @@ -165,7 +162,7 @@ public string Email { get; set; }

Example:
```C#
[UmbracoMinLength(20)]
[UmbracoMinLength(20, DictionaryKey = "MyCustomKey")]
property string MyProperty { get; set; }
```

Expand All @@ -177,7 +174,7 @@ property string MyProperty { get; set; }

Example:
```C#
[UmbracoMaxLength(120)]
[UmbracoMaxLength(120, DictionaryKey = "MyCustomKey")]
property string MyProperty { get; set; }
```

Expand All @@ -190,7 +187,7 @@ property string MyProperty { get; set; }

Example:
```C#
[UmbracoStringLength(120, MinimumLength = 30)]
[UmbracoStringLength(120, MinimumLength = 30, DictionaryKey = "MyCustomKey")]
property string Message { get; set; }
```

Expand All @@ -201,7 +198,7 @@ property string Message { get; set; }

Example:
```C#
[MustBeTrue]
[UmbracoMustBeTrue(DictionaryKey = "MyCustomKey")]
property boool Consent { get; set; }
```

Expand All @@ -215,7 +212,11 @@ property boool Consent { get; set; }

Example:
```C#
[UmbracoPassword]
[UmbracoPassword(DictionaryKey = "CustomPasswordKey",
MinPasswordLengthDictionaryKey = "CustomMinPasswordLengthKey",
MinNonAlphanumericCharactersDictionaryKey = "MyCustomMinNonAlphanumericCharactersKey",
PasswordStrengthDictionaryKey = "MyCustomPasswordStrengtKey",
PasswordStrengthRegexTimeout = 360)]
property string Password { get; set; }
```

Expand All @@ -225,14 +226,25 @@ There are no default keys for this attribute, since each regex validation is uni

Example:
```C#
[UmbracoRegularExpression()]
[UmbracoRegularExpression(DictionaryKey = "MyCustomRegexKey")]
property string Password { get; set; }
```

### UmbracoRequired

Example:
```C#
[UmbracoRequired]
[UmbracoRequired(DictionaryKey = "MyCustomRequiredKey")]
property string MyProperty { get; set; }
```

## Custom dictionary keys
Each Attribute, has a public property `DictionaryKey` which can be set like this:
```
[UmbracoReguired(DictionaryKey = "MyCustomKey")]
[UmbracoRegularExpression(DictionaryKey = "MyCustomRegexKey")]
[UmbracoRegularExpression(DictionaryKey = "MyCustomRegexKey")]
property string MyProperty { get; set; }
```
```

Not setting a custom key, will fallback to the default dictionary key.
10 changes: 8 additions & 2 deletions src/Our.Umbraco.DataAnnotations.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.28010.2048
# Visual Studio Version 16
VisualStudioVersion = 16.0.29403.142
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Our.Umbraco.DataAnnotations", "Our.Umbraco.DataAnnotations\Our.Umbraco.DataAnnotations.csproj", "{C808941C-0685-4F06-80CE-81AE4E8B82E3}"
EndProject
Expand All @@ -19,6 +19,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "build", "build", "{041121B7
Our.Umbraco.DataAnnotations\Our.Umbraco.DataAnnotations.nuspec = Our.Umbraco.DataAnnotations\Our.Umbraco.DataAnnotations.nuspec
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UmbracoCms", "UmbracoCms\UmbracoCms.csproj", "{03A5C03C-2BF9-4EF5-9473-867F2D31BFC6}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -29,6 +31,10 @@ Global
{C808941C-0685-4F06-80CE-81AE4E8B82E3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C808941C-0685-4F06-80CE-81AE4E8B82E3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C808941C-0685-4F06-80CE-81AE4E8B82E3}.Release|Any CPU.Build.0 = Release|Any CPU
{03A5C03C-2BF9-4EF5-9473-867F2D31BFC6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{03A5C03C-2BF9-4EF5-9473-867F2D31BFC6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{03A5C03C-2BF9-4EF5-9473-867F2D31BFC6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{03A5C03C-2BF9-4EF5-9473-867F2D31BFC6}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Our.Umbraco.DataAnnotations.Interfaces
{
public interface IUmbracoValidationAttribute
{
string DictionaryKey { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@
<ItemGroup>
<Compile Include="Conditionals\ConditionalValidationAttributes.cs" />
<Compile Include="Constants.cs" />
<Compile Include="Interfaces\IUmbracoValidationAttribute.cs" />
<Compile Include="Migrations\CreateDictionaryKeys.cs" />
<Compile Include="Migrations\Runner.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand Down
8 changes: 5 additions & 3 deletions src/Our.Umbraco.DataAnnotations/UmbracoCompareAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
using System.Collections.Generic;
using Our.Umbraco.DataAnnotations.Interfaces;
using System.Collections.Generic;
using System.Web.Mvc;

namespace Our.Umbraco.DataAnnotations
{
/// <summary>
/// Specified that two properties data field value must match.
/// </summary>
public class UmbracoCompareAttribute : System.ComponentModel.DataAnnotations.CompareAttribute, IClientValidatable
public sealed class UmbracoCompareAttribute : System.ComponentModel.DataAnnotations.CompareAttribute, IClientValidatable, IUmbracoValidationAttribute
{
public string DictionaryKey { get; set; } = "EqualToError";
public new string ErrorMessageString { get; set; }
public new string OtherPropertyDisplayName { get; set; }

public UmbracoCompareAttribute(string otherProperty)
: base(otherProperty)
{
ErrorMessageString = UmbracoDictionary.GetDictionaryValue("EqualToError");
ErrorMessageString = UmbracoDictionary.GetDictionaryValue(DictionaryKey);
}

public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
Expand Down
2 changes: 1 addition & 1 deletion src/Our.Umbraco.DataAnnotations/UmbracoDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Our.Umbraco.DataAnnotations
{
public class UmbracoDictionary
public sealed class UmbracoDictionary
{
private static UmbracoHelper _helper;

Expand Down
11 changes: 6 additions & 5 deletions src/Our.Umbraco.DataAnnotations/UmbracoDisplayNameAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
using System.ComponentModel;
using Our.Umbraco.DataAnnotations.Interfaces;
using System.ComponentModel;

namespace Our.Umbraco.DataAnnotations
{
public class UmbracoDisplayNameAttribute : DisplayNameAttribute
public sealed class UmbracoDisplayNameAttribute : DisplayNameAttribute, IUmbracoValidationAttribute
{
private readonly string dictionaryKey;
public string DictionaryKey { get; set; }

public UmbracoDisplayNameAttribute(string dictionaryKey)
: base()
{
this.dictionaryKey = dictionaryKey;
DictionaryKey = dictionaryKey;
}

public override string DisplayName
{
get
{
return UmbracoDictionary.GetDictionaryValue(dictionaryKey);
return UmbracoDictionary.GetDictionaryValue(DictionaryKey);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using Our.Umbraco.DataAnnotations.Interfaces;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;

Expand All @@ -7,7 +8,7 @@ namespace Our.Umbraco.DataAnnotations
/// <summary>
/// Specifies that a data field value must be a valid Email Address
/// </summary>
public class UmbracoEmailAddressAttribute : RegularExpressionAttribute, IClientValidatable
public sealed class UmbracoEmailAddressAttribute : RegularExpressionAttribute, IClientValidatable, IUmbracoValidationAttribute
{
public string DictionaryKey { get; set; } = "EmailError";

Expand Down
15 changes: 6 additions & 9 deletions src/Our.Umbraco.DataAnnotations/UmbracoMaxLengthAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using Our.Umbraco.DataAnnotations.Interfaces;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;

Expand All @@ -7,24 +8,20 @@ namespace Our.Umbraco.DataAnnotations
/// <summary>
/// Specifies the maximum length of array or string data allowed in a property.
/// </summary>
public class UmbracoMaxLengthAttribute : MaxLengthAttribute, IClientValidatable
public sealed class UmbracoMaxLengthAttribute : MaxLengthAttribute, IClientValidatable, IUmbracoValidationAttribute
{
public string DictionaryKey { get; set; } = "MaxLengthError";

public UmbracoMaxLengthAttribute(int length)
: base(length)
{
ErrorMessage = UmbracoDictionary.GetDictionaryValue(DictionaryKey);
}

public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
ErrorMessage = UmbracoDictionary.GetDictionaryValue("MaxLengthError");
yield return
new ModelClientValidationMaxLengthRule(FormatErrorMessage(metadata.GetDisplayName()), Length);
}

public UmbracoMaxLengthAttribute(int length, string dictionaryKey)
: base(length)
{
ErrorMessage = UmbracoDictionary.GetDictionaryValue(dictionaryKey);
}
}
}
15 changes: 6 additions & 9 deletions src/Our.Umbraco.DataAnnotations/UmbracoMinLengthAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using Our.Umbraco.DataAnnotations.Interfaces;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;

Expand All @@ -7,24 +8,20 @@ namespace Our.Umbraco.DataAnnotations
/// <summary>
/// Specifies the minimum length of array or string data allowed in a property.
/// </summary>
public class UmbracoMinLengthAttribute : MinLengthAttribute, IClientValidatable
public sealed class UmbracoMinLengthAttribute : MinLengthAttribute, IClientValidatable, IUmbracoValidationAttribute
{
public string DictionaryKey { get; set; } = "MinLengthError";

public UmbracoMinLengthAttribute(int length)
: base(length)
{
ErrorMessage = UmbracoDictionary.GetDictionaryValue(DictionaryKey);
}

public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
ErrorMessage = UmbracoDictionary.GetDictionaryValue("MinLengthError");
yield return
new ModelClientValidationMinLengthRule(FormatErrorMessage(metadata.GetDisplayName()), Length);
}

public UmbracoMinLengthAttribute(int length, string dictionaryKey)
: base(length)
{
ErrorMessage = UmbracoDictionary.GetDictionaryValue(dictionaryKey);
}
}
}
9 changes: 6 additions & 3 deletions src/Our.Umbraco.DataAnnotations/UmbracoMustBeTrueAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
using System;
using Our.Umbraco.DataAnnotations.Interfaces;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;

namespace Our.Umbraco.DataAnnotations
{
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = false)]
public class UmbracoMustBeTrueAttribute : ValidationAttribute, IClientValidatable
public sealed class UmbracoMustBeTrueAttribute : ValidationAttribute, IClientValidatable, IUmbracoValidationAttribute
{
public string DictionaryKey { get; set; } = "MustBeTrueError";

public UmbracoMustBeTrueAttribute()
{
ErrorMessage = UmbracoDictionary.GetDictionaryValue("MustBeTrueError");
ErrorMessage = UmbracoDictionary.GetDictionaryValue(DictionaryKey);
}

public override bool IsValid(object value)
Expand Down
17 changes: 11 additions & 6 deletions src/Our.Umbraco.DataAnnotations/UmbracoPasswordAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using Our.Umbraco.DataAnnotations.Interfaces;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
Expand All @@ -13,18 +14,22 @@ namespace Our.Umbraco.DataAnnotations
/// Validates the string data using default membership provider.
/// </summary>
/// https://referencesource.microsoft.com/#System.Web/Security/MembershipPasswordAttribute.cs,19c2a804c4a5eaf5,references
public class UmbracoPasswordAttribute : MembershipPasswordAttribute, IClientValidatable
public sealed class UmbracoPasswordAttribute : MembershipPasswordAttribute, IClientValidatable, IUmbracoValidationAttribute
{
public string DictionaryKey { get; set; } = "PasswordError";
public string MinPasswordLengthDictionaryKey { get; set; } = "MinPasswordLengthError";
public string MinNonAlphanumericCharactersDictionaryKey { get; set; } = "MinPasswordLengthError";
public string PasswordStrengthDictionaryKey { get; set; } = "MinPasswordLengthError";
public int? PasswordStrengthRegexTimeout { get; set; }
public string ValidationName = "password";

public UmbracoPasswordAttribute()
: base()
{
ErrorMessage = UmbracoDictionary.GetDictionaryValue("PasswordError");
MinPasswordLengthError = UmbracoDictionary.GetDictionaryValue("MinPasswordLengthError");
MinNonAlphanumericCharactersError = UmbracoDictionary.GetDictionaryValue("MinNonAlphanumericCharactersError");
PasswordStrengthError = UmbracoDictionary.GetDictionaryValue("PasswordStrengthError");
ErrorMessage = UmbracoDictionary.GetDictionaryValue(DictionaryKey);
MinPasswordLengthError = UmbracoDictionary.GetDictionaryValue(MinPasswordLengthDictionaryKey);
MinNonAlphanumericCharactersError = UmbracoDictionary.GetDictionaryValue(MinNonAlphanumericCharactersDictionaryKey);
PasswordStrengthError = UmbracoDictionary.GetDictionaryValue(PasswordStrengthDictionaryKey);
}

public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
Expand Down
9 changes: 5 additions & 4 deletions src/Our.Umbraco.DataAnnotations/UmbracoRangeAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
using System.Collections.Generic;
using Our.Umbraco.DataAnnotations.Interfaces;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;

namespace Our.Umbraco.DataAnnotations
{
public class UmbracoRangeAttribute : RangeAttribute, IClientValidatable
public sealed class UmbracoRangeAttribute : RangeAttribute, IClientValidatable, IUmbracoValidationAttribute
{
public string ResourceKey { get; set; } = "RangeError";
public string DictionaryKey { get; set; } = "RangeError";

public UmbracoRangeAttribute(int minimum, int maximum)
: base(minimum, maximum)
{
ErrorMessage = UmbracoDictionary.GetDictionaryValue(DictionaryKey);
}

public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
ErrorMessage = UmbracoDictionary.GetDictionaryValue(ResourceKey);
yield return
new ModelClientValidationRangeRule(FormatErrorMessage(metadata.GetDisplayName()), Minimum, Maximum);
}
Expand Down
Loading

0 comments on commit 936ffd7

Please sign in to comment.