Skip to content

Commit

Permalink
Added MiniValidator.RequiresValidation()
Browse files Browse the repository at this point in the history
Bumped to version 0.6.0
  • Loading branch information
DamianEdwards committed May 27, 2022
1 parent 73f430d commit d3392b7
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project>

<PropertyGroup>
<VersionPrefix>0.5.1</VersionPrefix>
<VersionPrefix>0.6.0</VersionPrefix>
<VersionSuffix Condition="'$(BuildNumber)' == ''">pre</VersionSuffix>
<VersionSuffix Condition="'$(BuildNumber)' != ''">pre.$(BuildNumber)</VersionSuffix>
<Authors>Damian Edwards</Authors>
Expand Down
32 changes: 32 additions & 0 deletions src/MiniValidation/MiniValidator.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Resources;
Expand All @@ -15,13 +16,36 @@ namespace MiniValidation
public static class MiniValidator
{
private static readonly TypeDetailsCache _typeDetailsCache = new();
private static readonly IDictionary<string, string[]> _emptyErrors = new ReadOnlyDictionary<string, string[]>(new Dictionary<string, string[]>());

/// <summary>
/// Gets or sets the maximum depth allowed when validating an object with recursion enabled.
/// Defaults to 32.
/// </summary>
public static int MaxDepth { get; set; } = 32;

/// <summary>
/// Determines if the specified <see cref="Type"/> has anything to validate.
/// </summary>
/// <remarks>
/// Objects of types with nothing to validate will always return <c>true</c> when passed to <see cref="TryValidate(object, out IDictionary{string, string[]})"/>.
/// </remarks>
/// <param name="targetType">The <see cref="Type"/>.</param>
/// <param name="recurse"><c>true</c> to recursively check descendant types; if <c>false</c> only simple values directly on the target type are checked.</param>
/// <returns><c>true</c> if the <see cref="Type"/> has anything to validate, <c>false</c> if not.</returns>
/// <exception cref="ArgumentNullException"></exception>
public static bool RequiresValidation(Type targetType, bool recurse = true)
{
if (targetType == null)
{
throw new ArgumentNullException(nameof(targetType));
}

return typeof(IValidatableObject).IsAssignableFrom(targetType)
|| (recurse && typeof(IEnumerable).IsAssignableFrom(targetType))
|| _typeDetailsCache.Get(targetType).Any(p => p.HasValidationAttributes || recurse);
}

/// <summary>
/// Determines whether the specific object is valid. This method recursively validates descendant objects.
/// </summary>
Expand Down Expand Up @@ -53,6 +77,14 @@ public static bool TryValidate(object target, bool recurse, out IDictionary<stri
throw new ArgumentNullException(nameof(target));
}

if (!RequiresValidation(target.GetType(), recurse))
{
errors = _emptyErrors;

// Return true for types with nothing to validate
return true;
}

var validatedObjects = new Dictionary<object, bool?>();
var workingErrors = new Dictionary<string, List<string>>();
var isValid = TryValidateImpl(target, recurse, workingErrors, validatedObjects);
Expand Down

0 comments on commit d3392b7

Please sign in to comment.