Skip to content

Commit

Permalink
fix(InputArgumentValidator): add InputArgumentValidator
Browse files Browse the repository at this point in the history
  • Loading branch information
MingboPeng committed May 10, 2023
1 parent 098223d commit 1440b0f
Show file tree
Hide file tree
Showing 2 changed files with 165 additions and 31 deletions.
147 changes: 147 additions & 0 deletions src/PollinationSDK/Wrapper/InputArgumentValidator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@

using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using PollinationSDK;

namespace PollinationSDK.Wrapper
{
public class InputArgumentValidator
{
public PollinationSDK.Interface.Io.Inputs.IDag DagInput { get; set; }
/// <summary>
/// Platform specific DagInputAlias
/// </summary>
public PollinationSDK.Interface.Io.Inputs.IAlias DagInputAlias { get; set; }


public string Name => this.DagInput?.Name;

public List<PollinationSDK.IOAliasHandler> Handlers => this.DagInputAlias?.Handler;
public PollinationSDK.DAGLinkedInputAlias LinkedAlias => this.DagInputAlias as PollinationSDK.DAGLinkedInputAlias;

public InputArgumentValidator(PollinationSDK.Interface.Io.Inputs.IDag dagInput, string platform)
{
this.DagInput = dagInput;
this.DagInputAlias = dagInput.GetAlias(platform);
}

public static object CheckAndValidate(PollinationSDK.Interface.Io.Inputs.IDag dagInput, string platform, object value, HandlerChecker handlerChecker)
{
if (dagInput == null)
throw new ArgumentNullException(nameof(dagInput));

if (value == null) return null;

var vd = new InputArgumentValidator(dagInput, platform);
return vd.CheckAndValidate(value, handlerChecker);
}

/// <summary>
/// Checks the data with alias' JsonSchema spec (if any), followed by checks of handlers and original DagInput's JsonSchema spec.
/// It runs in order of following: ValidateWithAliasSpec, CheckInputWithHandler, and CheckAndValidate.
/// </summary>
/// <param name="value">Data to be checked. Null will be skipped</param>
/// <param name="handlerChecker"></param>
/// <returns>Checked value by handler, return null if any checks failed and an ArgumentException will be thrown.</returns>
public object CheckAndValidate(object value, HandlerChecker handlerChecker)
{
if (value == null) return null;
// check linked input
var linkValue = CheckLinkedData(handlerChecker);
// validate Alias input
if (!this.ValidateWithAliasSpec(linkValue)) return null;
// convert with handlers
var obj = this.CheckInputWithHandler(linkValue, handlerChecker);
// validate input specs
if (!this.ValidateWithSpec(obj)) return null;
return obj;
}

public bool ValidateWithSpec(object value)
{
return this.DagInput.ValidateWithSpec(value);
}

public bool ValidateWithAliasSpec(object value)
{
return (this.DagInputAlias?.ValidateWithSpec(value)).GetValueOrDefault(true);
}


public object CheckInputWithHandler(object inputData, HandlerChecker handlerChecker)
{
return handlerChecker.CheckWithHandlers(inputData, this.Handlers);
}

public object CheckLinkedData(HandlerChecker handlerChecker, string language = "csharp")
{
if (this.LinkedAlias?.Handler == null)
return null;

var handler = this.LinkedAlias.Handler.FirstOrDefault(_ => _.Language == language);

return handlerChecker?.CheckLinkedData(handler);
}

public bool IsPathType() => this.DagInput.IsPathType();


public List<string> ValidatePathInputs(IEnumerable<string> userInputs)
{
var checkedValues = userInputs.Where(_ => !string.IsNullOrEmpty(_.ToString().Trim()));
//var type = inputInfo.DagInput.GetType();

var name = this.Name;

foreach (var userInput in checkedValues)
{
var userValue = userInput.Trim();

// validate file extension
if (this.DagInput is PollinationSDK.DAGFileInput p)
CheckIfFileExist(userValue, p.Extensions);
else if (this.DagInput is PollinationSDK.DAGFolderInput f)
CheckIfFolderExist(userValue);
else if (this.DagInput is PollinationSDK.DAGPathInput path)
{
FileAttributes attr = File.GetAttributes(userValue);
if (attr.HasFlag(FileAttributes.Directory))
CheckIfFolderExist(userValue);
else
CheckIfFileExist(userValue, path.Extensions);
}
else
{
throw new ArgumentException($"Unknown input type. It most likely happens with mismatching versions!");
}

}

return checkedValues.ToList();

void CheckIfFileExist(string value, List<string> validExtensions)
{
if (!File.Exists(value))
throw new ArgumentException($"\nInput parameter \"{name}\" is not a file path or it does not exist: {value}.\nPlease use the absolute path.");
var ext = validExtensions;
if (ext == null) return;

if (!ext.Any(_ => Path.GetExtension(value).EndsWith(_)))
throw new ArgumentException($"Input parameter \"{name}\" has the file path without [\"{string.Join(", ", ext)}\"] extension.");
}

void CheckIfFolderExist(string value)
{
if (!Directory.Exists(value))
throw new ArgumentException($"\nInput parameter \"{name}\" is not a directory path or it does not exist: {value}.\nPlease use the absolute path.");

}

}

}


}
49 changes: 18 additions & 31 deletions src/PollinationSDK/Wrapper/JobInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,52 +97,34 @@ public JobInfo Duplicate()
{
return FromJson(this.ToJson());
}
private List<AnyOf<JobArgument, JobPathArgument>> CheckArgumentsWithHandlers(List<AnyOf<JobArgument, JobPathArgument>> args, string platform, string handlerLanguage)

