diff --git a/SessionZero.SzfLib/Helpers/SzfHelper.cs b/SessionZero.SzfLib/Helpers/SzfHelper.cs index ee33f19..c4fa7a1 100644 --- a/SessionZero.SzfLib/Helpers/SzfHelper.cs +++ b/SessionZero.SzfLib/Helpers/SzfHelper.cs @@ -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 GetAllSectionsAndSubsections(ISzfObject obj) + { + List allSections = new List(); + + foreach (var section in obj.Sections) + { + allSections.Add(section); + allSections.AddRange(GetAllSubsections(section)); + } + + return allSections; + } + + public static IEnumerable GetAllSubsections(SzfSection section) + { + List subsections = new List(); + + foreach (var subsection in section.Subsections.Values) + { + subsections.Add(subsection); + subsections.AddRange(GetAllSubsections(subsection)); + } + + return subsections; + } + + public static IEnumerable GetAllFields(ISzfObject obj) + { + List allFields = new List(); + + foreach (var section in GetAllSectionsAndSubsections(obj)) + { + allFields.AddRange(section.Fields.Values); + } + + return allFields; + } } \ No newline at end of file diff --git a/SessionZero.SzfLib/Parser/SzfField.cs b/SessionZero.SzfLib/Parser/SzfField.cs index 3f0a5ca..5df4d57 100644 --- a/SessionZero.SzfLib/Parser/SzfField.cs +++ b/SessionZero.SzfLib/Parser/SzfField.cs @@ -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() + /// + /// Validates the field based on its type and value. Returns an SzfError object containing validation results, if any. + /// + /// + /// + 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; + } + + /// + /// Validates the value of the field based on its SzfType. + /// + /// + /// + 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; } } \ No newline at end of file