mirror of
https://github.com/codewriter-packages/Tri-Inspector.git
synced 2025-01-22 08:18:49 -05:00
Add Dropdown values validation
This commit is contained in:
parent
69ebc4dc89
commit
6881e0fe00
52
Editor.Extras/Validators/DropdownValidator.cs
Normal file
52
Editor.Extras/Validators/DropdownValidator.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
3
Editor.Extras/Validators/DropdownValidator.cs.meta
Normal file
3
Editor.Extras/Validators/DropdownValidator.cs.meta
Normal file
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 22bd6d63410f4a7ba8564e185844ddbf
|
||||
timeCreated: 1680714123
|
@ -9,6 +9,8 @@ namespace TriInspector
|
||||
{
|
||||
public string Values { get; }
|
||||
|
||||
public TriMessageType ValidationMessageType { get; set; } = TriMessageType.Error;
|
||||
|
||||
public DropdownAttribute(string values)
|
||||
{
|
||||
Values = values;
|
||||
|
@ -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);
|
||||
|
Loading…
Reference in New Issue
Block a user