private List<AnyOf<JobArgument, JobPathArgument>> CheckArgumentsWithHandlers(List<AnyOf<JobArgument, JobPathArgument>> args, string platform, string handlerLanguage, HandlerChecker handlerChecker)
{
//Deal with single run
var inputs = this.Recipe.InputList;
var handlerChecker = DefaultHandlerChecker.Instance;


// create a placeholder
var job = new Job("invalid");

foreach (var item in inputs)
{
var isPath = item.IsPathType();
PollinationSDK.Interface.Io.Inputs.IJob currentArg = null;
object currentValue = null;
if (isPath)
currentArg = args.OfType<JobPathArgument>().FirstOrDefault(_ => _.Name == item.Name);
else
currentArg = args.OfType<JobArgument>().FirstOrDefault(_ => _.Name == item.Name);

var dagInputAlias = item.GetAlias(platform);
if (dagInputAlias == null) // do nothing is there is no alias
{
job.AddArgument(currentArg);
continue;
}

var linkedAlias = dagInputAlias as PollinationSDK.DAGLinkedInputAlias;
object processedData = null;
if (linkedAlias?.Handler != null) // linked input
{
var handler = linkedAlias.Handler.FirstOrDefault(_ => _.Language == handlerLanguage);
processedData = handlerChecker.CheckLinkedData(handler)?.ToString();
var pathArg = args.OfType<JobPathArgument>().FirstOrDefault(_ => _.Name == item.Name);
currentArg = pathArg;
currentValue = pathArg.Source;
}
else // other alias
else
{
if (isPath)
{
var a = currentArg as JobPathArgument;
processedData = handlerChecker.CheckWithHandlers(a.Source, dagInputAlias.Handler);
}
else
{
var a = currentArg as JobArgument;
processedData = handlerChecker.CheckWithHandlers(a.Value, dagInputAlias.Handler);
}
var valueArg = args.OfType<JobArgument>().FirstOrDefault(_ => _.Name == item.Name);
currentArg = valueArg;
currentValue = valueArg.Value;
}

var processedData = InputArgumentValidator.CheckAndValidate(item, platform, currentValue, handlerChecker);
if (processedData == null)
{
job.AddArgument(currentArg);
Expand All @@ -165,6 +147,11 @@ public JobInfo Duplicate()
}

public void CheckArgumentsWithHandlers(string platform, string handlerLanguage)
{
CheckArgumentsWithHandlers(platform, handlerLanguage, DefaultHandlerChecker.Instance);
}

public void CheckArgumentsWithHandlers(string platform, string handlerLanguage, HandlerChecker handlerChecker)
{
if(this.Recipe == null)
{
Expand All @@ -181,7 +168,7 @@ public void CheckArgumentsWithHandlers(string platform, string handlerLanguage)
var newArgSets = new List<List<AnyOf<JobArgument, JobPathArgument>>>();
foreach (var argSet in argSets)
{
var set = CheckArgumentsWithHandlers(argSet, platform, handlerLanguage);
var set = CheckArgumentsWithHandlers(argSet, platform, handlerLanguage, handlerChecker);
newArgSets.Add(set);
}

Expand Down

0 comments on commit 1440b0f

Please sign in to comment.