Added validation for fields
This commit is contained in:
parent
f00bde7e24
commit
c4dcca8971
@ -1,5 +1,6 @@
|
||||
using System.Reflection;
|
||||
using SessionZero.SzfLib.Objects;
|
||||
using SessionZero.SzfLib.Parser;
|
||||
|
||||
namespace SessionZero.SzfLib.Helpers;
|
||||
|
||||
@ -17,7 +18,7 @@ public static class SzfHelper
|
||||
return obj.IsAbstract || obj.IsInterface ? null : obj;
|
||||
}
|
||||
|
||||
public static SzfError BasicSzfValidation(ISzfObject obj)
|
||||
public static SzfError BasicSzfValidation(ISzfObject obj, bool canHaveEmptyFieldValues = false)
|
||||
{
|
||||
SzfError result = new SzfError();
|
||||
|
||||
@ -26,6 +27,50 @@ public static class SzfHelper
|
||||
|
||||
if (obj.GetMetadataField("Name") == string.Empty) result.AddError("No metadata field name found");
|
||||
|
||||
foreach (var field in GetAllFields(obj))
|
||||
{
|
||||
var fieldValidation = field.Validate(canHaveEmptyFieldValues);
|
||||
if (!fieldValidation.IsValid) result.AddError(fieldValidation.Message);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public static IEnumerable<SzfSection> GetAllSectionsAndSubsections(ISzfObject obj)
|
||||
{
|
||||
List<SzfSection> allSections = new List<SzfSection>();
|
||||
|
||||
foreach (var section in obj.Sections)
|
||||
{
|
||||
allSections.Add(section);
|
||||
allSections.AddRange(GetAllSubsections(section));
|
||||
}
|
||||
|
||||
return allSections;
|
||||
}
|
||||
|
||||
public static IEnumerable<SzfSection> GetAllSubsections(SzfSection section)
|
||||
{
|
||||
List<SzfSection> subsections = new List<SzfSection>();
|
||||
|
||||
foreach (var subsection in section.Subsections.Values)
|
||||
{
|
||||
subsections.Add(subsection);
|
||||
subsections.AddRange(GetAllSubsections(subsection));
|
||||
}
|
||||
|
||||
return subsections;
|
||||
}
|
||||
|
||||
public static IEnumerable<SzfField> GetAllFields(ISzfObject obj)
|
||||
{
|
||||
List<SzfField> allFields = new List<SzfField>();
|
||||
|
||||
foreach (var section in GetAllSectionsAndSubsections(obj))
|
||||
{
|
||||
allFields.AddRange(section.Fields.Values);
|
||||
}
|
||||
|
||||
return allFields;
|
||||
}
|
||||
}
|
@ -1,3 +1,5 @@
|
||||
using SessionZero.SzfLib.Helpers;
|
||||
|
||||
namespace SessionZero.SzfLib.Parser;
|
||||
|
||||
public class SzfField(string name, SzfFieldType type)
|
||||
@ -6,8 +8,72 @@ public class SzfField(string name, SzfFieldType type)
|
||||
public SzfFieldType SzfType { get; set; } = type;
|
||||
public string? Value { get; set; }
|
||||
|
||||
public bool Validate()
|
||||
/// <summary>
|
||||
/// Validates the field based on its type and value. Returns an SzfError object containing validation results, if any.
|
||||
/// </summary>
|
||||
/// <param name="canHaveEmptyValue"></param>
|
||||
/// <returns></returns>
|
||||
public SzfError Validate(bool canHaveEmptyValue = false)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
SzfError result = new();
|
||||
|
||||
if (string.IsNullOrEmpty(Name))
|
||||
{
|
||||
result.AddError("Field name cannot be null or empty.");
|
||||
}
|
||||
|
||||
if (!canHaveEmptyValue && string.IsNullOrEmpty(Value))
|
||||
{
|
||||
result.AddError($"Field '{Name}' cannot be empty.");
|
||||
}
|
||||
else
|
||||
{
|
||||
var isValid = ValidateSzfType(this);
|
||||
if (!isValid)
|
||||
{
|
||||
result.AddError($"Field '{Name}' has an invalid value for type '{SzfType}'.");
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Validates the value of the field based on its SzfType.
|
||||
/// </summary>
|
||||
/// <param name="field"></param>
|
||||
/// <returns></returns>
|
||||
private bool ValidateSzfType(SzfField field)
|
||||
{
|
||||
var modifiedValue = field.Value?.Trim().ToLower();
|
||||
|
||||
switch (field.SzfType)
|
||||
{
|
||||
case SzfFieldType.Text:
|
||||
return !string.IsNullOrEmpty(modifiedValue);
|
||||
case SzfFieldType.TextField:
|
||||
return !string.IsNullOrEmpty(modifiedValue);
|
||||
case SzfFieldType.Number:
|
||||
if (decimal.TryParse(modifiedValue, out _)) return true;
|
||||
break;
|
||||
case SzfFieldType.Bool:
|
||||
if (bool.TryParse(modifiedValue, out _)) return true;
|
||||
break;
|
||||
case SzfFieldType.Calculated:
|
||||
// TODO: Implement validation for calculated fields
|
||||
// For now, we assume they are valid if they are not empty
|
||||
return !string.IsNullOrEmpty(modifiedValue);
|
||||
case SzfFieldType.EntryReference:
|
||||
// Currently, we assume entry references are valid if they are not empty
|
||||
return !string.IsNullOrEmpty(modifiedValue);
|
||||
case SzfFieldType.EntryReferenceList:
|
||||
// Currently, we assume entry reference lists are valid if they are not empty
|
||||
return !string.IsNullOrEmpty(modifiedValue);
|
||||
case SzfFieldType.System:
|
||||
// System fields are typically predefined and may not require validation
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user