Skip to content

Commit

Permalink
Merge pull request #6 from luboshl/feature/rename-validator
Browse files Browse the repository at this point in the history
Rename validator to MiniValidatorPlus
  • Loading branch information
luboshl authored Jul 16, 2024
2 parents 9953f03 + b7805e0 commit bc1f202
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 86 deletions.
2 changes: 1 addition & 1 deletion samples/Samples.Console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
var allValid = true;
foreach (var widget in widgets)
{
if (!MiniValidator.TryValidate(widget, out var errors))
if (!MiniValidatorPlus.TryValidate(widget, out var errors))
{
allValid = false;
Console.WriteLine($"{nameof(Widget)} has errors!");
Expand Down
4 changes: 2 additions & 2 deletions samples/Samples.Web/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@
new Widget { Name = name });

app.MapPost("/widgets", Results<ValidationProblem, Created<Widget>> (Widget widget) =>
!MiniValidator.TryValidate(widget, out var errors)
!MiniValidatorPlus.TryValidate(widget, out var errors)
? TypedResults.ValidationProblem(errors)
: TypedResults.Created($"/widgets/{widget.Name}", widget));

app.MapPost("/widgets/custom-validation", Results<ValidationProblem, Created<WidgetWithCustomValidation>> (WidgetWithCustomValidation widget) =>
!MiniValidator.TryValidate(widget, out var errors)
!MiniValidatorPlus.TryValidate(widget, out var errors)
? TypedResults.ValidationProblem(errors)
: TypedResults.Created($"/widgets/{widget.Name}", widget));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace MiniValidationPlus;
/// Contains methods and properties for performing validation operations with <see cref="Validator"/> on objects whos properties
/// are decorated with <see cref="ValidationAttribute"/>s.
/// </summary>
public static class MiniValidator
public static class MiniValidatorPlus
{
private static readonly TypeDetailsCache _typeDetailsCache = new();
private static readonly IDictionary<string, string[]> _emptyErrors = new ReadOnlyDictionary<string, string[]>(new Dictionary<string, string[]>());
Expand Down
2 changes: 1 addition & 1 deletion src/MiniValidationPlus/SkipRecursionAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace MiniValidationPlus;

/// <summary>
/// Indicates that a property should be ignored during recursive validation when using
/// <see cref="MiniValidator.TryValidate{TTarget}(TTarget, out System.Collections.Generic.IDictionary{string, string[]})"/>.
/// <see cref="MiniValidatorPlus.TryValidate{TTarget}(TTarget, out System.Collections.Generic.IDictionary{string, string[]})"/>.
/// Note that any validation attributes on the property itself will still be validated.
/// </summary>
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
Expand Down
12 changes: 6 additions & 6 deletions tests/MiniValidationPlus.Benchmarks/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,31 +22,31 @@ public void Initialize()
foreach (var type in types)
{
var target = Activator.CreateInstance(type);
MiniValidator.TryValidate(target, recurse: true, allowAsync: false, out var _);
MiniValidatorPlus.TryValidate(target, recurse: true, allowAsync: false, out var _);
}
}

[Benchmark(Baseline = true)]
public (bool, IDictionary<string, string[]>) NothingToValidate()
{
var target = new BenchmarkTypes.TodoWithNoValidation();
var isValid = MiniValidator.TryValidate(target, out var errors);
var isValid = MiniValidatorPlus.TryValidate(target, out var errors);
return (isValid, errors);
}

[Benchmark]
public (bool, IDictionary<string, string[]>) SinglePropertyToValidate_NoRecursion_Valid()
{
var target = new BenchmarkTypes.Todo { Title = "This is the title" };
var isValid = MiniValidator.TryValidate(target, recurse: false, allowAsync: false, out var errors);
var isValid = MiniValidatorPlus.TryValidate(target, recurse: false, allowAsync: false, out var errors);
return (isValid, errors);
}

[Benchmark]
public (bool, IDictionary<string, string[]>) SinglePropertyToValidate_NoRecursion_Invalid()
{
var target = new BenchmarkTypes.Todo { Title = "" };
var isValid = MiniValidator.TryValidate(target, recurse: false, allowAsync: false, out var errors);
var isValid = MiniValidatorPlus.TryValidate(target, recurse: false, allowAsync: false, out var errors);
return (isValid, errors);
}

Expand All @@ -55,7 +55,7 @@ public void Initialize()
{
var target = new BenchmarkTypes.Todo { Title = "This is the title" };
target.Tags.Add(new() { Name = "A tag" });
var isValid = MiniValidator.TryValidate(target, out var errors);
var isValid = MiniValidatorPlus.TryValidate(target, out var errors);
return (isValid, errors);
}

Expand All @@ -64,7 +64,7 @@ public void Initialize()
{
var target = new BenchmarkTypes.Todo { Title = "This is the title" };
target.Tags.Add(new() { Name = "" });
var isValid = MiniValidator.TryValidate(target, out var errors);
var isValid = MiniValidatorPlus.TryValidate(target, out var errors);
return (isValid, errors);
}
#pragma warning restore CA1822 // Mark members as static
Expand Down
66 changes: 33 additions & 33 deletions tests/MiniValidationPlus.UnitTests/Recursion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public void Recurses_When_Top_Level_Is_Invalid()
{
var thingToValidate = new TestType { RequiredName = null, Child = new TestChildType { RequiredCategory = null, MinLengthFive = "123" } };

var result = MiniValidator.TryValidate(thingToValidate, recurse: true, out var errors);
var result = MiniValidatorPlus.TryValidate(thingToValidate, recurse: true, out var errors);

Assert.False(result);
Assert.Equal(3, errors.Count);
Expand All @@ -25,7 +25,7 @@ public void Invalid_When_Child_Invalid_And_Recurse_True()
{
var thingToValidate = new TestType { Child = new TestChildType { RequiredCategory = null, MinLengthFive = "123" } };

var result = MiniValidator.TryValidate(thingToValidate, recurse: true, out var errors);
var result = MiniValidatorPlus.TryValidate(thingToValidate, recurse: true, out var errors);

Assert.False(result);
Assert.Equal(2, errors.Count);
Expand All @@ -36,7 +36,7 @@ public void Invalid_When_Child_Invalid_And_Recurse_Default()
{
var thingToValidate = new TestType { Child = new TestChildType { RequiredCategory = null } };

var result = MiniValidator.TryValidate(thingToValidate, out var errors);
var result = MiniValidatorPlus.TryValidate(thingToValidate, out var errors);

Assert.False(result);
Assert.Single(errors);
Expand All @@ -47,7 +47,7 @@ public void Valid_When_Child_Invalid_And_Recurse_False()
{
var thingToValidate = new TestType { Child = new TestChildType { RequiredCategory = null, MinLengthFive = "123" } };

var result = MiniValidator.TryValidate(thingToValidate, recurse: false, out var errors);
var result = MiniValidatorPlus.TryValidate(thingToValidate, recurse: false, out var errors);

Assert.True(result);
Assert.Empty(errors);
Expand All @@ -58,7 +58,7 @@ public void Valid_When_Child_Invalid_And_Property_Decorated_With_SkipRecursion()
{
var thingToValidate = new TestType { SkippedChild = new TestChildType { RequiredCategory = null, MinLengthFive = "123" } };

var result = MiniValidator.TryValidate(thingToValidate, recurse: false, out var errors);
var result = MiniValidatorPlus.TryValidate(thingToValidate, recurse: false, out var errors);

Assert.True(result);
Assert.Empty(errors);
Expand All @@ -69,7 +69,7 @@ public void Invalid_When_Enumerable_Item_Invalid_When_Recurse_Default()
{
var thingToValidate = new List<TestType> { new() { Child = new TestChildType { RequiredCategory = null, MinLengthFive = "123" } } };

var result = MiniValidator.TryValidate(thingToValidate, out var errors);
var result = MiniValidatorPlus.TryValidate(thingToValidate, out var errors);

Assert.False(result);
Assert.Equal(2, errors.Count);
Expand All @@ -80,7 +80,7 @@ public void Invalid_When_Enumerable_Item_Invalid_When_Recurse_True()
{
var thingToValidate = new List<TestType> { new() { Child = new TestChildType { RequiredCategory = null, MinLengthFive = "123" } } };

var result = MiniValidator.TryValidate(thingToValidate, recurse: true, out var errors);
var result = MiniValidatorPlus.TryValidate(thingToValidate, recurse: true, out var errors);

Assert.False(result);
Assert.Equal(2, errors.Count);
Expand All @@ -91,7 +91,7 @@ public void Valid_When_Enumerable_Item_Invalid_When_Recurse_False()
{
var thingToValidate = new List<TestType> { new() { Child = new TestChildType { RequiredCategory = null, MinLengthFive = "123" } } };

var result = MiniValidator.TryValidate(thingToValidate, recurse: false, out _);
var result = MiniValidatorPlus.TryValidate(thingToValidate, recurse: false, out _);

Assert.True(result);
}
Expand All @@ -101,7 +101,7 @@ public void Valid_When_Enumerable_Item_Has_Invalid_Descendant_But_Property_Decor
{
var thingToValidate = new List<TestType> { new() { SkippedChild = new() { RequiredCategory = null } } };

var result = MiniValidator.TryValidate(thingToValidate, recurse: true, out _);
var result = MiniValidatorPlus.TryValidate(thingToValidate, recurse: true, out _);

Assert.True(result);
}
Expand All @@ -116,7 +116,7 @@ public void Error_Message_Keys_For_Descendants_Are_Formatted_Correctly(int maxDe

TestChildType.AddDescendents(thingToValidate.Child, maxDepth);

var result = MiniValidator.TryValidate(thingToValidate, recurse: true, out var errors);
var result = MiniValidatorPlus.TryValidate(thingToValidate, recurse: true, out var errors);

Assert.False(result);
Assert.Single(errors);
Expand All @@ -135,7 +135,7 @@ public void Error_Message_Keys_For_Root_Enumerable_Are_Formatted_Correctly()
new() { RequiredName = null, TenOrMore = 5 },
};

var result = MiniValidator.TryValidate(thingToValidate, recurse: true, out var errors);
var result = MiniValidatorPlus.TryValidate(thingToValidate, recurse: true, out var errors);

Assert.False(result);
Assert.Equal(2, errors.Count);
Expand All @@ -151,7 +151,7 @@ public void Error_Message_Keys_For_Descendant_Enumerable_Are_Formatted_Correctly
thingToValidate.Children.Add(new() { });
thingToValidate.Children.Add(new() { RequiredCategory = null });

var result = MiniValidator.TryValidate(thingToValidate, recurse: true, out var errors);
var result = MiniValidatorPlus.TryValidate(thingToValidate, recurse: true, out var errors);

Assert.False(result);
Assert.Single(errors);
Expand All @@ -168,7 +168,7 @@ public void First_Error_In_Root_Enumerable_Returns_Immediately()
new() { RequiredName = null },
};

var result = MiniValidator.TryValidate(thingToValidate, recurse: true, out var errors);
var result = MiniValidatorPlus.TryValidate(thingToValidate, recurse: true, out var errors);

Assert.False(result);
Assert.Single(errors);
Expand All @@ -183,7 +183,7 @@ public void First_Error_In_Descendant_Enumerable_Returns_Immediately()
thingToValidate.Children.Add(new() { MinLengthFive = "123" });
thingToValidate.Children.Add(new() { RequiredCategory = null });

var result = MiniValidator.TryValidate(thingToValidate, recurse: true, out var errors);
var result = MiniValidatorPlus.TryValidate(thingToValidate, recurse: true, out var errors);

Assert.False(result);
Assert.Single(errors);
Expand All @@ -198,7 +198,7 @@ public void All_Errors_From_Invalid_Item_In_Descendant_Enumerable_Reported()
thingToValidate.Children.Add(new());
thingToValidate.Children.Add(new() { RequiredCategory = null, MinLengthFive = "123" });

var result = MiniValidator.TryValidate(thingToValidate, recurse: true, out var errors);
var result = MiniValidatorPlus.TryValidate(thingToValidate, recurse: true, out var errors);

Assert.False(result);
Assert.Equal(2, errors.Count);
Expand All @@ -214,7 +214,7 @@ public void Valid_When_Descendant_Invalid_And_Property_Decorated_With_SkipRecurs
thingToValidate.Children.Add(new());
thingToValidate.Children.Add(new() { SkippedChild = new() { RequiredCategory = null } });

var result = MiniValidator.TryValidate(thingToValidate, recurse: false, out var errors);
var result = MiniValidatorPlus.TryValidate(thingToValidate, recurse: false, out var errors);

Assert.True(result);
Assert.Empty(errors);
Expand All @@ -225,7 +225,7 @@ public void Invalid_When_Descendant_Invalid_And_Property_Is_Required_And_Decorat
{
var thingToValidate = new TestSkippedChildType();

var result = MiniValidator.TryValidate(thingToValidate, recurse: false, out var errors);
var result = MiniValidatorPlus.TryValidate(thingToValidate, recurse: false, out var errors);

Assert.False(result);
Assert.Single(errors);
Expand All @@ -236,7 +236,7 @@ public void Invalid_When_Derived_Type_Has_Invalid_Inherited_Property()
{
var thingToValidate = new TestType { Child = new TestChildTypeDerivative { RequiredCategory = null } };

var result = MiniValidator.TryValidate(thingToValidate, recurse: true, out var errors);
var result = MiniValidatorPlus.TryValidate(thingToValidate, recurse: true, out var errors);

Assert.False(result);
Assert.Single(errors);
Expand All @@ -247,7 +247,7 @@ public void Invalid_When_Derived_Type_Has_Invalid_Own_Property()
{
var thingToValidate = new TestType { Child = new TestChildTypeDerivative { DerivedMinLengthTen = "123" } };

var result = MiniValidator.TryValidate(thingToValidate, recurse: true, out var errors);
var result = MiniValidatorPlus.TryValidate(thingToValidate, recurse: true, out var errors);

Assert.False(result);
Assert.Single(errors);
Expand All @@ -258,7 +258,7 @@ public void Valid_When_Derived_Type_Has_Invalid_Own_Property_With_Recurse_False(
{
var thingToValidate = new TestType { Child = new TestChildTypeDerivative { DerivedMinLengthTen = "123" } };

var result = MiniValidator.TryValidate(thingToValidate, recurse: false, out var errors);
var result = MiniValidatorPlus.TryValidate(thingToValidate, recurse: false, out var errors);

Assert.True(result);
Assert.Empty(errors);
Expand All @@ -272,7 +272,7 @@ public void Invalid_When_ValidatableObject_Child_Validate_Is_Invalid()
ValidatableChild = new TestValidatableChildType { TwentyOrMore = 12 }
};

var result = MiniValidator.TryValidate(thingToValidate, out var errors);
var result = MiniValidatorPlus.TryValidate(thingToValidate, out var errors);

Assert.False(result);
Assert.Single(errors);
Expand All @@ -287,7 +287,7 @@ public void Invalid_When_Derived_ValidatableObject_Child_Validate_Is_Invalid()
Child = new TestValidatableChildType { TwentyOrMore = 19 }
};

var result = MiniValidator.TryValidate(thingToValidate, out var errors);
var result = MiniValidatorPlus.TryValidate(thingToValidate, out var errors);

Assert.False(result);
Assert.Single(errors);
Expand All @@ -302,7 +302,7 @@ public void Invalid_When_Derived_Polymorphic_Child_Validate_Is_Invalid()
Child = new TestValidatableChildType { MinLengthFive = "123" }
};

var result = MiniValidator.TryValidate(thingToValidate, out var errors);
var result = MiniValidatorPlus.TryValidate(thingToValidate, out var errors);

Assert.False(result);
Assert.Single(errors);
Expand All @@ -318,7 +318,7 @@ public void Child_ValidatableObject_Is_Validated_When_Parent_Is_Invalid()
ValidatableChild = new() { TwentyOrMore = 12 }
};

var result = MiniValidator.TryValidate(thingToValidate, out var errors);
var result = MiniValidatorPlus.TryValidate(thingToValidate, out var errors);

Assert.False(result);
Assert.Equal(2, errors.Count);
Expand All @@ -333,7 +333,7 @@ public void Invalid_When_Derived_ValidatableOnlyChild_Is_Invalid()
ValidatableOnlyChild = new TestValidatableOnlyType { TwentyOrMore = 12 }
};

var result = MiniValidator.TryValidate(thingToValidate, out var errors);
var result = MiniValidatorPlus.TryValidate(thingToValidate, out var errors);

Assert.False(result);
Assert.Single(errors);
Expand All @@ -348,7 +348,7 @@ public void Invalid_When_Polymorphic_ValidatableOnlyChild_Is_Invalid()
PocoChild = new TestValidatableOnlyType { TwentyOrMore = 12 }
};

var result = MiniValidator.TryValidate(thingToValidate, out var errors);
var result = MiniValidatorPlus.TryValidate(thingToValidate, out var errors);

Assert.False(result);
Assert.Single(errors);
Expand All @@ -363,7 +363,7 @@ public void Invalid_When_Polymorphic_Child_With_Validation_Attributes_Is_Invalid
PocoChild = new TestChildType { MinLengthFive = "123" }
};

var result = MiniValidator.TryValidate(thingToValidate, out var errors);
var result = MiniValidatorPlus.TryValidate(thingToValidate, out var errors);

Assert.False(result);
Assert.Single(errors);
Expand All @@ -380,7 +380,7 @@ public void Throws_InvalidOperationException_When_AsyncValidatableOnlyChild_Is_I

Assert.Throws<ArgumentException>(() =>
{
var result = MiniValidator.TryValidate(thingToValidate, out var errors);
var result = MiniValidatorPlus.TryValidate(thingToValidate, out var errors);
});
}

Expand All @@ -392,7 +392,7 @@ public async Task Invalid_When_AsyncValidatableOnlyChild_Is_Invalid()
NeedsAsync = new() { TwentyOrMore = 12 }
};

var (isValid, errors) = await MiniValidator.TryValidateAsync(thingToValidate);
var (isValid, errors) = await MiniValidatorPlus.TryValidateAsync(thingToValidate);

Assert.False(isValid);
Assert.Single(errors);
Expand All @@ -407,7 +407,7 @@ public void Invalid_When_AsyncValidatableOnlyChild_Is_Invalid_Allowing_SyncOverA
NeedsAsync = new() { TwentyOrMore = 12 }
};

var result = MiniValidator.TryValidate(thingToValidate, recurse: true, allowAsync: true, out var errors);
var result = MiniValidatorPlus.TryValidate(thingToValidate, recurse: true, allowAsync: true, out var errors);

Assert.False(result);
Assert.Single(errors);
Expand All @@ -429,7 +429,7 @@ public void Throws_InvalidOperationException_When_Polymorphic_AsyncValidatableOn

try
{
var result = MiniValidator.TryValidate(thingToValidate, serviceProvider, out var errors);
var result = MiniValidatorPlus.TryValidate(thingToValidate, serviceProvider, out var errors);
}
finally
{
Expand All @@ -446,7 +446,7 @@ public void Invalid_When_Polymorphic_AsyncValidatableOnlyChild_Is_Invalid_Allowi
PocoChild = new TestAsyncValidatableChildType { TwentyOrMore = 12 }
};

var result = MiniValidator.TryValidate(thingToValidate, recurse: true, allowAsync: true, out var errors);
var result = MiniValidatorPlus.TryValidate(thingToValidate, recurse: true, allowAsync: true, out var errors);

Assert.False(result);
Assert.Single(errors);
Expand All @@ -461,7 +461,7 @@ public async Task Invalid_When_Polymorphic_AsyncValidatableOnlyChild_Is_Invalid(
PocoChild = new TestAsyncValidatableChildType { TwentyOrMore = 12 }
};

var (isValid, errors) = await MiniValidator.TryValidateAsync(thingToValidate);
var (isValid, errors) = await MiniValidatorPlus.TryValidateAsync(thingToValidate);

Assert.False(isValid);
Assert.Single(errors);
Expand Down
Loading

0 comments on commit bc1f202

Please sign in to comment.