Skip to content

Commit 311ea36

Browse files
authored
Merged develop into master
develop into master
2 parents 15da546 + 68e2a4d commit 311ea36

6 files changed

Lines changed: 398 additions & 8 deletions

File tree

README.md

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,35 @@
22

33
Valit is **dead simple** validation for .NET Core. No more if-statements all around your code. Write nice and clean **fluent validators** instead!
44

5-
[![Build status](https://ci.appveyor.com/api/projects/status/github/valit-stack/Valit?branch=master&svg=true&passingText=master%20passing&failingText=master%20failing&pendingText=master%20pending)](https://ci.appveyor.com/project/GooRiOn/valit/branch/master)
6-
[![codecov](https://codecov.io/gh/valit-stack/valit/branch/master/graph/badge.svg)](https://codecov.io/gh/valit-stack/valit/branch/master)
5+
| | master | develop |
6+
|---|--------|----------|
7+
|AppVeyor|[![Build status](https://ci.appveyor.com/api/projects/status/github/valit-stack/Valit?branch=master&svg=true&passingText=master%20passing&failingText=master%20failing&pendingText=master%20pending)](https://ci.appveyor.com/project/GooRiOn/valit/branch/master)|[![Build status](https://ci.appveyor.com/api/projects/status/github/valit-stack/Valit?branch=develop&svg=true&passingText=develop%20passing&failingText=develop%20failing&pendingText=develop%20pending)](https://ci.appveyor.com/project/GooRiOn/valit/branch/develop)|
8+
|Codecov|[![codecov](https://codecov.io/gh/valit-stack/valit/branch/master/graph/badge.svg)](https://codecov.io/gh/valit-stack/valit/branch/master)|[![codecov](https://codecov.io/gh/valit-stack/valit/branch/develop/graph/badge.svg)](https://codecov.io/gh/valit-stack/valit/branch/develop)|
79

8-
[![Build status](https://ci.appveyor.com/api/projects/status/github/valit-stack/Valit?branch=develop&svg=true&passingText=develop%20passing&failingText=develop%20failing&pendingText=develop%20pending)](https://ci.appveyor.com/project/GooRiOn/valit/branch/develop)
9-
[![codecov](https://codecov.io/gh/valit-stack/valit/branch/develop/graph/badge.svg)](https://codecov.io/gh/valit-stack/valit/branch/develop)
10+
![NuGet](https://img.shields.io/nuget/v/Valit.svg)
11+
12+
13+
# Installation
14+
Valit is available on [NuGet](https://www.nuget.org/packages/Valit/).
15+
16+
### Package manager
17+
```bash
18+
Install-Package Valit -Version 0.1.0
19+
```
20+
21+
### .NET CLI
22+
```bash
23+
dotnet add package Valit --version 0.1.0
24+
```
1025

1126
# Getting started
1227
In order to create a validator you need to go through few steps. It's worth mentioning that not all of them are mandatory. The steps are:
1328

14-
- creating new instance of validator using ``Create()`` static method
15-
- choosing [validation strategy](http://valitdocs.readthedocs.io/en/latest/strategies/index.html) using ``WithStrategy()`` method **(not required)**
29+
- creating new instance of validator using ``Create()`` static method.
30+
- choosing [validation strategy](http://valitdocs.readthedocs.io/en/latest/strategies/index.html) using ``WithStrategy()`` method **(not required)**.
1631
- selecting property using ``Ensure()`` method and defining rules for it.
1732
- Extending rules with [custom errors](http://valitdocs.readthedocs.io/en/latest/validation-errors/index.html) (such as messages or error codes), [tags and conditions](http://valitdocs.readthedocs.io/en/latest/validation-rules/index.html). **(not required)**.
18-
- applying created rules to an object using ``For()`` method
33+
- applying created rules to an object using ``For()`` method.
1934

2035
Having the validator created, simply invoke ``Validate()`` method which will produce the result with all the data.
2136

@@ -37,7 +52,8 @@ These are the validation criteria:
3752
- ``Password`` is required and needs to be at least 10 characters long
3853
- ``Age`` must be greater than 16
3954

40-
This is how Valit handles such scenario:
55+
56+
This is how you can handle such scenario using Valit:
4157

4258
```cs
4359

src/Valit/Valit.csproj

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4+
<Title>Valit</Title>
5+
<AssemblyName>Valit</AssemblyName>
6+
<PackageId>Valit</PackageId>
7+
<PackageTags>valit;validation;fluentapi</PackageTags>
8+
<Description>Valit is dead simple validation for .NET Core. No more if-statements all around your code. Write nice and clean fluent validators instead!</Description>
9+
<Authors>Dariusz Pawlukiewicz, Daniel Barwikowski, Arkadiusz Bal</Authors>
10+
<PackageProjectUrl>https://github.com/valit-stack/Valit</PackageProjectUrl>
11+
<PackageLicenseUrl>https://github.com/valit-stack/Valit/blob/master/LICENSE</PackageLicenseUrl>
12+
<PackageIconUrl>https://user-images.githubusercontent.com/7096476/32700023-298ffb9e-c7bf-11e7-9d83-82690b7e260b.png</PackageIconUrl>
13+
<VersionPrefix>0.1.0</VersionPrefix>
414
<TargetFramework>netcoreapp2.0</TargetFramework>
515
</PropertyGroup>
616

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System;
2+
3+
namespace Valit
4+
{
5+
public static class ValitRuleGuidExtensions
6+
{
7+
public static IValitRule<TObject, Guid> IsEqualTo<TObject>(this IValitRule<TObject, Guid> rule, Guid guid) where TObject : class
8+
=> rule.Satisfies(p => p == guid);
9+
10+
public static IValitRule<TObject, Guid> IsEqualTo<TObject>(this IValitRule<TObject, Guid> rule, Guid? guid) where TObject : class
11+
=> rule.Satisfies(p => guid.HasValue && p == guid.Value);
12+
13+
public static IValitRule<TObject, Guid?> IsEqualTo<TObject>(this IValitRule<TObject, Guid?> rule, Guid? guid) where TObject : class
14+
=> rule.Satisfies(p => p.HasValue && guid.HasValue && p.Value == guid.Value);
15+
16+
public static IValitRule<TObject, Guid?> IsEqualTo<TObject>(this IValitRule<TObject, Guid?> rule, Guid guid) where TObject : class
17+
=> rule.Satisfies(p => p.HasValue && p.Value == guid);
18+
19+
public static IValitRule<TObject, Guid?> Required<TObject>(this IValitRule<TObject, Guid?> rule) where TObject : class
20+
=> rule.Satisfies(p => p.HasValue);
21+
22+
public static IValitRule<TObject, Guid> IsNotEmpty<TObject>(this IValitRule<TObject, Guid> rule) where TObject : class
23+
=> rule.Satisfies(p => p != Guid.Empty);
24+
25+
public static IValitRule<TObject, Guid?> IsNotEmpty<TObject>(this IValitRule<TObject, Guid?> rule) where TObject : class
26+
=> rule.Satisfies(p => p.HasValue && p.Value != Guid.Empty);
27+
}
28+
}
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
using Xunit;
2+
using Shouldly;
3+
using System;
4+
5+
namespace Valit.Tests.Guid_
6+
{
7+
public class Guid_IsEqualTo_Tests
8+
{
9+
[Fact]
10+
public void Guid_IsEqualTo_For_Not_Nullable_Values_Throws_When_Null_Rule_Is_Given()
11+
{
12+
var exception = Record.Exception(() => {
13+
((IValitRule<Model, Guid>)null)
14+
.IsEqualTo(Guid.NewGuid());
15+
});
16+
17+
exception.ShouldBeOfType(typeof(ValitException));
18+
}
19+
20+
[Fact]
21+
public void Guid_IsEqualTo_For_Not_Nullable_Value_And_Nullable_Value_Throws_When_Null_Rule_Is_Given()
22+
{
23+
var exception = Record.Exception(() => {
24+
((IValitRule<Model, Guid>)null)
25+
.IsEqualTo((Guid?)Guid.NewGuid());
26+
});
27+
28+
exception.ShouldBeOfType(typeof(ValitException));
29+
}
30+
31+
[Fact]
32+
public void Guid_IsEqualTo_For_Nullable_Value_And_Not_Nullable_Value_Throws_When_Null_Rule_Is_Given()
33+
{
34+
var exception = Record.Exception(() => {
35+
((IValitRule<Model, Guid?>)null)
36+
.IsEqualTo(Guid.NewGuid());
37+
});
38+
39+
exception.ShouldBeOfType(typeof(ValitException));
40+
}
41+
42+
[Fact]
43+
public void Guid_IsEqualTo_For_Nullable_Values_Throws_When_Null_Rule_Is_Given()
44+
{
45+
var exception = Record.Exception(() => {
46+
((IValitRule<Model, Guid?>)null)
47+
.IsEqualTo((Guid?)Guid.NewGuid());
48+
});
49+
50+
exception.ShouldBeOfType(typeof(ValitException));
51+
}
52+
53+
[Theory]
54+
[InlineData("6f72f4ab-c2fc-4fca-b70e-b5ac7eccd054", true)]
55+
[InlineData("7dca6d8e-255c-401c-b701-f18af9b2e191", false)]
56+
public void Guid_IsEqualTo_Returns_Proper_Result_For_Left_Value(string strValue, bool expected)
57+
{
58+
Guid value = Guid.Parse(strValue);
59+
60+
IValitResult result = ValitRules<Model>
61+
.Create()
62+
.Ensure(m => m.Value, _ => _
63+
.IsEqualTo(value))
64+
.For(_model)
65+
.Validate();
66+
67+
result.Succeeded.ShouldBe(expected);
68+
}
69+
70+
[Theory]
71+
[InlineData("7dca6d8e-255c-401c-b701-f18af9b2e191", false)]
72+
[InlineData("6f72f4ab-c2fc-4fca-b70e-b5ac7eccd054", true)]
73+
[InlineData("5c7ad720-6813-4c45-89c9-c4484bbbc34d", false)]
74+
public void Guid_IsEqualTo_Returns_Proper_Result_For_Nullable_Left_Value(string strValue, bool expected)
75+
{
76+
Guid value = Guid.Parse(strValue);
77+
78+
var result = ValitRules<Model>
79+
.Create()
80+
.Ensure(m => m.NullableValue, _ => _
81+
.IsEqualTo(value))
82+
.For(_model)
83+
.Validate();
84+
85+
result.Succeeded.ShouldBe(expected);
86+
}
87+
88+
[Theory]
89+
[InlineData("7dca6d8e-255c-401c-b701-f18af9b2e191", false)]
90+
[InlineData("6f72f4ab-c2fc-4fca-b70e-b5ac7eccd054", false)]
91+
[InlineData("5c7ad720-6813-4c45-89c9-c4484bbbc34d", false)]
92+
public void Guid_IsEqualTo_Returns_Proper_Result_For_Null_Left_Value(string strValue, bool expected)
93+
{
94+
Guid value = Guid.Parse(strValue);
95+
96+
var result = ValitRules<Model>
97+
.Create()
98+
.Ensure(m => m.NullValue, _ => _
99+
.IsEqualTo(value))
100+
.For(_model)
101+
.Validate();
102+
103+
result.Succeeded.ShouldBe(expected);
104+
}
105+
106+
[Theory]
107+
[InlineData("7dca6d8e-255c-401c-b701-f18af9b2e191", false)]
108+
[InlineData("6f72f4ab-c2fc-4fca-b70e-b5ac7eccd054", true)]
109+
[InlineData("5c7ad720-6813-4c45-89c9-c4484bbbc34d", false)]
110+
[InlineData("", false, true)]
111+
public void Guid_IsEqualTo_Returns_Proper_Result_For_Nullable_Right_Value(string strValue, bool expected, bool useNull = false)
112+
{
113+
Guid? value = useNull ? (Guid?)null : Guid.Parse(strValue);
114+
115+
var result = ValitRules<Model>
116+
.Create()
117+
.Ensure(m => m.Value, _ => _
118+
.IsEqualTo(value))
119+
.For(_model)
120+
.Validate();
121+
122+
result.Succeeded.ShouldBe(expected);
123+
}
124+
125+
[Theory]
126+
[InlineData("7dca6d8e-255c-401c-b701-f18af9b2e191", false)]
127+
[InlineData("6f72f4ab-c2fc-4fca-b70e-b5ac7eccd054", true)]
128+
[InlineData("5c7ad720-6813-4c45-89c9-c4484bbbc34d", false)]
129+
[InlineData("", false, true)]
130+
public void Guid_IsEqualTo_Returns_Proper_Result_For_Nullable_Values(string strValue, bool expected, bool useNull = false)
131+
{
132+
Guid? value = useNull ? (Guid?)null : Guid.Parse(strValue);
133+
134+
var result = ValitRules<Model>
135+
.Create()
136+
.Ensure(m => m.NullableValue, _ => _
137+
.IsEqualTo(value))
138+
.For(_model)
139+
.Validate();
140+
141+
result.Succeeded.ShouldBe(expected);
142+
}
143+
144+
#region ARRANGE
145+
public Guid_IsEqualTo_Tests()
146+
{
147+
_model = new Model();
148+
149+
}
150+
151+
private readonly Model _model;
152+
153+
class Model
154+
{
155+
public Guid? NullableValue => System.Guid.Parse("6f72f4ab-c2fc-4fca-b70e-b5ac7eccd054");
156+
public Guid Value => System.Guid.Parse("6f72f4ab-c2fc-4fca-b70e-b5ac7eccd054");
157+
public Guid? NullValue => null;
158+
}
159+
#endregion
160+
}
161+
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
using System;
2+
using Shouldly;
3+
using Xunit;
4+
5+
namespace Valit.Tests.Guid_
6+
{
7+
public class Guid_IsNotEmpty_Tests
8+
{
9+
[Fact]
10+
public void Guid_IsNotEmpty_For_Not_Nullable_Values_Throws_When_Null_Rule_Is_Given()
11+
{
12+
var exception = Record.Exception(() => {
13+
((IValitRule<Model, Guid>)null)
14+
.IsNotEmpty();
15+
});
16+
17+
exception.ShouldBeOfType(typeof(ValitException));
18+
}
19+
20+
[Fact]
21+
public void Guid_IsNotEmpty_For_Nullable_Value_And_Not_Nullable_Value_Throws_When_Null_Rule_Is_Given()
22+
{
23+
var exception = Record.Exception(() => {
24+
((IValitRule<Model, Guid?>)null)
25+
.IsNotEmpty();
26+
});
27+
28+
exception.ShouldBeOfType(typeof(ValitException));
29+
}
30+
31+
[Fact]
32+
public void Guid_IsNotEmpty_Succeeds_For_Value()
33+
{
34+
var result = ValitRules<Model>
35+
.Create()
36+
.Ensure(m => m.Value, _ => _
37+
.IsNotEmpty())
38+
.For(_model)
39+
.Validate();
40+
41+
result.Succeeded.ShouldBe(true);
42+
}
43+
44+
[Fact]
45+
public void Guid_IsNotEmpty_Fails_For_Empty_Value()
46+
{
47+
var result = ValitRules<Model>
48+
.Create()
49+
.Ensure(m => m.Empty, _ => _
50+
.IsNotEmpty())
51+
.For(_model)
52+
.Validate();
53+
54+
result.Succeeded.ShouldBe(false);
55+
}
56+
57+
[Fact]
58+
public void Guid_IsNotEmpty_Succeeds_For_Nullable_Value()
59+
{
60+
var result = ValitRules<Model>
61+
.Create()
62+
.Ensure(m => m.Value, _ => _
63+
.IsNotEmpty())
64+
.For(_model)
65+
.Validate();
66+
67+
result.Succeeded.ShouldBe(true);
68+
}
69+
70+
[Fact]
71+
public void Guid_IsNotEmpty_Fails_For_Nullable_Empty_Value()
72+
{
73+
var result = ValitRules<Model>
74+
.Create()
75+
.Ensure(m => m.NullableEmpty, _ => _
76+
.IsNotEmpty())
77+
.For(_model)
78+
.Validate();
79+
80+
result.Succeeded.ShouldBe(false);
81+
}
82+
83+
[Fact]
84+
public void Guid_IsNotEmpty_Fails_For_Nullable_Null()
85+
{
86+
var result = ValitRules<Model>
87+
.Create()
88+
.Ensure(m => m.NullValue, _ => _
89+
.IsNotEmpty())
90+
.For(_model)
91+
.Validate();
92+
93+
result.Succeeded.ShouldBe(false);
94+
}
95+
96+
#region ARRANGE
97+
public Guid_IsNotEmpty_Tests()
98+
{
99+
_model = new Model();
100+
}
101+
102+
private readonly Model _model;
103+
104+
class Model
105+
{
106+
public Guid Value => System.Guid.Parse("6f72f4ab-c2fc-4fca-b70e-b5ac7eccd054");
107+
public Guid Empty => Guid.Empty;
108+
public Guid? NullableValue => System.Guid.Parse("6f72f4ab-c2fc-4fca-b70e-b5ac7eccd054");
109+
public Guid? NullableEmpty => Guid.Empty;
110+
public Guid? NullValue => null;
111+
}
112+
#endregion
113+
}
114+
}

0 commit comments

Comments
 (0)