Validation work
This commit is contained in:
@@ -0,0 +1,11 @@
|
|||||||
|
using System.Diagnostics.CodeAnalysis;
|
||||||
|
|
||||||
|
namespace Toolkit.Foundation;
|
||||||
|
|
||||||
|
public interface IValidatorCollection :
|
||||||
|
IReadOnlyCollection<Validator>
|
||||||
|
{
|
||||||
|
void Add(string key, Validator binder);
|
||||||
|
|
||||||
|
bool TryGet(string key, [MaybeNull] out Validator? value);
|
||||||
|
}
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
namespace Toolkit.Foundation;
|
||||||
|
|
||||||
|
public class PropertyValidator
|
||||||
|
{
|
||||||
|
public PropertyValidator(Func<bool> validation,
|
||||||
|
string message)
|
||||||
|
{
|
||||||
|
Validation = validation;
|
||||||
|
Message = new Func<string>(() => message);
|
||||||
|
}
|
||||||
|
|
||||||
|
public PropertyValidator(Func<bool> validation,
|
||||||
|
Func<string> message)
|
||||||
|
{
|
||||||
|
Validation = validation;
|
||||||
|
Message = message;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Func<string> Message { get; }
|
||||||
|
|
||||||
|
public Func<bool>? Validation { get; }
|
||||||
|
}
|
||||||
@@ -0,0 +1,154 @@
|
|||||||
|
using System.Collections.Specialized;
|
||||||
|
using System.Collections;
|
||||||
|
using System.ComponentModel;
|
||||||
|
using System.Diagnostics.CodeAnalysis;
|
||||||
|
|
||||||
|
namespace Toolkit.Foundation;
|
||||||
|
|
||||||
|
public class ValidationErrorCollection : IDictionary<string, string>,
|
||||||
|
IDictionary,
|
||||||
|
INotifyCollectionChanged,
|
||||||
|
INotifyPropertyChanged
|
||||||
|
{
|
||||||
|
private Dictionary<string, string> items;
|
||||||
|
|
||||||
|
public ValidationErrorCollection()
|
||||||
|
{
|
||||||
|
items = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
public event NotifyCollectionChangedEventHandler? CollectionChanged;
|
||||||
|
|
||||||
|
public event PropertyChangedEventHandler? PropertyChanged;
|
||||||
|
|
||||||
|
public int Count => items.Count;
|
||||||
|
|
||||||
|
bool IDictionary.IsFixedSize => ((IDictionary)items).IsFixedSize;
|
||||||
|
|
||||||
|
public bool IsReadOnly => false;
|
||||||
|
|
||||||
|
bool ICollection.IsSynchronized => ((IDictionary)items).IsSynchronized;
|
||||||
|
|
||||||
|
public ICollection<string> Keys => items.Keys;
|
||||||
|
|
||||||
|
ICollection IDictionary.Keys => ((IDictionary)items).Keys;
|
||||||
|
|
||||||
|
object ICollection.SyncRoot => ((IDictionary)items).SyncRoot;
|
||||||
|
|
||||||
|
public ICollection<string> Values => items.Values;
|
||||||
|
|
||||||
|
ICollection IDictionary.Values => ((IDictionary)items).Values;
|
||||||
|
|
||||||
|
public string this[string key]
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return items[key];
|
||||||
|
}
|
||||||
|
|
||||||
|
set
|
||||||
|
{
|
||||||
|
bool replace = items.TryGetValue(key, out var old);
|
||||||
|
items[key] = value;
|
||||||
|
|
||||||
|
if (replace)
|
||||||
|
{
|
||||||
|
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs($"Item[{key}]"));
|
||||||
|
CollectionChanged?.Invoke(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Replace, new KeyValuePair<string, string>(key, value), new KeyValuePair<string, string>(key, old!)));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
NotifyAdd(key, value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
object? IDictionary.this[object key]
|
||||||
|
{
|
||||||
|
get => ((IDictionary)items)[key];
|
||||||
|
set => ((IDictionary)items)[key] = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Add(string key, string value)
|
||||||
|
{
|
||||||
|
items.Add(key, value);
|
||||||
|
NotifyAdd(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ICollection<KeyValuePair<string, string>>.Add(KeyValuePair<string, string> item) =>
|
||||||
|
Add(item.Key, item.Value);
|
||||||
|
|
||||||
|
void IDictionary.Add(object key, object? value) =>
|
||||||
|
Add((string)key, (string)value!);
|
||||||
|
|
||||||
|
public void Clear()
|
||||||
|
{
|
||||||
|
Dictionary<string, string> old = items;
|
||||||
|
items = [];
|
||||||
|
|
||||||
|
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Count)));
|
||||||
|
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Item"));
|
||||||
|
|
||||||
|
CollectionChanged?.Invoke(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, old.ToArray(), -1));
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ICollection<KeyValuePair<string, string>>.Contains(KeyValuePair<string, string> item) =>
|
||||||
|
items.Contains(item);
|
||||||
|
|
||||||
|
bool IDictionary.Contains(object key) =>
|
||||||
|
((IDictionary)items).Contains(key);
|
||||||
|
|
||||||
|
public bool ContainsKey(string key) => items.ContainsKey(key);
|
||||||
|
|
||||||
|
public void CopyTo(KeyValuePair<string, string>[] array, int arrayIndex) =>
|
||||||
|
((IDictionary<string, string>)items).CopyTo(array, arrayIndex);
|
||||||
|
|
||||||
|
void ICollection.CopyTo(Array array, int index) =>
|
||||||
|
((ICollection)items).CopyTo(array, index);
|
||||||
|
|
||||||
|
public IEnumerator<KeyValuePair<string, string>> GetEnumerator() =>
|
||||||
|
items.GetEnumerator();
|
||||||
|
|
||||||
|
IEnumerator IEnumerable.GetEnumerator() =>
|
||||||
|
items.GetEnumerator();
|
||||||
|
|
||||||
|
IDictionaryEnumerator IDictionary.GetEnumerator() =>
|
||||||
|
((IDictionary)items).GetEnumerator();
|
||||||
|
|
||||||
|
public bool Remove(string key)
|
||||||
|
{
|
||||||
|
if (items.TryGetValue(key, out var value))
|
||||||
|
{
|
||||||
|
items.Remove(key);
|
||||||
|
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Count)));
|
||||||
|
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs($"Item[{key}]"));
|
||||||
|
|
||||||
|
CollectionChanged?.Invoke(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, new[] { new KeyValuePair<string, string>(key, value) }, -1));
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool Contains(string key) =>
|
||||||
|
items.ContainsKey(key);
|
||||||
|
|
||||||
|
bool ICollection<KeyValuePair<string, string>>.Remove(KeyValuePair<string, string> item) =>
|
||||||
|
Remove(item.Key);
|
||||||
|
|
||||||
|
void IDictionary.Remove(object key) => Remove((string)key);
|
||||||
|
|
||||||
|
public bool TryGetValue(string key, [MaybeNullWhen(false)] out string value) => items.TryGetValue(key, out value);
|
||||||
|
|
||||||
|
private void NotifyAdd(string key, string value)
|
||||||
|
{
|
||||||
|
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Count)));
|
||||||
|
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs($"Item[{key}]"));
|
||||||
|
|
||||||
|
CollectionChanged?.Invoke(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add,
|
||||||
|
new[] { new KeyValuePair<string, string>(key, value) }, -1));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,51 @@
|
|||||||
|
using System.Diagnostics.CodeAnalysis;
|
||||||
|
|
||||||
|
namespace Toolkit.Foundation;
|
||||||
|
|
||||||
|
public class Validator
|
||||||
|
{
|
||||||
|
private readonly Action? propertyChanged;
|
||||||
|
private readonly PropertyValidator? propertyValidation;
|
||||||
|
|
||||||
|
internal Validator(string propertyName,
|
||||||
|
Action propertyChanged)
|
||||||
|
{
|
||||||
|
PropertyName = propertyName;
|
||||||
|
this.propertyChanged = propertyChanged;
|
||||||
|
}
|
||||||
|
|
||||||
|
internal Validator(string propertyName,
|
||||||
|
Action propertyChanged,
|
||||||
|
PropertyValidator validation)
|
||||||
|
{
|
||||||
|
PropertyName = propertyName;
|
||||||
|
|
||||||
|
this.propertyChanged = propertyChanged;
|
||||||
|
propertyValidation = validation;
|
||||||
|
}
|
||||||
|
|
||||||
|
internal Validator(string propertyName,
|
||||||
|
PropertyValidator validation)
|
||||||
|
{
|
||||||
|
PropertyName = propertyName;
|
||||||
|
propertyValidation = validation;
|
||||||
|
}
|
||||||
|
|
||||||
|
public string? PropertyName { get; }
|
||||||
|
|
||||||
|
public void Set() => propertyChanged?.Invoke();
|
||||||
|
|
||||||
|
public bool TryValidate([MaybeNull] out string message)
|
||||||
|
{
|
||||||
|
message = "";
|
||||||
|
|
||||||
|
if (propertyValidation is not null && propertyValidation.Validation?.Invoke() == false)
|
||||||
|
{
|
||||||
|
message = propertyValidation.Message.Invoke();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
propertyChanged?.Invoke();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
using System.Diagnostics.CodeAnalysis;
|
||||||
|
using System.Collections;
|
||||||
|
|
||||||
|
namespace Toolkit.Foundation;
|
||||||
|
|
||||||
|
public class ValidatorCollection :
|
||||||
|
IValidatorCollection
|
||||||
|
{
|
||||||
|
private readonly Dictionary<string, Validator> binders = [];
|
||||||
|
|
||||||
|
public int Count => binders.Count;
|
||||||
|
|
||||||
|
public void Add(string key, Validator binder) =>
|
||||||
|
binders.Add(key, binder);
|
||||||
|
|
||||||
|
public IEnumerator<Validator> GetEnumerator() =>
|
||||||
|
binders.Select(x => x.Value).GetEnumerator();
|
||||||
|
|
||||||
|
public bool TryGet(string key, [MaybeNull] out Validator? value) =>
|
||||||
|
binders.TryGetValue(key, out value);
|
||||||
|
|
||||||
|
IEnumerator IEnumerable.GetEnumerator() =>
|
||||||
|
binders.Select(x => x.Value).GetEnumerator();
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user