Add Dropdown values validation

This commit is contained in:
VladV 2023-04-05 21:14:35 +04:00
parent 69ebc4dc89
commit 6881e0fe00
4 changed files with 62 additions and 0 deletions

View File

@ -0,0 +1,52 @@
using TriInspector;
using TriInspector.Resolvers;
using TriInspector.Validators;
[assembly: RegisterTriAttributeValidator(typeof(DropdownValidator<>))]
namespace TriInspector.Validators
{
public class DropdownValidator<T> : TriAttributeValidator<DropdownAttribute>
{
private DropdownValuesResolver<T> _valuesResolver;
public override TriExtensionInitializationResult Initialize(TriPropertyDefinition propertyDefinition)
{
_valuesResolver = DropdownValuesResolver<T>.Resolve(propertyDefinition, Attribute.Values);
if (_valuesResolver.TryGetErrorString(out var error))
{
return error;
}
return TriExtensionInitializationResult.Ok;
}
public override TriValidationResult Validate(TriProperty property)
{
foreach (var item in _valuesResolver.GetDropdownItems(property))
{
if (property.Comparer.Equals(item.Value, property.Value))
{
return TriValidationResult.Valid;
}
}
var msg = $"Dropdown value '{property.Value}' not valid";
switch (Attribute.ValidationMessageType)
{
case TriMessageType.Info:
return TriValidationResult.Info(msg);
case TriMessageType.Warning:
return TriValidationResult.Warning(msg);
case TriMessageType.Error:
return TriValidationResult.Error(msg);
}
return TriValidationResult.Valid;
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 22bd6d63410f4a7ba8564e185844ddbf
timeCreated: 1680714123

View File

@ -9,6 +9,8 @@ namespace TriInspector
{
public string Values { get; }
public TriMessageType ValidationMessageType { get; set; } = TriMessageType.Error;
public DropdownAttribute(string values)
{
Values = values;

View File

@ -15,6 +15,11 @@
public string Message { get; }
public TriMessageType MessageType { get; }
public static TriValidationResult Info(string error)
{
return new TriValidationResult(false, error, TriMessageType.Info);
}
public static TriValidationResult Error(string error)
{
return new TriValidationResult(false, error, TriMessageType.Error);