Skip to content

Commit

Permalink
Extend output overloads
Browse files Browse the repository at this point in the history
  • Loading branch information
ArchLeaders committed Jul 11, 2023
1 parent 15ee7c0 commit 4f768f5
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
9 changes: 7 additions & 2 deletions src/ConfigFactory.Core/ConfigModule.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using CommunityToolkit.Mvvm.ComponentModel;
using ConfigFactory.Core.Attributes;
using ConfigFactory.Core.Components;
using ConfigFactory.Core.Models;
using System.Linq.Expressions;
Expand Down Expand Up @@ -89,10 +90,13 @@ public void Save()
JsonSerializer.Serialize(fs, (T)this);
}

public bool Validate(out string? message)
public bool Validate() => Validate(out _, out _);
public bool Validate(out string? message) => Validate(out message, out _);
public bool Validate(out string? message, out (PropertyInfo? info, ConfigAttribute? attribute) target)
{
foreach ((var name, (var validate, var errorMessage)) in Validators) {
PropertyInfo propertyInfo = Properties[name].info;
target = Properties[name];
PropertyInfo propertyInfo = target.info!;
if (validate(propertyInfo.GetValue(this)) is bool isValid) {
ValidationInterface?.SetValidationColor(propertyInfo, isValid ? SuccessColor : FailureColor);

Expand All @@ -103,6 +107,7 @@ public bool Validate(out string? message)
}
}

target = (null, null);
message = "Validation Successful";
return true;
}
Expand Down
14 changes: 11 additions & 3 deletions src/ConfigFactory.Core/IConfigModule.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using ConfigFactory.Core.Components;
using ConfigFactory.Core.Attributes;
using ConfigFactory.Core.Components;
using ConfigFactory.Core.Models;
using System.Reflection;

namespace ConfigFactory.Core;

Expand Down Expand Up @@ -37,10 +39,16 @@ public interface IConfigModule
public void Save();

/// <summary>
/// Validates the properties and sends the invalid error message to the output <paramref name="message"/>
/// Validates each property registered in the <see cref="Validators"/> collection
/// </summary>
/// <returns>
/// <see langword="true"/> if the validation was successful; <see langword="false"/> if the validation failed
/// </returns>
public bool Validate(out string? message);
public virtual bool Validate() => Validate(out _, out _);

/// <inheritdoc cref="Validate()"/>
public virtual bool Validate(out string? message) => Validate(out message, out _);

/// <inheritdoc cref="Validate()"/>
public bool Validate(out string? message, out (PropertyInfo? info, ConfigAttribute? attribute) target);
}
2 changes: 1 addition & 1 deletion src/ConfigFactory/ConfigFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public static ConfigPageModel Append(this ConfigPageModel configPageModel, IConf
}

module.ValidationInterface = new ValidationInterface(configPageModel);
module.Validate(out _);
module.Validate();

return configPageModel;
}
Expand Down

0 comments on commit 4f768f5

Please sign in to comment.