You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have a list of database items that are of class DbElement. I need to build an error message specific to the element being validated and looping through each element isn't viable as some may be dependent on an element in the elements list. How can I get the "Expected Console Results" I am looking for, with this current setup. Or is it not possible.
Expected Console Results
"Failure: The value provided for (Street Name) exceeds maximum allowed length of 25. ",
"Failure: The value provided for (Age) exceeds range of 1 to 50."
public class DbElement
{
public string Name { get; set; } = string.Empty;
public string DataType { get; set; } = string.Empty;
public string Value { get; set; } = string.Empty;
}
public void ValidateElements()
{
var inputs = new list<DbElement>
{
new DbElement { Name = "First Name", DataType = "string", Value = "Gerthin" },
new DbElement { Name = "Last Name", DataType = "string", Value = "Hungswell" },
new DbElement { Name = "Street Name", DataType = "string", Value = "Long Street name that is going to fail validation." },
new DbElement { Name = "Age", DataType = "nbr", Value = "65" },
new DbElement { Name = "Game Age", DataType = "nbr", Value = "25" },
};
var files = Directory.GetFiles(Directory.GetCurrentDirectory(), "Rules.json", SearchOption.AllDirectories);
if (files == null || files.Length == 0)
throw new Exception("Rules not found.");
var fileData = File.ReadAllText(files[0]);
var workflow = JsonConvert.DeserializeObject<List<Workflow>>(fileData);
if (workflow == null)
throw new Exception($"Workflow doesn't exists");
var bre = new RulesEngine.RulesEngine(workflow.ToArray(), null);
List<RuleResultTree> resultList = bre.ExecuteAllRulesAsync("Database Element Validation", inputs).Result;
foreach (var res in resultList)
{
if (!res.IsSuccess)
Console.WriteLine($"Failure: {res.ExceptionMessage}");
}
}
Rules.Json File Contents
[
{
"WorkflowName": "Database Element Validation",
"Rules": [
{
"RuleName": "StringMaxLength",
"ErrorType": "Error",
"ErrorMessage": "The value provided for ($(input.Name)) exceeds maximum allowed length of $(maxLength). ",
"RuleExpressionType": "LambdaExpression",
"Expression": "input.DataType == \"string\" AND input.Value.Length <= maxLength",
"LocalParams": [
{
"Name": "maxLength",
"Expression": 25
}
]
},
{
"RuleName": "NumericRange",
"ErrorType": "Error",
"ErrorMessage": "The value provided for ($(input.Name)) exceeds range of $(minValue) to $(maxValue). ",
"RuleExpressionType": "LambdaExpression",
"Expression": "input.DataType == \"nbr\" AND input.Value >= minValue AND input.Value <= maxValue",
"LocalParams": [
{
"Name": "minValue",
"Expression": 1
},
{
"Name": "maxValue",
"Expression": 50
}
]
}
]
}
]
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I have a list of database items that are of class DbElement. I need to build an error message specific to the element being validated and looping through each element isn't viable as some may be dependent on an element in the elements list. How can I get the "Expected Console Results" I am looking for, with this current setup. Or is it not possible.
Expected Console Results
"Failure: The value provided for (Street Name) exceeds maximum allowed length of 25. ",
"Failure: The value provided for (Age) exceeds range of 1 to 50."
Rules.Json File Contents
Beta Was this translation helpful? Give feedback.
All reactions