1
Fork 0
mirror of https://github.com/xivdev/EXDSchema.git synced 2025-06-06 16:17:46 +00:00

... and add a field name validator

This commit is contained in:
Liam 2023-10-17 00:16:32 -04:00
parent cc494e9454
commit ef2b780c39
2 changed files with 44 additions and 0 deletions

View file

@ -36,6 +36,7 @@ public class SchemaValidator
new ColumnCountValidator(gameData),
new IconTypeValidator(gameData),
new NamedInnerNamedOuterValidator(gameData),
new FieldNameValidator(gameData),
new ModelIdTypeValidator(gameData),
new ColorTypeValidator(gameData),
new IconPathExistsValidator(gameData),

View file

@ -0,0 +1,43 @@
using System.Text.RegularExpressions;
using Lumina;
using Lumina.Data.Files.Excel;
using SchemaValidator.New;
using SchemaValidator.Util;
namespace SchemaValidator.Validation.Validators;
public partial class FieldNameValidator : Validator
{
public override string ValidatorName() => "FieldNameValidator";
private string _sheetName = "";
[GeneratedRegex("^[a-zA-Z_][a-zA-Z0-9_]*$", RegexOptions.Compiled)]
private static partial Regex _nameRegex();
public override ValidationResults Validate(ExcelHeaderFile exh, Sheet sheet)
{
_sheetName = sheet.Name;
// I just don't have the brainpower to recurse right now
var flat = SchemaUtil.Flatten(exh, sheet);
var results = new ValidationResults();
foreach (var fieldName in flat.Select(d => d.Field.Name).Distinct())
{
if (string.IsNullOrEmpty(fieldName))
{
results.Add(ValidationResult.Error(_sheetName, ValidatorName(), "Field name is empty."));
continue;
}
if (!_nameRegex().IsMatch(fieldName))
results.Results.Add(ValidationResult.Error(_sheetName, ValidatorName(), $"Field name {fieldName} is not a valid name."));
}
if (results.Results.Count == 0)
return ValidationResults.Success(sheet.Name, ValidatorName());
return results;
}
public FieldNameValidator(GameData gameData) : base(gameData) { }
}