├── .gitignore ├── FluentValidator.Tests ├── BoolValidationContractTests.cs ├── DateTimeValidationContractTests.cs ├── DecimalValidationContractTests.cs ├── DoubleValidationContractTests.cs ├── Entities │ └── Dummy.cs ├── FloatValidationContractTests.cs ├── FluentValidator.Tests.csproj ├── IntValidationContractTests.cs ├── NotifiableTests.cs └── StringValidationContractTests.cs ├── FluentValidator.sln ├── FluentValidator ├── FluentValidator.csproj ├── Notifications │ ├── Notifiable.cs │ └── Notification.cs └── Validation │ ├── BoolValidationContract.cs │ ├── Contracts │ ├── Contract.cs │ ├── IContract.cs │ └── IValidatable.cs │ ├── DateTimeValidationContract.cs │ ├── DecimalValidationContract.cs │ ├── DoubleValidationContract.cs │ ├── FloatValidationContract.cs │ ├── GuidValidationContract.cs │ ├── IntValidationContract.cs │ ├── ObjectValidationContract.cs │ ├── StringValidationContract.cs │ └── ValidationContract.cs └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | 4 | .vscode/* 5 | !.vscode/settings.json 6 | !.vscode/tasks.json 7 | !.vscode/launch.json 8 | !.vscode/extensions.json 9 | .DS_Store 10 | .vscode 11 | 12 | # User-specific files 13 | *.suo 14 | *.user 15 | *.userosscache 16 | *.sln.docstates 17 | 18 | # User-specific files (MonoDevelop/Xamarin Studio) 19 | *.userprefs 20 | 21 | # Build results 22 | [Dd]ebug/ 23 | [Dd]ebugPublic/ 24 | [Rr]elease/ 25 | [Rr]eleases/ 26 | x64/ 27 | x86/ 28 | bld/ 29 | [Bb]in/ 30 | [Oo]bj/ 31 | [Ll]og/ 32 | 33 | # Visual Studio 2015 cache/options directory 34 | .vs/ 35 | # Uncomment if you have tasks that create the project's static files in wwwroot 36 | #wwwroot/ 37 | 38 | # MSTest test Results 39 | [Tt]est[Rr]esult*/ 40 | [Bb]uild[Ll]og.* 41 | 42 | # NUNIT 43 | *.VisualState.xml 44 | TestResult.xml 45 | 46 | # Build Results of an ATL Project 47 | [Dd]ebugPS/ 48 | [Rr]eleasePS/ 49 | dlldata.c 50 | 51 | # DNX 52 | project.lock.json 53 | project.fragment.lock.json 54 | artifacts/ 55 | 56 | *_i.c 57 | *_p.c 58 | *_i.h 59 | *.ilk 60 | *.meta 61 | *.obj 62 | *.pch 63 | *.pdb 64 | *.pgc 65 | *.pgd 66 | *.rsp 67 | *.sbr 68 | *.tlb 69 | *.tli 70 | *.tlh 71 | *.tmp 72 | *.tmp_proj 73 | *.log 74 | *.vspscc 75 | *.vssscc 76 | .builds 77 | *.pidb 78 | *.svclog 79 | *.scc 80 | 81 | # Chutzpah Test files 82 | _Chutzpah* 83 | 84 | # Visual C++ cache files 85 | ipch/ 86 | *.aps 87 | *.ncb 88 | *.opendb 89 | *.opensdf 90 | *.sdf 91 | *.cachefile 92 | *.VC.db 93 | *.VC.VC.opendb 94 | 95 | # Visual Studio profiler 96 | *.psess 97 | *.vsp 98 | *.vspx 99 | *.sap 100 | 101 | # TFS 2012 Local Workspace 102 | $tf/ 103 | 104 | # Guidance Automation Toolkit 105 | *.gpState 106 | 107 | # ReSharper is a .NET coding add-in 108 | _ReSharper*/ 109 | *.[Rr]e[Ss]harper 110 | *.DotSettings.user 111 | 112 | # JustCode is a .NET coding add-in 113 | .JustCode 114 | 115 | # TeamCity is a build add-in 116 | _TeamCity* 117 | 118 | # DotCover is a Code Coverage Tool 119 | *.dotCover 120 | 121 | # Visual Studio code coverage results 122 | *.coverage 123 | *.coveragexml 124 | 125 | # NCrunch 126 | _NCrunch_* 127 | .*crunch*.local.xml 128 | nCrunchTemp_* 129 | 130 | # MightyMoose 131 | *.mm.* 132 | AutoTest.Net/ 133 | 134 | # Web workbench (sass) 135 | .sass-cache/ 136 | 137 | # Installshield output folder 138 | [Ee]xpress/ 139 | 140 | # DocProject is a documentation generator add-in 141 | DocProject/buildhelp/ 142 | DocProject/Help/*.HxT 143 | DocProject/Help/*.HxC 144 | DocProject/Help/*.hhc 145 | DocProject/Help/*.hhk 146 | DocProject/Help/*.hhp 147 | DocProject/Help/Html2 148 | DocProject/Help/html 149 | 150 | # Click-Once directory 151 | publish/ 152 | 153 | # Publish Web Output 154 | *.[Pp]ublish.xml 155 | *.azurePubxml 156 | # TODO: Comment the next line if you want to checkin your web deploy settings 157 | # but database connection strings (with potential passwords) will be unencrypted 158 | *.pubxml 159 | *.publishproj 160 | 161 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 162 | # checkin your Azure Web App publish settings, but sensitive information contained 163 | # in these scripts will be unencrypted 164 | PublishScripts/ 165 | 166 | # NuGet Packages 167 | *.nupkg 168 | # The packages folder can be ignored because of Package Restore 169 | **/packages/* 170 | # except build/, which is used as an MSBuild target. 171 | !**/packages/build/ 172 | # Uncomment if necessary however generally it will be regenerated when needed 173 | #!**/packages/repositories.config 174 | # NuGet v3's project.json files produces more ignoreable files 175 | *.nuget.props 176 | *.nuget.targets 177 | 178 | # Microsoft Azure Build Output 179 | csx/ 180 | *.build.csdef 181 | 182 | # Microsoft Azure Emulator 183 | ecf/ 184 | rcf/ 185 | 186 | # Windows Store app package directories and files 187 | AppPackages/ 188 | BundleArtifacts/ 189 | Package.StoreAssociation.xml 190 | _pkginfo.txt 191 | 192 | # Visual Studio cache files 193 | # files ending in .cache can be ignored 194 | *.[Cc]ache 195 | # but keep track of directories ending in .cache 196 | !*.[Cc]ache/ 197 | 198 | # Others 199 | ClientBin/ 200 | ~$* 201 | *~ 202 | *.dbmdl 203 | *.dbproj.schemaview 204 | *.jfm 205 | *.pfx 206 | *.publishsettings 207 | node_modules/ 208 | orleans.codegen.cs 209 | 210 | # Since there are multiple workflows, uncomment next line to ignore bower_components 211 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 212 | #bower_components/ 213 | 214 | # RIA/Silverlight projects 215 | Generated_Code/ 216 | 217 | # Backup & report files from converting an old project file 218 | # to a newer Visual Studio version. Backup files are not needed, 219 | # because we have git ;-) 220 | _UpgradeReport_Files/ 221 | Backup*/ 222 | UpgradeLog*.XML 223 | UpgradeLog*.htm 224 | 225 | # SQL Server files 226 | *.mdf 227 | *.ldf 228 | 229 | # Business Intelligence projects 230 | *.rdl.data 231 | *.bim.layout 232 | *.bim_*.settings 233 | 234 | # Microsoft Fakes 235 | FakesAssemblies/ 236 | 237 | # GhostDoc plugin setting file 238 | *.GhostDoc.xml 239 | 240 | # Node.js Tools for Visual Studio 241 | .ntvs_analysis.dat 242 | 243 | # Visual Studio 6 build log 244 | *.plg 245 | 246 | # Visual Studio 6 workspace options file 247 | *.opt 248 | 249 | # Visual Studio LightSwitch build output 250 | **/*.HTMLClient/GeneratedArtifacts 251 | **/*.DesktopClient/GeneratedArtifacts 252 | **/*.DesktopClient/ModelManifest.xml 253 | **/*.Server/GeneratedArtifacts 254 | **/*.Server/ModelManifest.xml 255 | _Pvt_Extensions 256 | 257 | # Paket dependency manager 258 | .paket/paket.exe 259 | paket-files/ 260 | 261 | # FAKE - F# Make 262 | .fake/ 263 | 264 | # JetBrains Rider 265 | .idea/ 266 | *.sln.iml 267 | 268 | # CodeRush 269 | .cr/ 270 | 271 | # Python Tools for Visual Studio (PTVS) 272 | __pycache__/ 273 | *.pyc 274 | 275 | # Cake - Uncomment if you are using it 276 | # tools/ 277 | -------------------------------------------------------------------------------- /FluentValidator.Tests/BoolValidationContractTests.cs: -------------------------------------------------------------------------------- 1 | using FluentValidator.Validation; 2 | using Microsoft.VisualStudio.TestTools.UnitTesting; 3 | 4 | namespace FluentValidator.Tests 5 | { 6 | [TestClass] 7 | public class BoolValidationContractTests 8 | { 9 | [TestMethod] 10 | [TestCategory("BoolValidation")] 11 | public void IsTrue() 12 | { 13 | var wrong = new ValidationContract() 14 | .Requires() 15 | .IsTrue(false, "bool", "Bool is false"); 16 | 17 | Assert.AreEqual(false, wrong.Valid); 18 | Assert.AreEqual(1, wrong.Notifications.Count); 19 | 20 | var right = new ValidationContract() 21 | .Requires() 22 | .IsTrue(true, "bool", "Bool is false"); 23 | Assert.AreEqual(true, right.Valid); 24 | } 25 | 26 | [TestMethod] 27 | [TestCategory("BoolValidation")] 28 | public void IsFalse() 29 | { 30 | var wrong = new ValidationContract() 31 | .Requires() 32 | .IsFalse(true, "bool", "Bool is true"); 33 | 34 | Assert.AreEqual(false, wrong.Valid); 35 | Assert.AreEqual(1, wrong.Notifications.Count); 36 | 37 | var right = new ValidationContract() 38 | .Requires() 39 | .IsFalse(false, "bool", "Bool is true"); 40 | Assert.AreEqual(true, right.Valid); 41 | } 42 | } 43 | } -------------------------------------------------------------------------------- /FluentValidator.Tests/DateTimeValidationContractTests.cs: -------------------------------------------------------------------------------- 1 | using FluentValidator.Validation; 2 | using Microsoft.VisualStudio.TestTools.UnitTesting; 3 | using System; 4 | 5 | namespace FluentValidator.Tests 6 | { 7 | [TestClass] 8 | public class DateTimeValidationContractTests 9 | { 10 | private Dummy _dummy; 11 | 12 | [TestMethod] 13 | [TestCategory("DateTimeValidation")] 14 | public void IsGreaterThan() 15 | { 16 | _dummy = new Dummy(); 17 | _dummy.dateTimeProp = new DateTime(2005, 5, 15, 16, 0, 0); 18 | 19 | var wrong = new ValidationContract() 20 | .Requires() 21 | .IsGreaterThan(_dummy.dateTimeProp, _dummy.dateTimeProp.AddMilliseconds(1), nameof(_dummy.dateTimeProp), "Date 1 should be greater than Date 2") 22 | .IsGreaterThan(_dummy.dateTimeProp, _dummy.dateTimeProp.AddSeconds(1), nameof(_dummy.dateTimeProp), "Date 1 should be greater than Date 2") 23 | .IsGreaterThan(_dummy.dateTimeProp, _dummy.dateTimeProp.AddMinutes(1), nameof(_dummy.dateTimeProp), "Date 1 should be greater than Date 2"); 24 | 25 | Assert.AreEqual(false, wrong.Valid); 26 | Assert.AreEqual(3, wrong.Notifications.Count); 27 | 28 | var right = new ValidationContract() 29 | .Requires() 30 | .IsGreaterThan(_dummy.dateTimeProp, _dummy.dateTimeProp.AddMilliseconds(-2), nameof(_dummy.dateTimeProp), "Date 1 is not greater than Date 2") 31 | .IsGreaterThan(_dummy.dateTimeProp, _dummy.dateTimeProp.AddSeconds(-2), nameof(_dummy.dateTimeProp), "Date 1 is not greater than Date 2") 32 | .IsGreaterThan(_dummy.dateTimeProp, _dummy.dateTimeProp.AddMinutes(-2), nameof(_dummy.dateTimeProp), "Date 1 is not greater than Date 2"); 33 | 34 | Assert.AreEqual(true, right.Valid); 35 | } 36 | 37 | [TestMethod] 38 | [TestCategory("DateTimeValidation")] 39 | public void IsGreaterOrEqualsThan() 40 | { 41 | _dummy = new Dummy(); 42 | _dummy.dateTimeProp = new DateTime(2017, 1, 1, 12, 0, 0); 43 | 44 | var wrong = new ValidationContract() 45 | .Requires() 46 | .IsGreaterOrEqualsThan(_dummy.dateTimeProp, _dummy.dateTimeProp.AddMilliseconds(1), nameof(_dummy.dateTimeProp), "Date 1 should be greater than Date 2") 47 | .IsGreaterOrEqualsThan(_dummy.dateTimeProp, _dummy.dateTimeProp.AddSeconds(1), nameof(_dummy.dateTimeProp), "Date 1 should be greater than Date 2") 48 | .IsGreaterOrEqualsThan(_dummy.dateTimeProp, _dummy.dateTimeProp.AddMinutes(1), nameof(_dummy.dateTimeProp), "Date 1 should be greater than Date 2"); 49 | 50 | Assert.AreEqual(false, wrong.Valid); 51 | Assert.AreEqual(3, wrong.Notifications.Count); 52 | 53 | var right = new ValidationContract() 54 | .Requires() 55 | .IsGreaterOrEqualsThan(_dummy.dateTimeProp, _dummy.dateTimeProp, nameof(_dummy.dateTimeProp), "Date 1 is not greater or equals than Date 2") 56 | .IsGreaterOrEqualsThan(_dummy.dateTimeProp, _dummy.dateTimeProp.AddMilliseconds(-1), nameof(_dummy.dateTimeProp), "Date 1 is not greater or equals than Date 2") 57 | .IsGreaterOrEqualsThan(_dummy.dateTimeProp, _dummy.dateTimeProp.AddSeconds(-1), nameof(_dummy.dateTimeProp), "Date 1 is not greater or equals than Date 2") 58 | .IsGreaterOrEqualsThan(_dummy.dateTimeProp, _dummy.dateTimeProp.AddMinutes(-1), nameof(_dummy.dateTimeProp), "Date 1 is not greater or equals than Date 2"); 59 | 60 | Assert.AreEqual(true, right.Valid); 61 | } 62 | 63 | [TestMethod] 64 | [TestCategory("DateTimeValidation")] 65 | public void IsLowerThan() 66 | { 67 | _dummy = new Dummy(); 68 | _dummy.dateTimeProp = new DateTime(2017, 9, 26, 15, 0, 0); 69 | 70 | var wrong = new ValidationContract() 71 | .Requires() 72 | .IsLowerThan(_dummy.dateTimeProp, _dummy.dateTimeProp.AddMilliseconds(-1), nameof(_dummy.dateTimeProp), "Date 1 should be lower than Date 2") 73 | .IsLowerThan(_dummy.dateTimeProp, _dummy.dateTimeProp.AddSeconds(-1), nameof(_dummy.dateTimeProp), "Date 1 should be lower than Date 2") 74 | .IsLowerThan(_dummy.dateTimeProp, _dummy.dateTimeProp.AddMinutes(-1), nameof(_dummy.dateTimeProp), "Date 1 should be lower than Date 2"); 75 | 76 | Assert.AreEqual(false, wrong.Valid); 77 | Assert.AreEqual(3, wrong.Notifications.Count); 78 | 79 | var right = new ValidationContract() 80 | .Requires() 81 | .IsLowerThan(_dummy.dateTimeProp, _dummy.dateTimeProp.AddMilliseconds(1),nameof(_dummy.dateTimeProp), "Date 1 is not lower than Date 2") 82 | .IsLowerThan(_dummy.dateTimeProp, _dummy.dateTimeProp.AddSeconds(1), nameof(_dummy.dateTimeProp), "Date 1 is not lower than Date 2") 83 | .IsLowerThan(_dummy.dateTimeProp, _dummy.dateTimeProp.AddMinutes(1), nameof(_dummy.dateTimeProp), "Date 1 is not lower than Date 2"); 84 | 85 | Assert.AreEqual(true, right.Valid); 86 | } 87 | 88 | [TestMethod] 89 | [TestCategory("DateTimeValidation")] 90 | public void IsLowerOrEqualsThan() 91 | { 92 | _dummy = new Dummy(); 93 | _dummy.dateTimeProp = new DateTime(2005, 5, 15, 16, 0, 0); 94 | 95 | var wrong = new ValidationContract() 96 | .Requires() 97 | .IsLowerOrEqualsThan(_dummy.dateTimeProp, _dummy.dateTimeProp.AddMilliseconds(-1), nameof(_dummy.dateTimeProp), "Date 1 should be lower or equals than Date 2") 98 | .IsLowerOrEqualsThan(_dummy.dateTimeProp, _dummy.dateTimeProp.AddSeconds(-1), nameof(_dummy.dateTimeProp), "Date 1 should be lower or equals than Date 2") 99 | .IsLowerOrEqualsThan(_dummy.dateTimeProp, _dummy.dateTimeProp.AddMinutes(-1), nameof(_dummy.dateTimeProp), "Date 1 should be lower or equals than Date 2"); 100 | 101 | Assert.AreEqual(false, wrong.Valid); 102 | Assert.AreEqual(3, wrong.Notifications.Count); 103 | 104 | var right = new ValidationContract() 105 | .Requires() 106 | .IsLowerOrEqualsThan(_dummy.dateTimeProp, _dummy.dateTimeProp, nameof(_dummy.dateTimeProp), "Date 1 is not lower or equals than Date 2") 107 | .IsLowerOrEqualsThan(_dummy.dateTimeProp, _dummy.dateTimeProp.AddMilliseconds(1), nameof(_dummy.dateTimeProp), "Date 1 is not lower or equals than Date 2") 108 | .IsLowerOrEqualsThan(_dummy.dateTimeProp, _dummy.dateTimeProp.AddSeconds(1), nameof(_dummy.dateTimeProp), "Date 1 is not lower or equals than Date 2") 109 | .IsLowerOrEqualsThan(_dummy.dateTimeProp, _dummy.dateTimeProp.AddMinutes(1), nameof(_dummy.dateTimeProp), "Date 1 is not lower or equals than Date 2"); 110 | 111 | Assert.AreEqual(true, right.Valid); 112 | } 113 | 114 | [TestMethod] 115 | [TestCategory("DateTimeValidation")] 116 | public void IsBetween() 117 | { 118 | var from = new DateTime(2017, 10, 1); 119 | var to = from.AddDays(30); 120 | 121 | var wrong = new ValidationContract() 122 | .Requires() 123 | .IsBetween(new DateTime(2017, 10, 1), from, to, "datetime", "The date must be between 01/10/2017 and 31/10/2017"); 124 | 125 | Assert.AreEqual(false, wrong.Valid); 126 | Assert.AreEqual(1, wrong.Notifications.Count); 127 | 128 | var right = new ValidationContract() 129 | .Requires() 130 | .IsBetween(new DateTime(2017, 10, 30), from, to, "datetime", "The date is between 01/10/2017 and 31/10/2017"); 131 | 132 | Assert.AreEqual(true, right.Valid); 133 | } 134 | } 135 | } 136 | -------------------------------------------------------------------------------- /FluentValidator.Tests/DecimalValidationContractTests.cs: -------------------------------------------------------------------------------- 1 | using FluentValidator.Validation; 2 | using Microsoft.VisualStudio.TestTools.UnitTesting; 3 | 4 | namespace FluentValidator.Tests 5 | { 6 | [TestClass] 7 | public class DecimalValidationContractTests 8 | { 9 | [TestMethod] 10 | [TestCategory("DecimalValidation")] 11 | public void IsGreaterThanDecimal() 12 | { 13 | decimal v1 = 5; 14 | decimal v2 = 10; 15 | var wrong = new ValidationContract() 16 | .Requires() 17 | .IsGreaterThan(v1, v2, "decimal", "V1 is not greater than v2") 18 | .IsGreaterThan(1, 1M, "decimal", "V1 is not greater than v2"); // 1 is not greater than 1 :) 19 | 20 | Assert.AreEqual(false, wrong.Valid); 21 | Assert.AreEqual(2, wrong.Notifications.Count); 22 | 23 | var right = new ValidationContract() 24 | .Requires() 25 | .IsGreaterThan(v2, v1, "decimal", "V1 is not greater than v2"); 26 | 27 | Assert.AreEqual(true, right.Valid); 28 | } 29 | 30 | [TestMethod] 31 | [TestCategory("DecimalValidation")] 32 | public void IsGreaterThanDouble() 33 | { 34 | double v1 = 5; 35 | decimal v2 = 10; 36 | var wrong = new ValidationContract() 37 | .Requires() 38 | .IsGreaterThan(v1, v2, "decimal", "V1 is not greater than v2"); 39 | 40 | Assert.AreEqual(false, wrong.Valid); 41 | Assert.AreEqual(1, wrong.Notifications.Count); 42 | 43 | v1 = 10; 44 | v2 = 5; 45 | var right = new ValidationContract() 46 | .Requires() 47 | .IsGreaterThan(v1, v2, "decimal", "V1 is not greater than v2"); 48 | 49 | Assert.AreEqual(true, right.Valid); 50 | } 51 | 52 | [TestMethod] 53 | [TestCategory("DecimalValidation")] 54 | public void IsGreaterThanFloat() 55 | { 56 | float v1 = 5; 57 | decimal v2 = 10; 58 | var wrong = new ValidationContract() 59 | .Requires() 60 | .IsGreaterThan(v1, v2, "decimal", "V1 is not greater than v2"); 61 | 62 | Assert.AreEqual(false, wrong.Valid); 63 | Assert.AreEqual(1, wrong.Notifications.Count); 64 | 65 | v1 = 10; 66 | v2 = 5; 67 | var right = new ValidationContract() 68 | .Requires() 69 | .IsGreaterThan(v1, v2, "decimal", "V1 is not greater than v2"); 70 | 71 | Assert.AreEqual(true, right.Valid); 72 | } 73 | 74 | [TestMethod] 75 | [TestCategory("DecimalValidation")] 76 | public void IsGreaterThanInt() 77 | { 78 | int v1 = 5; 79 | decimal v2 = 10; 80 | var wrong = new ValidationContract() 81 | .Requires() 82 | .IsGreaterThan(v1, v2, "decimal", "V1 is not greater than v2"); 83 | 84 | Assert.AreEqual(false, wrong.Valid); 85 | Assert.AreEqual(1, wrong.Notifications.Count); 86 | 87 | var right = new ValidationContract() 88 | .Requires() 89 | .IsGreaterThan(v2, v1, "decimal", "V1 is not greater than v2"); 90 | 91 | Assert.AreEqual(true, right.Valid); 92 | } 93 | 94 | [TestMethod] 95 | [TestCategory("DecimalValidation")] 96 | public void IsBetweenDecimal() 97 | { 98 | decimal value = -1.01M; 99 | decimal from = 1.01M; 100 | decimal to = 10; 101 | 102 | var wrong = new ValidationContract() 103 | .Requires() 104 | .IsBetween(value, from, to, "decimal", "The value -1.01 must be between 1.01 and 10"); 105 | 106 | Assert.AreEqual(false, wrong.Valid); 107 | Assert.AreEqual(1, wrong.Notifications.Count); 108 | 109 | value = 1.015M; 110 | from = 1.01M; 111 | to = 1.02M; 112 | 113 | var right = new ValidationContract() 114 | .Requires() 115 | .IsBetween(value, from, to, "decimal", "The value 1.015 is between 1.01 and 1.02"); 116 | 117 | Assert.AreEqual(true, right.Valid); 118 | } 119 | } 120 | } 121 | -------------------------------------------------------------------------------- /FluentValidator.Tests/DoubleValidationContractTests.cs: -------------------------------------------------------------------------------- 1 | using FluentValidator.Validation; 2 | using Microsoft.VisualStudio.TestTools.UnitTesting; 3 | 4 | namespace FluentValidator.Tests 5 | { 6 | [TestClass] 7 | public class DoubleValidationContractTests 8 | { 9 | [TestMethod] 10 | [TestCategory("DoubleValidation")] 11 | public void IsBetweenDouble() 12 | { 13 | double value = 49.999; 14 | double from = 50.000; 15 | double to = 59.999; 16 | 17 | var wrong = new ValidationContract() 18 | .Requires() 19 | .IsBetween(value, from, to, "double", "The value 49.999 must be between 50.000 and 59.999"); 20 | 21 | Assert.AreEqual(false, wrong.Valid); 22 | Assert.AreEqual(1, wrong.Notifications.Count); 23 | 24 | value = 1250.01; 25 | from = 1250.00; 26 | to = 1299.99; 27 | 28 | var right = new ValidationContract() 29 | .Requires() 30 | .IsBetween(value, from, to, "double", "The value 1250.01 is between 1000.01 and 1299.99"); 31 | 32 | Assert.AreEqual(true, right.Valid); 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /FluentValidator.Tests/Entities/Dummy.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace FluentValidator.Tests 4 | { 5 | public class Dummy 6 | { 7 | public bool boolProp { get; set; } 8 | public decimal decimalProp { get; set; } 9 | public double doubleProp { get; set; } 10 | public float floatProp { get; set; } 11 | public int intProp { get; set; } 12 | public long longProp { get; set; } 13 | public object objectProp { get; set; } 14 | public string stringProp { get; set; } 15 | public DateTime dateTimeProp {get;set;} 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /FluentValidator.Tests/FloatValidationContractTests.cs: -------------------------------------------------------------------------------- 1 | using FluentValidator.Validation; 2 | using Microsoft.VisualStudio.TestTools.UnitTesting; 3 | 4 | namespace FluentValidator.Tests 5 | { 6 | [TestClass] 7 | public class FloatValidationContractTests 8 | { 9 | [TestMethod] 10 | [TestCategory("FloatValidation")] 11 | public void IsBetweenFloat() 12 | { 13 | float value = -15; 14 | float from = -1.000F; 15 | float to = 1.999F; 16 | 17 | var wrong = new ValidationContract() 18 | .Requires() 19 | .IsBetween(value, from, to, "float", "The value -15 must be between -1.000 and 1.999"); 20 | 21 | Assert.AreEqual(false, wrong.Valid); 22 | Assert.AreEqual(1, wrong.Notifications.Count); 23 | 24 | value = 0; 25 | from = float.MinValue; 26 | to = float.MaxValue; 27 | 28 | var right = new ValidationContract() 29 | .Requires() 30 | .IsBetween(value, from, to, "float", $"The value 0 is between {from} and {to}"); 31 | 32 | Assert.AreEqual(true, right.Valid); 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /FluentValidator.Tests/FluentValidator.Tests.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netcoreapp1.0 5 | 6 | false 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /FluentValidator.Tests/IntValidationContractTests.cs: -------------------------------------------------------------------------------- 1 | using FluentValidator.Validation; 2 | using Microsoft.VisualStudio.TestTools.UnitTesting; 3 | 4 | namespace FluentValidator.Tests 5 | { 6 | [TestClass] 7 | public class IntValidationContractTests 8 | { 9 | [TestMethod] 10 | [TestCategory("IntValidation")] 11 | public void IsBetweenInt() 12 | { 13 | int value = 11; 14 | int from = 1; 15 | int to = 10; 16 | 17 | var wrong = new ValidationContract() 18 | .Requires() 19 | .IsBetween(value, from, to, "int", "The value 11 must be between 1 and 10"); 20 | 21 | Assert.AreEqual(false, wrong.Valid); 22 | Assert.AreEqual(1, wrong.Notifications.Count); 23 | 24 | value = 5; 25 | from = 1; 26 | to = 10; 27 | 28 | var right = new ValidationContract() 29 | .Requires() 30 | .IsBetween(5, 1, 10, "int", "The value 5 is between 1 and 10"); 31 | 32 | Assert.AreEqual(true, right.Valid); 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /FluentValidator.Tests/NotifiableTests.cs: -------------------------------------------------------------------------------- 1 | using FluentValidator.Validation; 2 | using Microsoft.VisualStudio.TestTools.UnitTesting; 3 | using System; 4 | using System.Collections.Generic; 5 | 6 | namespace FluentValidator.Tests 7 | { 8 | [TestClass] 9 | public class NotifiableTests : Notifiable 10 | { 11 | [TestMethod] 12 | [TestCategory("Notifiable")] 13 | public void AddNotificationForOneNotifiable() 14 | { 15 | var name =new Name(); 16 | var cus = new Customer(); 17 | 18 | AddNotifications(name); 19 | AddNotifications(cus); 20 | 21 | Assert.AreEqual(false, Valid); 22 | Assert.AreEqual(2, Notifications.Count); 23 | } 24 | 25 | [TestMethod] 26 | [TestCategory("Notifiable")] 27 | public void AddNotificationForMultipleNotifiable() 28 | { 29 | var name = new Name(); 30 | var customer = new Customer(); 31 | 32 | AddNotifications(name, customer); 33 | 34 | Assert.AreEqual(false, Valid); 35 | Assert.AreEqual(2, Notifications.Count); 36 | } 37 | 38 | [TestMethod] 39 | [TestCategory("Notifiable")] 40 | public void NotifiableWithNotifications() 41 | { 42 | Invoice invoice = new Invoice(0, DateTime.Today, 100, new Address("Name")); 43 | 44 | Assert.IsFalse(invoice.Valid); 45 | Assert.AreEqual(1, invoice.Notifications.Count); 46 | } 47 | 48 | [TestMethod] 49 | [TestCategory("Notifiable")] 50 | public void CascatedNotifiable() 51 | { 52 | Invoice invoice = new Invoice(0, DateTime.Today, 100, new Address(string.Empty)); 53 | 54 | Assert.IsFalse(invoice.Valid); 55 | Assert.AreEqual(2, invoice.Notifications.Count); 56 | } 57 | 58 | [TestMethod] 59 | [TestCategory("Notifiable")] 60 | public void DontRepeatNotifications() 61 | { 62 | Invoice invoice = new Invoice(0, DateTime.Today, 100, new Address(string.Empty)); 63 | 64 | Assert.IsFalse(invoice.Valid); 65 | Assert.IsFalse(invoice.Valid); 66 | 67 | Assert.AreEqual(2, invoice.Notifications.Count); 68 | } 69 | } 70 | 71 | public class Customer : Notifiable 72 | { 73 | public Customer() 74 | { 75 | AddNotification("Test", "Testing"); 76 | } 77 | 78 | public Name Name { get; set; } 79 | } 80 | 81 | public class Name : Notifiable 82 | { 83 | public Name() 84 | { 85 | AddNotification("Test", "Testing"); 86 | } 87 | 88 | public string FirstName { get; set; } 89 | public string LastName { get; set; } 90 | } 91 | 92 | public class Invoice : Notifiable 93 | { 94 | public int Code { get; private set; } 95 | public DateTime Date { get; private set; } 96 | public decimal Value { get; private set; } 97 | public Address Address { get; private set; } 98 | 99 | public Invoice(int code, DateTime date, decimal value, Address address) 100 | { 101 | Code = code; 102 | Date = date; 103 | Value = value; 104 | Address = address; 105 | } 106 | 107 | protected override IEnumerable Validations() 108 | { 109 | return new ValidationContract() 110 | .Requires() 111 | .Concat(Address) 112 | .IsGreaterThan(Code, 0, "Code", "Code is required") 113 | .IsGreaterThan(Date, DateTime.MinValue, "Date", "Date is required") 114 | .Notifications; 115 | } 116 | } 117 | 118 | public class Address : Notifiable 119 | { 120 | public string Value { get; private set; } 121 | 122 | public Address(string value) 123 | { 124 | Value = value; 125 | } 126 | 127 | protected override IEnumerable Validations() 128 | { 129 | return new ValidationContract() 130 | .Requires() 131 | .IsNotNullOrEmpty(Value, "Address", "Address is required") 132 | .Notifications; 133 | } 134 | } 135 | } 136 | -------------------------------------------------------------------------------- /FluentValidator.Tests/StringValidationContractTests.cs: -------------------------------------------------------------------------------- 1 | using FluentValidator.Validation; 2 | using Microsoft.VisualStudio.TestTools.UnitTesting; 3 | 4 | namespace FluentValidator.Tests 5 | { 6 | [TestClass] 7 | public class StringValidationContractTests 8 | { 9 | [TestMethod] 10 | [TestCategory("StringValidation")] 11 | public void IsNotNullOrEmpty() 12 | { 13 | var wrong = new ValidationContract() 14 | .Requires() 15 | .IsNotNullOrEmpty(null, "string", "String is Null") 16 | .IsNotNullOrEmpty("", "string", "String is Empty"); 17 | 18 | Assert.AreEqual(false, wrong.Valid); 19 | Assert.AreEqual(2, wrong.Notifications.Count); 20 | 21 | var right = new ValidationContract() 22 | .Requires() 23 | .IsNotNullOrEmpty("Some valid string", "string", "String is Null"); 24 | Assert.AreEqual(true, right.Valid); 25 | } 26 | 27 | [TestMethod] 28 | [TestCategory("StringValidation")] 29 | public void IsNullOrEmpty() 30 | { 31 | var right = new ValidationContract() 32 | .Requires() 33 | .IsNullOrEmpty(null, "string", "String is Null") 34 | .IsNullOrEmpty("", "string", "String is Empty"); 35 | 36 | Assert.AreEqual(true, right.Valid); 37 | Assert.AreEqual(0, right.Notifications.Count); 38 | 39 | var wrong = new ValidationContract() 40 | .Requires() 41 | .IsNullOrEmpty("Some valid string", "string", "String is Null"); 42 | Assert.AreEqual(false, wrong.Valid); 43 | } 44 | 45 | [TestMethod] 46 | [TestCategory("StringValidation")] 47 | public void MinLen() 48 | { 49 | var wrong = new ValidationContract() 50 | .Requires() 51 | .HasMinLen("null", 5, "string", "String len is less than permited"); 52 | 53 | Assert.AreEqual(false, wrong.Valid); 54 | Assert.AreEqual(1, wrong.Notifications.Count); 55 | 56 | var right = new ValidationContract() 57 | .Requires() 58 | .HasMinLen("Some Valid String", 5, "string", "String len is less than permited"); 59 | 60 | Assert.AreEqual(true, right.Valid); 61 | } 62 | 63 | [TestMethod] 64 | [TestCategory("StringValidation")] 65 | public void MaxLen() 66 | { 67 | var x = new ValidationContract() 68 | .Requires() 69 | .HasMaxLen("null", 3, "string", "String len is more than permited"); 70 | 71 | Assert.AreEqual(false, x.Valid); 72 | Assert.AreEqual(1, x.Notifications.Count); 73 | 74 | var right = new ValidationContract() 75 | .Requires() 76 | .HasMaxLen("Some", 5, "string", "String len is less than permited"); 77 | 78 | Assert.AreEqual(true, right.Valid); 79 | } 80 | 81 | [TestMethod] 82 | [TestCategory("StringValidation")] 83 | public void Len() 84 | { 85 | var x = new ValidationContract() 86 | .Requires() 87 | .HasLen("null", 3, "string", "String len is more than permited"); 88 | 89 | Assert.AreEqual(false, x.Valid); 90 | Assert.AreEqual(1, x.Notifications.Count); 91 | 92 | var right = new ValidationContract() 93 | .Requires() 94 | .HasLen("Some1", 5, "string", "String len is less than permited"); 95 | 96 | Assert.AreEqual(true, right.Valid); 97 | } 98 | 99 | [TestMethod] 100 | [TestCategory("StringValidation")] 101 | public void Contains() 102 | { 103 | var x = new ValidationContract() 104 | .Requires() 105 | .Contains("some text here", "banana", "string", "String does not contains banana"); 106 | 107 | Assert.AreEqual(false, x.Valid); 108 | Assert.AreEqual(1, x.Notifications.Count); 109 | 110 | var right = new ValidationContract() 111 | .Requires() 112 | .Contains("some banana here", "banana", "string", "String does not contains banana"); 113 | 114 | Assert.AreEqual(true, right.Valid); 115 | } 116 | 117 | [TestMethod] 118 | [TestCategory("StringValidation")] 119 | public void Email() 120 | { 121 | var wrong = new ValidationContract() 122 | .Requires() 123 | .IsEmail("wrongemail", "string", "Invalid E-mail"); 124 | 125 | Assert.AreEqual(false, wrong.Valid); 126 | Assert.AreEqual(1, wrong.Notifications.Count); 127 | 128 | var right = new ValidationContract() 129 | .Requires() 130 | .IsEmail("andrebaltieri@gmail.com", "string", "Invalid E-mail"); 131 | 132 | Assert.AreEqual(true, right.Valid); 133 | } 134 | 135 | [TestMethod] 136 | [TestCategory("StringValidation")] 137 | public void EmailOrEmpty() 138 | { 139 | var right = new ValidationContract() 140 | .Requires() 141 | .IsEmailOrEmpty("", "string", "Invalid E-mail"); 142 | 143 | Assert.AreEqual(true, right.Valid); 144 | } 145 | 146 | [TestMethod] 147 | [TestCategory("StringValidation")] 148 | public void Url() 149 | { 150 | var wrong = new ValidationContract() 151 | .Requires() 152 | .IsUrl("wrongurl", "string", "Invalid URL"); 153 | 154 | Assert.AreEqual(false, wrong.Valid); 155 | Assert.AreEqual(1, wrong.Notifications.Count); 156 | 157 | var right = new ValidationContract() 158 | .Requires() 159 | .IsUrl("https://gmail.com", "string", "Invalid URL") 160 | .IsUrl("http://gmail.com", "string", "Invalid URL") 161 | .IsUrl("http://balta.io/", "string", "Invalid URL"); 162 | 163 | Assert.AreEqual(true, right.Valid); 164 | } 165 | 166 | [TestMethod] 167 | [TestCategory("StringValidation")] 168 | public void UrlOrEmpty() 169 | { 170 | var right = new ValidationContract() 171 | .Requires() 172 | .IsUrlOrEmpty("", "string", "Invalid URL"); 173 | 174 | Assert.AreEqual(true, right.Valid); 175 | } 176 | 177 | [TestMethod] 178 | [TestCategory("StringValidation")] 179 | public void Match() 180 | { 181 | var wrong = new ValidationContract() 182 | .Requires() 183 | .Matchs("wrongurl", @"^(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$", "string", "Invalid URL"); 184 | 185 | Assert.AreEqual(false, wrong.Valid); 186 | Assert.AreEqual(1, wrong.Notifications.Count); 187 | 188 | var right = new ValidationContract() 189 | .Requires() 190 | .Matchs("http://gmail.com", @"^(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$", "string", "Invalid URL"); 191 | 192 | Assert.AreEqual(true, right.Valid); 193 | } 194 | } 195 | } 196 | -------------------------------------------------------------------------------- /FluentValidator.sln: -------------------------------------------------------------------------------- 1 | Microsoft Visual Studio Solution File, Format Version 12.00 2 | # Visual Studio 15 3 | VisualStudioVersion = 15.0.26730.16 4 | MinimumVisualStudioVersion = 15.0.26124.0 5 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FluentValidator", "FluentValidator\FluentValidator.csproj", "{BF5686E0-14F3-4A6A-9C0D-8B5B7AE1944C}" 6 | EndProject 7 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FluentValidator.Tests", "FluentValidator.Tests\FluentValidator.Tests.csproj", "{D029030D-4E22-4EBA-A3E4-B5E5B53A4C8D}" 8 | EndProject 9 | Global 10 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 11 | Debug|Any CPU = Debug|Any CPU 12 | Debug|x64 = Debug|x64 13 | Debug|x86 = Debug|x86 14 | Release|Any CPU = Release|Any CPU 15 | Release|x64 = Release|x64 16 | Release|x86 = Release|x86 17 | EndGlobalSection 18 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 19 | {BF5686E0-14F3-4A6A-9C0D-8B5B7AE1944C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 20 | {BF5686E0-14F3-4A6A-9C0D-8B5B7AE1944C}.Debug|Any CPU.Build.0 = Debug|Any CPU 21 | {BF5686E0-14F3-4A6A-9C0D-8B5B7AE1944C}.Debug|x64.ActiveCfg = Debug|Any CPU 22 | {BF5686E0-14F3-4A6A-9C0D-8B5B7AE1944C}.Debug|x64.Build.0 = Debug|Any CPU 23 | {BF5686E0-14F3-4A6A-9C0D-8B5B7AE1944C}.Debug|x86.ActiveCfg = Debug|Any CPU 24 | {BF5686E0-14F3-4A6A-9C0D-8B5B7AE1944C}.Debug|x86.Build.0 = Debug|Any CPU 25 | {BF5686E0-14F3-4A6A-9C0D-8B5B7AE1944C}.Release|Any CPU.ActiveCfg = Release|Any CPU 26 | {BF5686E0-14F3-4A6A-9C0D-8B5B7AE1944C}.Release|Any CPU.Build.0 = Release|Any CPU 27 | {BF5686E0-14F3-4A6A-9C0D-8B5B7AE1944C}.Release|x64.ActiveCfg = Release|Any CPU 28 | {BF5686E0-14F3-4A6A-9C0D-8B5B7AE1944C}.Release|x64.Build.0 = Release|Any CPU 29 | {BF5686E0-14F3-4A6A-9C0D-8B5B7AE1944C}.Release|x86.ActiveCfg = Release|Any CPU 30 | {BF5686E0-14F3-4A6A-9C0D-8B5B7AE1944C}.Release|x86.Build.0 = Release|Any CPU 31 | {D029030D-4E22-4EBA-A3E4-B5E5B53A4C8D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 32 | {D029030D-4E22-4EBA-A3E4-B5E5B53A4C8D}.Debug|Any CPU.Build.0 = Debug|Any CPU 33 | {D029030D-4E22-4EBA-A3E4-B5E5B53A4C8D}.Debug|x64.ActiveCfg = Debug|Any CPU 34 | {D029030D-4E22-4EBA-A3E4-B5E5B53A4C8D}.Debug|x64.Build.0 = Debug|Any CPU 35 | {D029030D-4E22-4EBA-A3E4-B5E5B53A4C8D}.Debug|x86.ActiveCfg = Debug|Any CPU 36 | {D029030D-4E22-4EBA-A3E4-B5E5B53A4C8D}.Debug|x86.Build.0 = Debug|Any CPU 37 | {D029030D-4E22-4EBA-A3E4-B5E5B53A4C8D}.Release|Any CPU.ActiveCfg = Release|Any CPU 38 | {D029030D-4E22-4EBA-A3E4-B5E5B53A4C8D}.Release|Any CPU.Build.0 = Release|Any CPU 39 | {D029030D-4E22-4EBA-A3E4-B5E5B53A4C8D}.Release|x64.ActiveCfg = Release|Any CPU 40 | {D029030D-4E22-4EBA-A3E4-B5E5B53A4C8D}.Release|x64.Build.0 = Release|Any CPU 41 | {D029030D-4E22-4EBA-A3E4-B5E5B53A4C8D}.Release|x86.ActiveCfg = Release|Any CPU 42 | {D029030D-4E22-4EBA-A3E4-B5E5B53A4C8D}.Release|x86.Build.0 = Release|Any CPU 43 | EndGlobalSection 44 | GlobalSection(SolutionProperties) = preSolution 45 | HideSolutionNode = FALSE 46 | EndGlobalSection 47 | GlobalSection(ExtensibilityGlobals) = postSolution 48 | SolutionGuid = {0AEFBA30-6B77-4B06-B028-773F80D810BC} 49 | EndGlobalSection 50 | EndGlobal 51 | -------------------------------------------------------------------------------- /FluentValidator/FluentValidator.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netstandard1.3 5 | 2.0.3 6 | André Baltieri 7 | balta.io 8 | Fluent Validator is a fluent way to use Notification Pattern with your entities 9 | https://github.com/andrebaltieri/FluentValidator 10 | https://github.com/andrebaltieri/FluentValidator 11 | https://github.com/andrebaltieri/FluentValidator 12 | 2.0.3.0 13 | 2.0.3.0 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /FluentValidator/Notifications/Notifiable.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Linq; 3 | 4 | namespace FluentValidator 5 | { 6 | public abstract class Notifiable 7 | { 8 | private readonly List _notifications; 9 | 10 | protected Notifiable() { _notifications = new List(); } 11 | 12 | public IReadOnlyCollection Notifications => 13 | new List(_notifications).Concat(GetNotificationsFromValidations()).ToList(); 14 | 15 | public void AddNotification(string property, string message) 16 | { 17 | _notifications.Add(new Notification(property, message)); 18 | } 19 | 20 | public void AddNotification(Notification notification) 21 | { 22 | _notifications.Add(notification); 23 | } 24 | 25 | public void AddNotifications(IReadOnlyCollection notifications) 26 | { 27 | _notifications.AddRange(notifications); 28 | } 29 | 30 | public void AddNotifications(IList notifications) 31 | { 32 | _notifications.AddRange(notifications); 33 | } 34 | 35 | public void AddNotifications(ICollection notifications) 36 | { 37 | _notifications.AddRange(notifications); 38 | } 39 | 40 | public void AddNotifications(Notifiable item) 41 | { 42 | AddNotifications(item.Notifications); 43 | } 44 | 45 | public void AddNotifications(params Notifiable[] items) 46 | { 47 | foreach (var item in items) 48 | AddNotifications(item); 49 | } 50 | 51 | protected virtual IEnumerable Validations() => null; 52 | 53 | private IEnumerable GetNotificationsFromValidations() 54 | { 55 | return Validations() ?? new List(); 56 | } 57 | 58 | public bool Invalid => _notifications.Any() || GetNotificationsFromValidations().Any(); 59 | public bool Valid => !Invalid; 60 | } 61 | } -------------------------------------------------------------------------------- /FluentValidator/Notifications/Notification.cs: -------------------------------------------------------------------------------- 1 | namespace FluentValidator 2 | { 3 | public sealed class Notification 4 | { 5 | public Notification(string property, string message) 6 | { 7 | Property = property; 8 | Message = message; 9 | } 10 | 11 | public string Property { get; private set; } 12 | public string Message { get; private set; } 13 | } 14 | } -------------------------------------------------------------------------------- /FluentValidator/Validation/BoolValidationContract.cs: -------------------------------------------------------------------------------- 1 | namespace FluentValidator.Validation 2 | { 3 | public partial class ValidationContract 4 | { 5 | public ValidationContract IsTrue(bool val, string property, string message) 6 | { 7 | if (!val) 8 | AddNotification(property, message); 9 | 10 | return this; 11 | } 12 | 13 | public ValidationContract IsFalse(bool val, string property, string message) 14 | { 15 | if (val) 16 | AddNotification(property, message); 17 | 18 | return this; 19 | } 20 | } 21 | } -------------------------------------------------------------------------------- /FluentValidator/Validation/Contracts/Contract.cs: -------------------------------------------------------------------------------- 1 | namespace FluentValidator.Validation 2 | { 3 | public abstract class Contract : Notifiable 4 | { 5 | protected Contract() 6 | { 7 | ValidationContract = new ValidationContract(); 8 | } 9 | 10 | public ValidationContract ValidationContract { get; set; } 11 | } 12 | } -------------------------------------------------------------------------------- /FluentValidator/Validation/Contracts/IContract.cs: -------------------------------------------------------------------------------- 1 | namespace FluentValidator.Validation 2 | { 3 | public interface IContract 4 | { 5 | ValidationContract Contract { get; } 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /FluentValidator/Validation/Contracts/IValidatable.cs: -------------------------------------------------------------------------------- 1 | namespace FluentValidator.Validation 2 | { 3 | public interface IValidatable 4 | { 5 | void Validate(); 6 | } 7 | } -------------------------------------------------------------------------------- /FluentValidator/Validation/DateTimeValidationContract.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace FluentValidator.Validation 4 | { 5 | public partial class ValidationContract 6 | { 7 | public ValidationContract IsGreaterThan(DateTime val, DateTime comparer, string property, string message) 8 | { 9 | if (val <= comparer) 10 | AddNotification(property, message); 11 | 12 | return this; 13 | } 14 | 15 | public ValidationContract IsGreaterOrEqualsThan(DateTime val, DateTime comparer, string property, string message) 16 | { 17 | if (val < comparer) 18 | AddNotification(property, message); 19 | 20 | return this; 21 | } 22 | 23 | public ValidationContract IsLowerThan(DateTime val, DateTime comparer, string property, string message) 24 | { 25 | if (val >= comparer) 26 | AddNotification(property, message); 27 | 28 | return this; 29 | } 30 | 31 | public ValidationContract IsLowerOrEqualsThan(DateTime val, DateTime comparer, string property, string message) 32 | { 33 | if (val > comparer) 34 | AddNotification(property, message); 35 | 36 | return this; 37 | } 38 | 39 | public ValidationContract IsBetween(DateTime val, DateTime from, DateTime to, string property, string message) 40 | { 41 | if (!(val > from && val < to)) 42 | AddNotification(property, message); 43 | 44 | return this; 45 | } 46 | 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /FluentValidator/Validation/DecimalValidationContract.cs: -------------------------------------------------------------------------------- 1 | namespace FluentValidator.Validation 2 | { 3 | public partial class ValidationContract 4 | { 5 | #region IsGreaterThan 6 | public ValidationContract IsGreaterThan(decimal val, decimal comparer, string property, string message) 7 | { 8 | if (val <= comparer) 9 | AddNotification(property, message); 10 | 11 | return this; 12 | } 13 | 14 | public ValidationContract IsGreaterThan(double val, decimal comparer, string property, string message) 15 | { 16 | if (val <= (double)comparer) 17 | AddNotification(property, message); 18 | 19 | return this; 20 | } 21 | 22 | public ValidationContract IsGreaterThan(float val, decimal comparer, string property, string message) 23 | { 24 | if (val <= (float)comparer) 25 | AddNotification(property, message); 26 | 27 | return this; 28 | } 29 | 30 | public ValidationContract IsGreaterThan(int val, decimal comparer, string property, string message) 31 | { 32 | if (val <= comparer) 33 | AddNotification(property, message); 34 | 35 | return this; 36 | } 37 | #endregion 38 | 39 | #region IsGreaterOrEqualsThan 40 | public ValidationContract IsGreaterOrEqualsThan(decimal val, decimal comparer, string property, string message) 41 | { 42 | if (val < comparer) 43 | AddNotification(property, message); 44 | 45 | return this; 46 | } 47 | 48 | public ValidationContract IsGreaterOrEqualsThan(double val, decimal comparer, string property, string message) 49 | { 50 | if (val < (double)comparer) 51 | AddNotification(property, message); 52 | 53 | return this; 54 | } 55 | 56 | public ValidationContract IsGreaterOrEqualsThan(float val, decimal comparer, string property, string message) 57 | { 58 | if (val < (float)comparer) 59 | AddNotification(property, message); 60 | 61 | return this; 62 | } 63 | 64 | public ValidationContract IsGreaterOrEqualsThan(int val, decimal comparer, string property, string message) 65 | { 66 | if (val < comparer) 67 | AddNotification(property, message); 68 | 69 | return this; 70 | } 71 | #endregion 72 | 73 | #region IsLowerThan 74 | public ValidationContract IsLowerThan(decimal val, decimal comparer, string property, string message) 75 | { 76 | if (val >= comparer) 77 | AddNotification(property, message); 78 | 79 | return this; 80 | } 81 | 82 | public ValidationContract IsLowerThan(double val, decimal comparer, string property, string message) 83 | { 84 | if (val >= (double)comparer) 85 | AddNotification(property, message); 86 | 87 | return this; 88 | } 89 | 90 | public ValidationContract IsLowerThan(float val, decimal comparer, string property, string message) 91 | { 92 | if (val >= (float)comparer) 93 | AddNotification(property, message); 94 | 95 | return this; 96 | } 97 | 98 | public ValidationContract IsLowerThan(int val, decimal comparer, string property, string message) 99 | { 100 | if (val >= comparer) 101 | AddNotification(property, message); 102 | 103 | return this; 104 | } 105 | #endregion 106 | 107 | #region IsLowerOrEqualsThan 108 | public ValidationContract IsLowerOrEqualsThan(decimal val, decimal comparer, string property, string message) 109 | { 110 | if (val > comparer) 111 | AddNotification(property, message); 112 | 113 | return this; 114 | } 115 | 116 | public ValidationContract IsLowerOrEqualsThan(double val, decimal comparer, string property, string message) 117 | { 118 | if (val > (double)comparer) 119 | AddNotification(property, message); 120 | 121 | return this; 122 | } 123 | 124 | public ValidationContract IsLowerOrEqualsThan(float val, decimal comparer, string property, string message) 125 | { 126 | if (val > (float)comparer) 127 | AddNotification(property, message); 128 | 129 | return this; 130 | } 131 | 132 | public ValidationContract IsLowerOrEqualsThan(int val, decimal comparer, string property, string message) 133 | { 134 | if (val > comparer) 135 | AddNotification(property, message); 136 | 137 | return this; 138 | } 139 | #endregion 140 | 141 | #region AreEquals 142 | public ValidationContract AreEquals(decimal val, decimal comparer, string property, string message) 143 | { 144 | if (val != comparer) 145 | AddNotification(property, message); 146 | 147 | return this; 148 | } 149 | 150 | public ValidationContract AreEquals(double val, decimal comparer, string property, string message) 151 | { 152 | if (val != (double)comparer) 153 | AddNotification(property, message); 154 | 155 | return this; 156 | } 157 | 158 | public ValidationContract AreEquals(float val, decimal comparer, string property, string message) 159 | { 160 | if (val != (float)comparer) 161 | AddNotification(property, message); 162 | 163 | return this; 164 | } 165 | 166 | public ValidationContract AreEquals(int val, decimal comparer, string property, string message) 167 | { 168 | if (val != comparer) 169 | AddNotification(property, message); 170 | 171 | return this; 172 | } 173 | #endregion 174 | 175 | #region AreNotEquals 176 | public ValidationContract AreNotEquals(decimal val, decimal comparer, string property, string message) 177 | { 178 | if (val == comparer) 179 | AddNotification(property, message); 180 | 181 | return this; 182 | } 183 | 184 | public ValidationContract AreNotEquals(double val, decimal comparer, string property, string message) 185 | { 186 | if (val == (double)comparer) 187 | AddNotification(property, message); 188 | 189 | return this; 190 | } 191 | 192 | public ValidationContract AreNotEquals(float val, decimal comparer, string property, string message) 193 | { 194 | if (val == (float)comparer) 195 | AddNotification(property, message); 196 | 197 | return this; 198 | } 199 | 200 | public ValidationContract AreNotEquals(int val, decimal comparer, string property, string message) 201 | { 202 | if (val == comparer) 203 | AddNotification(property, message); 204 | 205 | return this; 206 | } 207 | #endregion 208 | 209 | #region Between 210 | public ValidationContract IsBetween(decimal val, decimal from, decimal to, string property, string message) 211 | { 212 | if (!(val > from && val < to)) 213 | AddNotification(property, message); 214 | 215 | return this; 216 | } 217 | #endregion 218 | } 219 | } 220 | -------------------------------------------------------------------------------- /FluentValidator/Validation/DoubleValidationContract.cs: -------------------------------------------------------------------------------- 1 | namespace FluentValidator.Validation 2 | { 3 | public partial class ValidationContract 4 | { 5 | #region IsGreaterThan 6 | public ValidationContract IsGreaterThan(decimal val, double comparer, string property, string message) 7 | { 8 | if ((double)val <= comparer) 9 | AddNotification(property, message); 10 | 11 | return this; 12 | } 13 | 14 | public ValidationContract IsGreaterThan(double val, double comparer, string property, string message) 15 | { 16 | if (val <= comparer) 17 | AddNotification(property, message); 18 | 19 | return this; 20 | } 21 | 22 | public ValidationContract IsGreaterThan(float val, double comparer, string property, string message) 23 | { 24 | if (val <= comparer) 25 | AddNotification(property, message); 26 | 27 | return this; 28 | } 29 | 30 | public ValidationContract IsGreaterThan(int val, double comparer, string property, string message) 31 | { 32 | if (val <= comparer) 33 | AddNotification(property, message); 34 | 35 | return this; 36 | } 37 | #endregion 38 | 39 | #region IsGreaterOrEqualsThan 40 | public ValidationContract IsGreaterOrEqualsThan(decimal val, double comparer, string property, string message) 41 | { 42 | if ((double)val < comparer) 43 | AddNotification(property, message); 44 | 45 | return this; 46 | } 47 | 48 | public ValidationContract IsGreaterOrEqualsThan(double val, double comparer, string property, string message) 49 | { 50 | if (val < comparer) 51 | AddNotification(property, message); 52 | 53 | return this; 54 | } 55 | 56 | public ValidationContract IsGreaterOrEqualsThan(float val, double comparer, string property, string message) 57 | { 58 | if (val < comparer) 59 | AddNotification(property, message); 60 | 61 | return this; 62 | } 63 | 64 | public ValidationContract IsGreaterOrEqualsThan(int val, double comparer, string property, string message) 65 | { 66 | if (val < comparer) 67 | AddNotification(property, message); 68 | 69 | return this; 70 | } 71 | #endregion 72 | 73 | #region IsLowerThan 74 | public ValidationContract IsLowerThan(decimal val, double comparer, string property, string message) 75 | { 76 | if ((double)val >= comparer) 77 | AddNotification(property, message); 78 | 79 | return this; 80 | } 81 | 82 | public ValidationContract IsLowerThan(double val, double comparer, string property, string message) 83 | { 84 | if (val >= comparer) 85 | AddNotification(property, message); 86 | 87 | return this; 88 | } 89 | 90 | public ValidationContract IsLowerThan(float val, double comparer, string property, string message) 91 | { 92 | if (val >= comparer) 93 | AddNotification(property, message); 94 | 95 | return this; 96 | } 97 | 98 | public ValidationContract IsLowerThan(int val, double comparer, string property, string message) 99 | { 100 | if (val >= comparer) 101 | AddNotification(property, message); 102 | 103 | return this; 104 | } 105 | #endregion 106 | 107 | #region IsLowerOrEqualsThan 108 | public ValidationContract IsLowerOrEqualsThan(decimal val, double comparer, string property, string message) 109 | { 110 | if ((double)val > comparer) 111 | AddNotification(property, message); 112 | 113 | return this; 114 | } 115 | 116 | public ValidationContract IsLowerOrEqualsThan(double val, double comparer, string property, string message) 117 | { 118 | if (val > comparer) 119 | AddNotification(property, message); 120 | 121 | return this; 122 | } 123 | 124 | public ValidationContract IsLowerOrEqualsThan(float val, double comparer, string property, string message) 125 | { 126 | if (val > comparer) 127 | AddNotification(property, message); 128 | 129 | return this; 130 | } 131 | 132 | public ValidationContract IsLowerOrEqualsThan(int val, double comparer, string property, string message) 133 | { 134 | if (val > comparer) 135 | AddNotification(property, message); 136 | 137 | return this; 138 | } 139 | #endregion 140 | 141 | #region AreEquals 142 | public ValidationContract AreEquals(decimal val, double comparer, string property, string message) 143 | { 144 | if ((double)val != comparer) 145 | AddNotification(property, message); 146 | 147 | return this; 148 | } 149 | 150 | public ValidationContract AreEquals(double val, double comparer, string property, string message) 151 | { 152 | if (val != comparer) 153 | AddNotification(property, message); 154 | 155 | return this; 156 | } 157 | 158 | public ValidationContract AreEquals(float val, double comparer, string property, string message) 159 | { 160 | if (val != comparer) 161 | AddNotification(property, message); 162 | 163 | return this; 164 | } 165 | 166 | public ValidationContract AreEquals(int val, double comparer, string property, string message) 167 | { 168 | if (val != comparer) 169 | AddNotification(property, message); 170 | 171 | return this; 172 | } 173 | #endregion 174 | 175 | #region AreNotEquals 176 | public ValidationContract AreNotEquals(decimal val, double comparer, string property, string message) 177 | { 178 | if ((double)val == comparer) 179 | AddNotification(property, message); 180 | 181 | return this; 182 | } 183 | 184 | public ValidationContract AreNotEquals(double val, double comparer, string property, string message) 185 | { 186 | if (val == comparer) 187 | AddNotification(property, message); 188 | 189 | return this; 190 | } 191 | 192 | public ValidationContract AreNotEquals(float val, double comparer, string property, string message) 193 | { 194 | if (val == comparer) 195 | AddNotification(property, message); 196 | 197 | return this; 198 | } 199 | 200 | public ValidationContract AreNotEquals(int val, double comparer, string property, string message) 201 | { 202 | if (val == comparer) 203 | AddNotification(property, message); 204 | 205 | return this; 206 | } 207 | #endregion 208 | 209 | #region Between 210 | public ValidationContract IsBetween(double val, double from, double to, string property, string message) 211 | { 212 | if (!(val > from && val < to)) 213 | AddNotification(property, message); 214 | 215 | return this; 216 | } 217 | #endregion 218 | } 219 | } 220 | -------------------------------------------------------------------------------- /FluentValidator/Validation/FloatValidationContract.cs: -------------------------------------------------------------------------------- 1 | namespace FluentValidator.Validation 2 | { 3 | public partial class ValidationContract 4 | { 5 | #region IsGreaterThan 6 | public ValidationContract IsGreaterThan(decimal val, float comparer, string property, string message) 7 | { 8 | if ((double)val <= comparer) 9 | AddNotification(property, message); 10 | 11 | return this; 12 | } 13 | 14 | public ValidationContract IsGreaterThan(double val, float comparer, string property, string message) 15 | { 16 | if (val <= comparer) 17 | AddNotification(property, message); 18 | 19 | return this; 20 | } 21 | 22 | public ValidationContract IsGreaterThan(float val, float comparer, string property, string message) 23 | { 24 | if (val <= comparer) 25 | AddNotification(property, message); 26 | 27 | return this; 28 | } 29 | 30 | public ValidationContract IsGreaterThan(int val, float comparer, string property, string message) 31 | { 32 | if (val <= comparer) 33 | AddNotification(property, message); 34 | 35 | return this; 36 | } 37 | #endregion 38 | 39 | #region IsGreaterOrEqualsThan 40 | public ValidationContract IsGreaterOrEqualsThan(decimal val, float comparer, string property, string message) 41 | { 42 | if ((double)val < comparer) 43 | AddNotification(property, message); 44 | 45 | return this; 46 | } 47 | 48 | public ValidationContract IsGreaterOrEqualsThan(double val, float comparer, string property, string message) 49 | { 50 | if (val < comparer) 51 | AddNotification(property, message); 52 | 53 | return this; 54 | } 55 | 56 | public ValidationContract IsGreaterOrEqualsThan(float val, float comparer, string property, string message) 57 | { 58 | if (val < comparer) 59 | AddNotification(property, message); 60 | 61 | return this; 62 | } 63 | 64 | public ValidationContract IsGreaterOrEqualsThan(int val, float comparer, string property, string message) 65 | { 66 | if (val < comparer) 67 | AddNotification(property, message); 68 | 69 | return this; 70 | } 71 | #endregion 72 | 73 | #region IsLowerThan 74 | public ValidationContract IsLowerThan(decimal val, float comparer, string property, string message) 75 | { 76 | if ((double)val >= comparer) 77 | AddNotification(property, message); 78 | 79 | return this; 80 | } 81 | 82 | public ValidationContract IsLowerThan(double val, float comparer, string property, string message) 83 | { 84 | if (val >= comparer) 85 | AddNotification(property, message); 86 | 87 | return this; 88 | } 89 | 90 | public ValidationContract IsLowerThan(float val, float comparer, string property, string message) 91 | { 92 | if (val >= comparer) 93 | AddNotification(property, message); 94 | 95 | return this; 96 | } 97 | 98 | public ValidationContract IsLowerThan(int val, float comparer, string property, string message) 99 | { 100 | if (val >= comparer) 101 | AddNotification(property, message); 102 | 103 | return this; 104 | } 105 | #endregion 106 | 107 | #region IsLowerOrEqualsThan 108 | public ValidationContract IsLowerOrEqualsThan(decimal val, float comparer, string property, string message) 109 | { 110 | if ((double)val > comparer) 111 | AddNotification(property, message); 112 | 113 | return this; 114 | } 115 | 116 | public ValidationContract IsLowerOrEqualsThan(double val, float comparer, string property, string message) 117 | { 118 | if (val > comparer) 119 | AddNotification(property, message); 120 | 121 | return this; 122 | } 123 | 124 | public ValidationContract IsLowerOrEqualsThan(float val, float comparer, string property, string message) 125 | { 126 | if (val > comparer) 127 | AddNotification(property, message); 128 | 129 | return this; 130 | } 131 | 132 | public ValidationContract IsLowerOrEqualsThan(int val, float comparer, string property, string message) 133 | { 134 | if (val > comparer) 135 | AddNotification(property, message); 136 | 137 | return this; 138 | } 139 | #endregion 140 | 141 | #region AreEquals 142 | public ValidationContract AreEquals(decimal val, float comparer, string property, string message) 143 | { 144 | if ((double)val != comparer) 145 | AddNotification(property, message); 146 | 147 | return this; 148 | } 149 | 150 | public ValidationContract AreEquals(double val, float comparer, string property, string message) 151 | { 152 | if (val != comparer) 153 | AddNotification(property, message); 154 | 155 | return this; 156 | } 157 | 158 | public ValidationContract AreEquals(float val, float comparer, string property, string message) 159 | { 160 | if (val != comparer) 161 | AddNotification(property, message); 162 | 163 | return this; 164 | } 165 | 166 | public ValidationContract AreEquals(int val, float comparer, string property, string message) 167 | { 168 | if (val != comparer) 169 | AddNotification(property, message); 170 | 171 | return this; 172 | } 173 | #endregion 174 | 175 | #region AreNotEquals 176 | public ValidationContract AreNotEquals(decimal val, float comparer, string property, string message) 177 | { 178 | if ((double)val == comparer) 179 | AddNotification(property, message); 180 | 181 | return this; 182 | } 183 | 184 | public ValidationContract AreNotEquals(double val, float comparer, string property, string message) 185 | { 186 | if (val == comparer) 187 | AddNotification(property, message); 188 | 189 | return this; 190 | } 191 | 192 | public ValidationContract AreNotEquals(float val, float comparer, string property, string message) 193 | { 194 | if (val == comparer) 195 | AddNotification(property, message); 196 | 197 | return this; 198 | } 199 | 200 | public ValidationContract AreNotEquals(int val, float comparer, string property, string message) 201 | { 202 | if (val == comparer) 203 | AddNotification(property, message); 204 | 205 | return this; 206 | } 207 | #endregion 208 | 209 | #region Between 210 | public ValidationContract IsBetween(float val, float from, float to, string property, string message) 211 | { 212 | if (!(val > from && val < to)) 213 | AddNotification(property, message); 214 | 215 | return this; 216 | } 217 | #endregion 218 | } 219 | } 220 | -------------------------------------------------------------------------------- /FluentValidator/Validation/GuidValidationContract.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace FluentValidator.Validation 4 | { 5 | public partial class ValidationContract 6 | { 7 | public ValidationContract AreEquals(Guid val, Guid comparer, string property, string message) 8 | { 9 | // TODO: StringComparison.OrdinalIgnoreCase not suported yet 10 | if (val.ToString() != comparer.ToString()) 11 | AddNotification(property, message); 12 | 13 | return this; 14 | } 15 | 16 | public ValidationContract AreNotEquals(Guid val, Guid comparer, string property, string message) 17 | { 18 | // TODO: StringComparison.OrdinalIgnoreCase not suported yet 19 | if (val.ToString() == comparer.ToString()) 20 | AddNotification(property, message); 21 | 22 | return this; 23 | } 24 | } 25 | } -------------------------------------------------------------------------------- /FluentValidator/Validation/IntValidationContract.cs: -------------------------------------------------------------------------------- 1 | namespace FluentValidator.Validation 2 | { 3 | public partial class ValidationContract 4 | { 5 | #region IsGreaterThan 6 | public ValidationContract IsGreaterThan(decimal val, int comparer, string property, string message) 7 | { 8 | if ((double)val <= comparer) 9 | AddNotification(property, message); 10 | 11 | return this; 12 | } 13 | 14 | public ValidationContract IsGreaterThan(double val, int comparer, string property, string message) 15 | { 16 | if (val <= comparer) 17 | AddNotification(property, message); 18 | 19 | return this; 20 | } 21 | 22 | public ValidationContract IsGreaterThan(float val, int comparer, string property, string message) 23 | { 24 | if (val <= comparer) 25 | AddNotification(property, message); 26 | 27 | return this; 28 | } 29 | 30 | public ValidationContract IsGreaterThan(int val, int comparer, string property, string message) 31 | { 32 | if (val <= comparer) 33 | AddNotification(property, message); 34 | 35 | return this; 36 | } 37 | #endregion 38 | 39 | #region IsGreaterOrEqualsThan 40 | public ValidationContract IsGreaterOrEqualsThan(decimal val, int comparer, string property, string message) 41 | { 42 | if ((double)val < comparer) 43 | AddNotification(property, message); 44 | 45 | return this; 46 | } 47 | 48 | public ValidationContract IsGreaterOrEqualsThan(double val, int comparer, string property, string message) 49 | { 50 | if (val < comparer) 51 | AddNotification(property, message); 52 | 53 | return this; 54 | } 55 | 56 | public ValidationContract IsGreaterOrEqualsThan(float val, int comparer, string property, string message) 57 | { 58 | if (val < comparer) 59 | AddNotification(property, message); 60 | 61 | return this; 62 | } 63 | 64 | public ValidationContract IsGreaterOrEqualsThan(int val, int comparer, string property, string message) 65 | { 66 | if (val < comparer) 67 | AddNotification(property, message); 68 | 69 | return this; 70 | } 71 | #endregion 72 | 73 | #region IsLowerThan 74 | public ValidationContract IsLowerThan(decimal val, int comparer, string property, string message) 75 | { 76 | if ((double)val >= comparer) 77 | AddNotification(property, message); 78 | 79 | return this; 80 | } 81 | 82 | public ValidationContract IsLowerThan(double val, int comparer, string property, string message) 83 | { 84 | if (val >= comparer) 85 | AddNotification(property, message); 86 | 87 | return this; 88 | } 89 | 90 | public ValidationContract IsLowerThan(float val, int comparer, string property, string message) 91 | { 92 | if (val >= comparer) 93 | AddNotification(property, message); 94 | 95 | return this; 96 | } 97 | 98 | public ValidationContract IsLowerThan(int val, int comparer, string property, string message) 99 | { 100 | if (val >= comparer) 101 | AddNotification(property, message); 102 | 103 | return this; 104 | } 105 | #endregion 106 | 107 | #region IsLowerOrEqualsThan 108 | public ValidationContract IsLowerOrEqualsThan(decimal val, int comparer, string property, string message) 109 | { 110 | if ((double)val > comparer) 111 | AddNotification(property, message); 112 | 113 | return this; 114 | } 115 | 116 | public ValidationContract IsLowerOrEqualsThan(double val, int comparer, string property, string message) 117 | { 118 | if (val > comparer) 119 | AddNotification(property, message); 120 | 121 | return this; 122 | } 123 | 124 | public ValidationContract IsLowerOrEqualsThan(float val, int comparer, string property, string message) 125 | { 126 | if (val > comparer) 127 | AddNotification(property, message); 128 | 129 | return this; 130 | } 131 | 132 | public ValidationContract IsLowerOrEqualsThan(int val, int comparer, string property, string message) 133 | { 134 | if (val > comparer) 135 | AddNotification(property, message); 136 | 137 | return this; 138 | } 139 | #endregion 140 | 141 | #region AreEquals 142 | public ValidationContract AreEquals(decimal val, int comparer, string property, string message) 143 | { 144 | if ((double)val != comparer) 145 | AddNotification(property, message); 146 | 147 | return this; 148 | } 149 | 150 | public ValidationContract AreEquals(double val, int comparer, string property, string message) 151 | { 152 | if (val != comparer) 153 | AddNotification(property, message); 154 | 155 | return this; 156 | } 157 | 158 | public ValidationContract AreEquals(float val, int comparer, string property, string message) 159 | { 160 | if (val != comparer) 161 | AddNotification(property, message); 162 | 163 | return this; 164 | } 165 | 166 | public ValidationContract AreEquals(int val, int comparer, string property, string message) 167 | { 168 | if (val != comparer) 169 | AddNotification(property, message); 170 | 171 | return this; 172 | } 173 | #endregion 174 | 175 | #region AreNotEquals 176 | public ValidationContract AreNotEquals(decimal val, int comparer, string property, string message) 177 | { 178 | if ((double)val == comparer) 179 | AddNotification(property, message); 180 | 181 | return this; 182 | } 183 | 184 | public ValidationContract AreNotEquals(double val, int comparer, string property, string message) 185 | { 186 | if (val == comparer) 187 | AddNotification(property, message); 188 | 189 | return this; 190 | } 191 | 192 | public ValidationContract AreNotEquals(float val, int comparer, string property, string message) 193 | { 194 | if (val == comparer) 195 | AddNotification(property, message); 196 | 197 | return this; 198 | } 199 | 200 | public ValidationContract AreNotEquals(int val, int comparer, string property, string message) 201 | { 202 | if (val == comparer) 203 | AddNotification(property, message); 204 | 205 | return this; 206 | } 207 | #endregion 208 | 209 | #region Between 210 | public ValidationContract IsBetween(int val, int from, int to, string property, string message) 211 | { 212 | if (!(val > from && val < to)) 213 | AddNotification(property, message); 214 | 215 | return this; 216 | } 217 | #endregion 218 | } 219 | } 220 | -------------------------------------------------------------------------------- /FluentValidator/Validation/ObjectValidationContract.cs: -------------------------------------------------------------------------------- 1 | namespace FluentValidator.Validation 2 | { 3 | public partial class ValidationContract 4 | { 5 | public ValidationContract IsNull(object obj, string property, string message) 6 | { 7 | if (obj != null) 8 | AddNotification(property, message); 9 | 10 | return this; 11 | } 12 | 13 | public ValidationContract IsNotNull(object obj, string property, string message) 14 | { 15 | if (obj == null) 16 | AddNotification(property, message); 17 | 18 | return this; 19 | } 20 | 21 | public ValidationContract AreEquals(object obj, object comparer, string property, string message) 22 | { 23 | if (obj != comparer) 24 | AddNotification(property, message); 25 | 26 | return this; 27 | } 28 | 29 | public ValidationContract AreNotEquals(object obj, object comparer, string property, string message) 30 | { 31 | if (obj == comparer) 32 | AddNotification(property, message); 33 | 34 | return this; 35 | } 36 | } 37 | } -------------------------------------------------------------------------------- /FluentValidator/Validation/StringValidationContract.cs: -------------------------------------------------------------------------------- 1 | using System.Text.RegularExpressions; 2 | 3 | namespace FluentValidator.Validation 4 | { 5 | public partial class ValidationContract 6 | { 7 | public ValidationContract IsNotNullOrEmpty(string val, string property, string message) 8 | { 9 | if (string.IsNullOrEmpty(val)) 10 | AddNotification(property, message); 11 | 12 | return this; 13 | } 14 | 15 | public ValidationContract IsNullOrEmpty(string val, string property, string message) 16 | { 17 | if (!string.IsNullOrEmpty(val)) 18 | AddNotification(property, message); 19 | 20 | return this; 21 | } 22 | 23 | public ValidationContract IsNotNullOrWhiteSpace(string val, string property, string message) 24 | { 25 | if (string.IsNullOrWhiteSpace(val)) 26 | AddNotification(property, message); 27 | 28 | return this; 29 | } 30 | 31 | public ValidationContract IsNullOrWhiteSpace(string val, string property, string message) 32 | { 33 | if (!string.IsNullOrWhiteSpace(val)) 34 | AddNotification(property, message); 35 | 36 | return this; 37 | } 38 | 39 | public ValidationContract HasMinLen(string val, int min, string property, string message) 40 | { 41 | if ((val ?? "").Length < min) 42 | AddNotification(property, message); 43 | 44 | return this; 45 | } 46 | 47 | public ValidationContract HasMaxLen(string val, int max, string property, string message) 48 | { 49 | if ((val ?? "").Length > max) 50 | AddNotification(property, message); 51 | 52 | return this; 53 | } 54 | 55 | public ValidationContract HasLen(string val, int len, string property, string message) 56 | { 57 | if ((val ?? "").Length != len) 58 | AddNotification(property, message); 59 | 60 | return this; 61 | } 62 | 63 | public ValidationContract Contains(string val, string text, string property, string message) 64 | { 65 | // TODO: StringComparison.OrdinalIgnoreCase not suported yet 66 | if (!(val ?? "").Contains(text)) 67 | AddNotification(property, message); 68 | 69 | return this; 70 | } 71 | 72 | public ValidationContract AreEquals(string val, string text, string property, string message) 73 | { 74 | // TODO: StringComparison.OrdinalIgnoreCase not suported yet 75 | if (val != text) 76 | AddNotification(property, message); 77 | 78 | return this; 79 | } 80 | 81 | public ValidationContract AreNotEquals(string val, string text, string property, string message) 82 | { 83 | // TODO: StringComparison.OrdinalIgnoreCase not suported yet 84 | if (val == text) 85 | AddNotification(property, message); 86 | 87 | return this; 88 | } 89 | 90 | public ValidationContract IsEmail(string email, string property, string message) 91 | { 92 | const string pattern = @"^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$"; 93 | return Matchs(email, pattern, property, message); 94 | } 95 | 96 | public ValidationContract IsEmailOrEmpty(string email, string property, string message) 97 | { 98 | if (string.IsNullOrEmpty(email)) 99 | return this; 100 | 101 | return IsEmail(email, property, message); 102 | } 103 | 104 | public ValidationContract IsUrl(string url, string property, string message) 105 | { 106 | const string pattern = @"^(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$"; 107 | return Matchs(url, pattern, property, message); 108 | } 109 | 110 | public ValidationContract IsUrlOrEmpty(string url, string property, string message) 111 | { 112 | if (string.IsNullOrEmpty(url)) 113 | return this; 114 | 115 | return IsUrl(url, property, message); 116 | } 117 | 118 | public ValidationContract Matchs(string text, string pattern, string property, string message) 119 | { 120 | if (!Regex.IsMatch(text ?? "", pattern)) 121 | AddNotification(property, message); 122 | 123 | return this; 124 | } 125 | } 126 | } 127 | -------------------------------------------------------------------------------- /FluentValidator/Validation/ValidationContract.cs: -------------------------------------------------------------------------------- 1 | namespace FluentValidator.Validation 2 | { 3 | public partial class ValidationContract : Notifiable 4 | { 5 | public ValidationContract Requires() 6 | { 7 | return this; 8 | } 9 | 10 | public ValidationContract Concat(Notifiable notifiable) 11 | { 12 | if (notifiable.Invalid) 13 | AddNotifications(notifiable.Notifications); 14 | 15 | return this; 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # FluentValidator 2 | Fluent Validator is a fluent way to use Notification Pattern with your entities 3 | 4 | ## Dependencies 5 | .NET Standard 1.3+ 6 | 7 | You can check supported frameworks here: 8 | 9 | https://docs.microsoft.com/pt-br/dotnet/standard/net-standard 10 | 11 | ## Instalation 12 | This package is available through Nuget Packages: https://www.nuget.org/packages/FluentValidator 13 | ``` 14 | Install-Package FluentValidator 15 | ``` 16 | 17 | ## Older Versions 18 | We are moving to a new structure, with new namespaces, if you need to use our old version, try this: 19 | ``` 20 | Install-Package FluentValidator -Version 1.0.5 21 | ``` 22 | 1.0.5 was the latest stable version 23 | 24 | ## Signifcant Changes 25 | We removed reflection to increase performance, changed namespace and folders and externalize contracts from entities, so take a close look on our Wiki to learn how to use it. 26 | 27 | # How to Use 28 | Just check our Wiki 29 | 30 | https://github.com/andrebaltieri/FluentValidator/wiki 31 | --------------------------------------------------------------------------------