generated from Avanade/avanade-template
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
316 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Copyright (c) Avanade. Licensed under the MIT License. See https://github.com/Avanade/CoreEx | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace CoreEx.Validation.Rules | ||
{ | ||
/// <summary> | ||
/// Provides validation to ensure the value is not specified (is none); determined as when it does not equal its default value. | ||
/// </summary> | ||
/// <typeparam name="TEntity">The entity <see cref="Type"/>.</typeparam> | ||
/// <typeparam name="TProperty">The property <see cref="Type"/>.</typeparam> | ||
/// <remarks>A value will be determined as none when it equals its default value. For example an <see cref="int"/> will trigger when the value is zero; however, a | ||
/// <see cref="Nullable{Int32}"/> will trigger when null only (a zero is considered a value in this instance).</remarks> | ||
public class NoneRule<TEntity, TProperty> : ValueRuleBase<TEntity, TProperty> where TEntity : class | ||
{ | ||
/// <inheritdoc/> | ||
protected override Task ValidateAsync(PropertyContext<TEntity, TProperty> context, CancellationToken cancellationToken = default) | ||
{ | ||
// Compare the value against its default. | ||
if (Comparer<TProperty?>.Default.Compare(context.Value, default!) != 0) | ||
{ | ||
CreateErrorMessage(context); | ||
return Task.CompletedTask; | ||
} | ||
|
||
return Task.CompletedTask; | ||
} | ||
|
||
/// <summary> | ||
/// Create the error message. | ||
/// </summary> | ||
private void CreateErrorMessage(PropertyContext<TEntity, TProperty> context) => context.CreateErrorMessage(ErrorText ?? ValidatorStrings.NoneFormat); | ||
} | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
tests/CoreEx.Test/Framework/Validation/Rules/NoneRuleTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
using NUnit.Framework; | ||
using CoreEx.Validation; | ||
using CoreEx.Entities; | ||
using System.Threading.Tasks; | ||
|
||
namespace CoreEx.Test.Framework.Validation.Rules | ||
{ | ||
[TestFixture] | ||
public class NoneRuleTest | ||
{ | ||
[OneTimeSetUp] | ||
public void OneTimeSetUp() => CoreEx.Localization.TextProvider.SetTextProvider(new ValidationTextProvider()); | ||
|
||
[Test] | ||
public async Task Validate_String() | ||
{ | ||
var v1 = await "XXX".Validate("value").None().ValidateAsync(); | ||
Assert.IsTrue(v1.HasErrors); | ||
Assert.AreEqual(1, v1.Messages!.Count); | ||
Assert.AreEqual("Value must not be specified.", v1.Messages[0].Text); | ||
Assert.AreEqual(MessageType.Error, v1.Messages[0].Type); | ||
Assert.AreEqual("value", v1.Messages[0].Property); | ||
|
||
v1 = await ((string?)null).Validate("value").None().ValidateAsync(); | ||
Assert.IsFalse(v1.HasErrors); | ||
|
||
v1 = await (string.Empty).Validate("value").None().ValidateAsync(); | ||
Assert.IsTrue(v1.HasErrors); | ||
} | ||
|
||
[Test] | ||
public async Task Validate_Int32() | ||
{ | ||
var v1 = await (123).Validate("value").None().ValidateAsync(); | ||
Assert.IsTrue(v1.HasErrors); | ||
Assert.AreEqual(1, v1.Messages!.Count); | ||
Assert.AreEqual("Value must not be specified.", v1.Messages[0].Text); | ||
Assert.AreEqual(MessageType.Error, v1.Messages[0].Type); | ||
Assert.AreEqual("value", v1.Messages[0].Property); | ||
|
||
v1 = await (0).Validate("value").None().ValidateAsync(); | ||
Assert.IsFalse(v1.HasErrors); | ||
|
||
var v2 = await ((int?)123).Validate("value").None().ValidateAsync(); | ||
Assert.IsTrue(v2.HasErrors); | ||
|
||
v2 = await ((int?)0).Validate("value").None().ValidateAsync(); | ||
Assert.IsTrue(v2.HasErrors); | ||
|
||
v2 = await ((int?)null).Validate("value").None().ValidateAsync(); | ||
Assert.IsFalse(v2.HasErrors); | ||
} | ||
|
||
public class Foo | ||
{ | ||
public string? Bar { get; set; } | ||
} | ||
|
||
[Test] | ||
public async Task Validate_Entity() | ||
{ | ||
Foo? foo = new Foo(); | ||
var v1 = await foo.Validate("value").None().ValidateAsync(); | ||
Assert.IsTrue(v1.HasErrors); | ||
Assert.AreEqual(1, v1.Messages!.Count); | ||
Assert.AreEqual("Value must not be specified.", v1.Messages[0].Text); | ||
Assert.AreEqual(MessageType.Error, v1.Messages[0].Type); | ||
Assert.AreEqual("value", v1.Messages[0].Property); | ||
|
||
foo = null; | ||
v1 = await foo.Validate("value").None().ValidateAsync(); | ||
Assert.IsFalse(v1.HasErrors); | ||
} | ||
} | ||
} |