Skip to content
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
151 changes: 76 additions & 75 deletions DataAnnotationsExtensions.Core/DataAnnotationsExtensions.Core.csproj
Original file line number Diff line number Diff line change
@@ -1,76 +1,77 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>8.0.30703</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{144683DA-A20C-4A69-9310-1E21FE38B67B}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>DataAnnotationsExtensions.Core</RootNamespace>
<AssemblyName>DataAnnotationsExtensions.Core</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.ComponentModel.DataAnnotations" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="..\Build\CommonAssemblyInfo.cs">
<Link>Properties\CommonAssemblyInfo.cs</Link>
</Compile>
<Compile Include="YearEntity.cs" />
<Compile Include="CreditCardEntity.cs" />
<Compile Include="CuitEntity.cs" />
<Compile Include="DateEntity.cs" />
<Compile Include="DigitsEntity.cs" />
<Compile Include="EmailEntity.cs" />
<Compile Include="EqualToEntity.cs" />
<Compile Include="FileExtensionsEntity.cs" />
<Compile Include="IntegerEntity.cs" />
<Compile Include="MaxEntity.cs" />
<Compile Include="MinEntity.cs" />
<Compile Include="NumericEntity.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="UrlEntity.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\DataAnnotationsExtensions\DataAnnotationsExtensions.csproj">
<Project>{FE938484-722F-40F6-AC72-3B8C8533CA54}</Project>
<Name>DataAnnotationsExtensions</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>8.0.30703</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{144683DA-A20C-4A69-9310-1E21FE38B67B}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>DataAnnotationsExtensions.Core</RootNamespace>
<AssemblyName>DataAnnotationsExtensions.Core</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.ComponentModel.DataAnnotations" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="..\Build\CommonAssemblyInfo.cs">
<Link>Properties\CommonAssemblyInfo.cs</Link>
</Compile>
<Compile Include="NotNullOrZeroEntity.cs" />
<Compile Include="YearEntity.cs" />
<Compile Include="CreditCardEntity.cs" />
<Compile Include="CuitEntity.cs" />
<Compile Include="DateEntity.cs" />
<Compile Include="DigitsEntity.cs" />
<Compile Include="EmailEntity.cs" />
<Compile Include="EqualToEntity.cs" />
<Compile Include="FileExtensionsEntity.cs" />
<Compile Include="IntegerEntity.cs" />
<Compile Include="MaxEntity.cs" />
<Compile Include="MinEntity.cs" />
<Compile Include="NumericEntity.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="UrlEntity.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\DataAnnotationsExtensions\DataAnnotationsExtensions.csproj">
<Project>{FE938484-722F-40F6-AC72-3B8C8533CA54}</Project>
<Name>DataAnnotationsExtensions</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
11 changes: 11 additions & 0 deletions DataAnnotationsExtensions.Core/NotNullOrZeroEntity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace DataAnnotationsExtensions.Core
{
public class NotNullOrZeroEntity
{
[NotNullOrZero]
public int? NotNullOrZeroInt { get; set; }

[NotNullOrZero]
public double? NotNullOrZeroDouble { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
<Compile Include="Controllers\HomeControllerTest.cs" />
<Compile Include="Doubles\HttpPostedFileBaseStub.cs" />
<Compile Include="TestBase.cs" />
<Compile Include="ValidationAttributes\NotNullOrZeroAttributeTests.cs" />
<Compile Include="ValidationAttributes\RegularExpressionAttributeTests.cs" />
<Compile Include="ValidationAttributes\YearAttributeTests.cs" />
<Compile Include="ValidationAttributes\CreditCardAttributeTests.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace DataAnnotationsExtensions.Tests.ValidationAttributes
{
[TestClass]
public class NotNullOrZeroAttributeTests : TestBase
{
[TestMethod]
public void IsValidIntegerTests()
{
var attribute = new NotNullOrZeroAttribute();

Assert.IsFalse(attribute.IsValid(null)); // Null values are invalid
Assert.IsTrue(attribute.IsValid("42"));
Assert.IsTrue(attribute.IsValid(42.5));
Assert.IsTrue(attribute.IsValid("50"));
Assert.IsTrue(attribute.IsValid(100));
Assert.IsTrue(attribute.IsValid("10000000000"));
Assert.IsFalse(attribute.IsValid(0));
Assert.IsFalse(attribute.IsValid("0"));
Assert.IsTrue(attribute.IsValid(-50));
Assert.IsFalse(attribute.IsValid("fifty"));
}

[TestMethod]
public void IsValidDoubleTests()
{
var attribute = new NotNullOrZeroAttribute();

Assert.IsFalse(attribute.IsValid(null)); // Null values are invalid
Assert.IsTrue(attribute.IsValid("4"));
Assert.IsTrue(attribute.IsValid("4.5"));
Assert.IsTrue(attribute.IsValid(100));
Assert.IsTrue(attribute.IsValid("100.42"));
Assert.IsFalse(attribute.IsValid(0));
Assert.IsFalse(attribute.IsValid("0"));
Assert.IsFalse(attribute.IsValid("INVALID STRING"));
}

[TestMethod]
public void ErrorResourcesTest()
{
var attribute = new NotNullOrZeroAttribute();
attribute.ErrorMessageResourceName = "ErrorMessage";
attribute.ErrorMessageResourceType = typeof(ErrorResources);

const string invalidValue = "a";

var result = attribute.GetValidationResult(invalidValue, new ValidationContext(0, null, null));

Assert.AreEqual("error message", result.ErrorMessage);
}

[TestMethod]
public void GlobalizedErrorResourcesTest()
{
System.Threading.Thread.CurrentThread.CurrentUICulture = new CultureInfo("es-MX");

var attribute = new NotNullOrZeroAttribute();
attribute.ErrorMessageResourceName = "ErrorMessage";
attribute.ErrorMessageResourceType = typeof(ErrorResources);

const string invalidValue = "a";

var result = attribute.GetValidationResult(invalidValue, new ValidationContext(0, null, null));

Assert.AreEqual("mensaje de error", result.ErrorMessage);
}

[TestMethod]
public void ErrorMessageTest()
{
var attribute = new NotNullOrZeroAttribute();
attribute.ErrorMessage = "SampleErrorMessage";

const string invalidValue = "a";

var result = attribute.GetValidationResult(invalidValue, new ValidationContext(0, null, null));

Assert.AreEqual("SampleErrorMessage", result.ErrorMessage);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using DataAnnotationsExtensions.Core;

namespace DataAnnotationsExtensions.Web.Controllers
{
public class NotNullOrZeroController : ValidationControllerBase<NotNullOrZeroEntity>
{
}
}
Loading