forked from srkirkland/DataAnnotationsExtensions
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/rodrigojuarez/DataAnnotat…
…ionsExtensions into cuit-rodrigojuarez
- Loading branch information
Showing
14 changed files
with
266 additions
and
19 deletions.
There are no files selected for viewing
19 changes: 19 additions & 0 deletions
19
DataAnnotationsExtensions.ClientValidation/Adapters/CuitAttributeAdapter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using System.Collections.Generic; | ||
using System.Web.Mvc; | ||
using DataAnnotationsExtensions.ClientValidation.Rules; | ||
|
||
namespace DataAnnotationsExtensions.ClientValidation.Adapters | ||
{ | ||
public class CuitAttributeAdapter : DataAnnotationsModelValidator<CuitAttribute> | ||
{ | ||
public CuitAttributeAdapter(ModelMetadata metadata, ControllerContext context, CuitAttribute attribute) | ||
: base(metadata, context, attribute) | ||
{ | ||
} | ||
|
||
public override IEnumerable<ModelClientValidationRule> GetClientValidationRules() | ||
{ | ||
return new[] { new ModelClientValidationCuitRule(ErrorMessage) }; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
DataAnnotationsExtensions.ClientValidation/Rules/ModelClientValidationCuitRule.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using System.Web.Mvc; | ||
|
||
namespace DataAnnotationsExtensions.ClientValidation.Rules | ||
{ | ||
public class ModelClientValidationCuitRule : ModelClientValidationRule | ||
{ | ||
public ModelClientValidationCuitRule(string errorMessage) | ||
{ | ||
ErrorMessage = errorMessage; | ||
ValidationType = "cuit"; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using System.ComponentModel.DataAnnotations; | ||
|
||
namespace DataAnnotationsExtensions.Core | ||
{ | ||
public class CuitEntity | ||
{ | ||
[Cuit] | ||
[Required] | ||
public string Cuit { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
DataAnnotationsExtensions.Tests/ValidationAttributes/CuitAttributeTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
using System.ComponentModel.DataAnnotations; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
||
namespace DataAnnotationsExtensions.Tests.ValidationAttributes | ||
{ | ||
[TestClass] | ||
public class CuitAttributeTests | ||
{ | ||
[TestMethod] | ||
public void IsValidTests() | ||
{ | ||
var attribute = new CuitAttribute(); | ||
|
||
Assert.IsTrue(attribute.IsValid(null)); | ||
Assert.IsTrue(attribute.IsValid("20245597151")); | ||
Assert.IsTrue(attribute.IsValid("20-24559715-1")); | ||
Assert.IsTrue(attribute.IsValid("27-23840320-6")); | ||
Assert.IsTrue(attribute.IsValid("27238403206")); | ||
|
||
Assert.IsFalse(attribute.IsValid("20 24559715 1")); | ||
Assert.IsFalse(attribute.IsValid("")); | ||
Assert.IsFalse(attribute.IsValid("99-99999999-9")); | ||
Assert.IsFalse(attribute.IsValid("99999999999")); | ||
Assert.IsFalse(attribute.IsValid("4408 0412 3456 7890")); | ||
Assert.IsFalse(attribute.IsValid(0)); | ||
Assert.IsFalse(attribute.IsValid(20245597151)); | ||
} | ||
|
||
[TestMethod] | ||
public void ErrorResourcesTest() | ||
{ | ||
var attribute = new CuitAttribute(); | ||
attribute.ErrorMessageResourceName = "ErrorMessage"; | ||
attribute.ErrorMessageResourceType = typeof (ErrorResources); | ||
|
||
const int invalidValue = 0; | ||
|
||
var result = attribute.GetValidationResult(invalidValue, new ValidationContext(0, null, null)); | ||
|
||
Assert.AreEqual(ErrorResources.ErrorMessage, result.ErrorMessage); | ||
} | ||
|
||
[TestMethod] | ||
public void ErrorMessageTest() | ||
{ | ||
var attribute = new CuitAttribute(); | ||
attribute.ErrorMessage = "SampleErrorMessage"; | ||
|
||
const int invalidValue = 0; | ||
|
||
var result = attribute.GetValidationResult(invalidValue, new ValidationContext(0, null, null)); | ||
|
||
Assert.AreEqual("SampleErrorMessage", result.ErrorMessage); | ||
} | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
DataAnnotationsExtensions.Web/Controllers/CuitController.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using DataAnnotationsExtensions.Core; | ||
|
||
namespace DataAnnotationsExtensions.Web.Controllers | ||
{ | ||
public class CuitController : ValidationControllerBase<CuitEntity> | ||
{ | ||
protected override void AddMessage() | ||
{ | ||
Message = | ||
"CUIT validation: Examples of valid values {20245597151, 20-24559715-1}."; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
@model DataAnnotationsExtensions.Core.CuitEntity | ||
|
||
@{ | ||
ViewBag.Title = "Create"; | ||
} | ||
|
||
<h2>Create</h2> | ||
|
||
@using (Html.BeginForm()) { | ||
@Html.ValidationSummary(true) | ||
<fieldset> | ||
<legend>Fields</legend> | ||
|
||
@Html.EditorForModel() | ||
|
||
<p> | ||
<input type="submit" value="Create" /> | ||
</p> | ||
</fieldset> | ||
} | ||
|
||
<div> | ||
@Html.ActionLink("Back to demos", "Demos", "Home") | ||
</div> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.ComponentModel.DataAnnotations; | ||
using DataAnnotationsExtensions.Resources; | ||
|
||
namespace DataAnnotationsExtensions | ||
{ | ||
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)] | ||
public class CuitAttribute : DataTypeAttribute | ||
{ | ||
public CuitAttribute() | ||
: base("cuit") | ||
{ | ||
} | ||
|
||
public override string FormatErrorMessage(string name) | ||
{ | ||
if (ErrorMessage == null && ErrorMessageResourceName == null) | ||
{ | ||
ErrorMessage = ValidatorResources.CuitAttribute_Invalid; | ||
} | ||
|
||
return base.FormatErrorMessage(name); | ||
} | ||
|
||
public override bool IsValid(object value) | ||
{ | ||
if (value == null) | ||
{ | ||
return true; | ||
} | ||
|
||
try | ||
{ | ||
string cuit = (string)value; | ||
List<int> prefijosValidos = new List<int>(new[] { 20, 22, 23, 27, 30 }); | ||
|
||
int preFijo = 0; | ||
int dni = 0; | ||
int control = 0; | ||
if (cuit.Length == 11 || cuit.Length == 13) | ||
{ | ||
if (cuit.Contains("-")) | ||
{ | ||
string[] aCuit = cuit.Split('-'); | ||
preFijo = int.Parse(aCuit[0]); | ||
dni = int.Parse(aCuit[1]); | ||
control = int.Parse(aCuit[2]); | ||
} | ||
else | ||
{ | ||
preFijo = int.Parse(cuit.Substring(0, 2)); | ||
dni = int.Parse(cuit.Substring(2, 8)); | ||
control = int.Parse(cuit.Substring(10, 1)); | ||
} | ||
} | ||
|
||
string prefijoDni = preFijo + dni.ToString().PadLeft(8, '0'); | ||
int suma = 0; | ||
int n; | ||
|
||
if (!prefijosValidos.Contains(preFijo)) | ||
return false; | ||
|
||
//Algoritmo de calculo del digito verificador | ||
const string coef = "5432765432"; | ||
for (n = 0; n < 10; n++) | ||
suma += int.Parse(prefijoDni.Substring(n, 1)) * int.Parse(coef.Substring(n, 1)); | ||
|
||
int resto = suma % 11; | ||
int digitoVerificador; | ||
|
||
if (resto == 0) | ||
digitoVerificador = 0; | ||
|
||
else if (resto == 10) | ||
digitoVerificador = 1; | ||
|
||
else | ||
digitoVerificador = 11 - resto; | ||
|
||
return (digitoVerificador == control); | ||
} | ||
catch (Exception) | ||
{ | ||
return false; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 10 additions & 1 deletion
11
DataAnnotationsExtensions/Resources/ValidatorResources.Designer.cs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters