Skip to content

Commit

Permalink
Remove redundant check and bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
mr5z committed May 9, 2021
1 parent 7d97755 commit 07af7cf
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 19 deletions.
2 changes: 1 addition & 1 deletion PropertyValidator/Extensions/ExpressionExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace PropertyValidator.Extensions
{
public static class ExpressionExtension
static class ExpressionExtension
{
public static string GetMemberName<T>(this Expression<T> expression)
{
Expand Down
3 changes: 1 addition & 2 deletions PropertyValidator/Extensions/ObjectExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;

namespace PropertyValidator.Extensions
{
public static class ObjectExtension
static class ObjectExtension
{
public static T ToObject<T>(this IDictionary<string, object> source)
where T : class, new() => ToObject<T, object>(source);
Expand Down
2 changes: 1 addition & 1 deletion PropertyValidator/Helpers/ReflectionHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace PropertyValidator.Helpers
{
public static class ReflectionHelper
static class ReflectionHelper
{
public static FieldInfo? GetField(Type fromType, string fieldName)
{
Expand Down
2 changes: 1 addition & 1 deletion PropertyValidator/Models/RuleCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace PropertyValidator.Models
{
public class RuleCollection<TModel>
{
private readonly List<IValidationRule> validationRuleList = new List<IValidationRule>();
private readonly List<IValidationRule> validationRuleList = new();
private readonly object? target;

public RuleCollection(object? target)
Expand Down
9 changes: 6 additions & 3 deletions PropertyValidator/Models/ValidationResultArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@ public ValidationResultArgs(string? propertyName, IDictionary<string, IEnumerabl

public string? PropertyName { get; }

public IEnumerable<string?> ErrorMessages => ErrorDictionary.Values.FirstOrDefault();

public IDictionary<string, IEnumerable<string?>> ErrorDictionary { get; }

public string? FirstError => ErrorDictionary.Values.FirstOrDefault()?.FirstOrDefault();
public IEnumerable<string?> ErrorMessages => ErrorDictionary
.Values
.FirstOrDefault(errors => errors.Any(message => !string.IsNullOrEmpty(message)));

public string? FirstError => ErrorMessages
.FirstOrDefault();

public void FillErrorProperty<T>(T notifiableModel) where T : INotifyPropertyChanged
{
Expand Down
6 changes: 5 additions & 1 deletion PropertyValidator/Models/ValidationRule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ public abstract class ValidationRule<T> : IValidationRule

public bool HasError { get; private set; }

public bool Validate(object value) => (HasError = !IsValid((T)value));
public bool Validate(object value)
{
HasError = !IsValid((T)value);
return !HasError;
}

// TODO support async validations
//public virtual Task<bool> IsValidAsync(T value) => Task.FromResult(IsValid(value));
Expand Down
9 changes: 5 additions & 4 deletions PropertyValidator/PropertyValidator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
<Authors>Mark Laureta</Authors>
<Company>Nkraft</Company>
<Description>A simple library to help you validate properties of class that implements INotifyPropertyChanged.</Description>
<PackageReleaseNotes>Fix property notification when the error message has been cleared for ValidationResultArgs::FillErrorProperty</PackageReleaseNotes>
<PackageReleaseNotes>- Fix property notification when the error message has been cleared for ValidationResultArgs::FillErrorProperty
- Remove redundant check and bug fixes</PackageReleaseNotes>
<PackageProjectUrl>https://github.com/mr5z/PropertyValidator</PackageProjectUrl>
<RepositoryUrl>https://github.com/mr5z/PropertyValidator</RepositoryUrl>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<FileVersion>1.0.6.5</FileVersion>
<AssemblyVersion>1.0.6.5</AssemblyVersion>
<Version>1.0.6.5</Version>
<FileVersion>1.0.6.6</FileVersion>
<AssemblyVersion>1.0.6.6</AssemblyVersion>
<Version>1.0.6.6</Version>
<NeutralLanguage>en</NeutralLanguage>
</PropertyGroup>

Expand Down
13 changes: 7 additions & 6 deletions PropertyValidator/Services/ValidationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -196,17 +196,18 @@ public static bool ValidateRuleCollection(
object target,
string? propertyName = null)
{
if (string.IsNullOrEmpty(propertyName))
return true;

bool noErrors = true;
foreach (var rule in ruleCollection)
var filteredCollection = ruleCollection.Where(it => it.PropertyName == propertyName);
foreach (var rule in filteredCollection)
{
if (!string.IsNullOrEmpty(propertyName) && rule.PropertyName != propertyName)
continue;

var type = target.GetType();
var property = type.GetProperty(rule.PropertyName);
var value = property.GetValue(target, null);
rule.Validate(value);
noErrors = noErrors && !rule.HasError;
var isValid = rule.Validate(value);
noErrors = noErrors && isValid;
}
return noErrors;
}
Expand Down

0 comments on commit 07af7cf

Please sign in to comment.