-
-
Notifications
You must be signed in to change notification settings - Fork 2
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
1 parent
4a329a3
commit 3c43b40
Showing
5 changed files
with
101 additions
and
13 deletions.
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
src/Immediate.Validations.Shared/Validators/LengthAttribute.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,35 @@ | ||
using System.Diagnostics.CodeAnalysis; | ||
|
||
namespace Immediate.Validations.Shared; | ||
|
||
/// <summary> | ||
/// Applied to a <see cref="string"/> property to indicate that the value should have a length of exactly <paramref | ||
/// name="length"/>. | ||
/// </summary> | ||
/// <param name="length"> | ||
/// The maximum length of the <see cref="string"/>. | ||
/// </param> | ||
public sealed class LengthAttribute( | ||
[TargetType] | ||
object length | ||
) : ValidatorAttribute | ||
{ | ||
/// <summary> | ||
/// Validates that the <see cref="string"/> has a length of exactly <paramref name="length"/>. | ||
/// </summary> | ||
/// <param name="target"> | ||
/// The value to validate. | ||
/// </param> | ||
/// <param name="length"> | ||
/// The valid length for the string <paramref name="target"/>. | ||
/// </param> | ||
/// <returns> | ||
/// A <see cref="ValueTuple{T1, T2}"/> indicating whether the property is valid or not, along with an error | ||
/// message if the property is not valid. | ||
/// </returns> | ||
[SuppressMessage("Design", "CA1062:Validate arguments of public methods", Justification = "Will already by validated by IV first.")] | ||
public static (bool Invalid, string? Message) ValidateProperty(string target, int length) => | ||
target.Length == length | ||
? default | ||
: (true, $"String is of length '{target.Length}', which is {(target.Length < length ? "shorter" : "longer")} than the allowed length of '{length}'."); | ||
} |
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
62 changes: 62 additions & 0 deletions
62
tests/Immediate.Validations.FunctionalTests/Validators/LengthTests.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,62 @@ | ||
using Immediate.Validations.Shared; | ||
using Xunit; | ||
|
||
namespace Immediate.Validations.FunctionalTests.Validators; | ||
|
||
public sealed partial class LengthTests | ||
{ | ||
[Validate] | ||
public partial record StringRecord : IValidationTarget<StringRecord> | ||
{ | ||
[Length(12)] | ||
public required string StringValue { get; init; } | ||
} | ||
|
||
[Fact] | ||
public void LengthWhenShort() | ||
{ | ||
var instance = new StringRecord { StringValue = "Hello" }; | ||
|
||
var errors = StringRecord.Validate(instance); | ||
|
||
Assert.Equal( | ||
[ | ||
new() | ||
{ | ||
PropertyName = nameof(StringRecord.StringValue), | ||
ErrorMessage = "String is of length '5', which is shorter than the allowed length of '12'.", | ||
} | ||
], | ||
errors | ||
); | ||
} | ||
|
||
[Fact] | ||
public void LengthWhenEqual() | ||
{ | ||
var instance = new StringRecord { StringValue = "Hello World!" }; | ||
|
||
var errors = StringRecord.Validate(instance); | ||
|
||
Assert.Empty(errors); | ||
} | ||
|
||
[Fact] | ||
public void LengthWhenLong() | ||
{ | ||
var instance = new StringRecord { StringValue = "Hello World! Hello World! Hello World!" }; | ||
|
||
var errors = StringRecord.Validate(instance); | ||
|
||
Assert.Equal( | ||
[ | ||
new() | ||
{ | ||
PropertyName = nameof(StringRecord.StringValue), | ||
ErrorMessage = "String is of length '38', which is longer than the allowed length of '12'.", | ||
} | ||
], | ||
errors | ||
); | ||
} | ||
} |