mirror of https://github.com/abpframework/abp.git
22 changed files with 467 additions and 4 deletions
@ -0,0 +1,14 @@ |
|||||
|
using System; |
||||
|
|
||||
|
namespace Volo.Abp.Validation.StringValues |
||||
|
{ |
||||
|
[Serializable] |
||||
|
[ValueValidator("NULL")] |
||||
|
public class AlwaysValidValueValidator : ValueValidatorBase |
||||
|
{ |
||||
|
public override bool IsValid(object value) |
||||
|
{ |
||||
|
return true; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,24 @@ |
|||||
|
using System; |
||||
|
|
||||
|
namespace Volo.Abp.Validation.StringValues |
||||
|
{ |
||||
|
[Serializable] |
||||
|
[ValueValidator("BOOLEAN")] |
||||
|
public class BooleanValueValidator : ValueValidatorBase |
||||
|
{ |
||||
|
public override bool IsValid(object value) |
||||
|
{ |
||||
|
if (value == null) |
||||
|
{ |
||||
|
return false; |
||||
|
} |
||||
|
|
||||
|
if (value is bool) |
||||
|
{ |
||||
|
return true; |
||||
|
} |
||||
|
|
||||
|
return bool.TryParse(value.ToString(), out _); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,19 @@ |
|||||
|
using System; |
||||
|
|
||||
|
namespace Volo.Abp.Validation.StringValues |
||||
|
{ |
||||
|
[Serializable] |
||||
|
[StringValueType("FREE_TEXT")] |
||||
|
public class FreeTextStringValueType : StringValueTypeBase |
||||
|
{ |
||||
|
public FreeTextStringValueType() |
||||
|
{ |
||||
|
|
||||
|
} |
||||
|
|
||||
|
public FreeTextStringValueType(IValueValidator validator) |
||||
|
: base(validator) |
||||
|
{ |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,9 @@ |
|||||
|
namespace Volo.Abp.Validation.StringValues |
||||
|
{ |
||||
|
public interface ISelectionStringValueItem |
||||
|
{ |
||||
|
string Value { get; set; } |
||||
|
|
||||
|
LocalizableStringInfo DisplayText { get; set; } |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,9 @@ |
|||||
|
using System.Collections.Generic; |
||||
|
|
||||
|
namespace Volo.Abp.Validation.StringValues |
||||
|
{ |
||||
|
public interface ISelectionStringValueItemSource |
||||
|
{ |
||||
|
ICollection<ISelectionStringValueItem> Items { get; } |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,18 @@ |
|||||
|
using System.Collections.Generic; |
||||
|
using JetBrains.Annotations; |
||||
|
|
||||
|
namespace Volo.Abp.Validation.StringValues |
||||
|
{ |
||||
|
public interface IStringValueType |
||||
|
{ |
||||
|
string Name { get; } |
||||
|
|
||||
|
[CanBeNull] |
||||
|
object this[string key] { get; set; } |
||||
|
|
||||
|
[NotNull] |
||||
|
Dictionary<string, object> Properties { get; } |
||||
|
|
||||
|
IValueValidator Validator { get; set; } |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,18 @@ |
|||||
|
using System.Collections.Generic; |
||||
|
using JetBrains.Annotations; |
||||
|
|
||||
|
namespace Volo.Abp.Validation.StringValues |
||||
|
{ |
||||
|
public interface IValueValidator |
||||
|
{ |
||||
|
string Name { get; } |
||||
|
|
||||
|
[CanBeNull] |
||||
|
object this[string key] { get; set; } |
||||
|
|
||||
|
[NotNull] |
||||
|
IDictionary<string, object> Properties { get; } |
||||
|
|
||||
|
bool IsValid(object value); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,9 @@ |
|||||
|
namespace Volo.Abp.Validation.StringValues |
||||
|
{ |
||||
|
public class LocalizableSelectionStringValueItem : ISelectionStringValueItem |
||||
|
{ |
||||
|
public string Value { get; set; } |
||||
|
|
||||
|
public LocalizableStringInfo DisplayText { get; set; } |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,15 @@ |
|||||
|
namespace Volo.Abp.Validation.StringValues |
||||
|
{ |
||||
|
public class LocalizableStringInfo |
||||
|
{ |
||||
|
public string ResourceName { get; } |
||||
|
|
||||
|
public string Name { get; } |
||||
|
|
||||
|
public LocalizableStringInfo(string resourceName, string name) |
||||
|
{ |
||||
|
ResourceName = resourceName; |
||||
|
Name = name; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,61 @@ |
|||||
|
using System; |
||||
|
|
||||
|
namespace Volo.Abp.Validation.StringValues |
||||
|
{ |
||||
|
[Serializable] |
||||
|
[ValueValidator("NUMERIC")] |
||||
|
public class NumericValueValidator : ValueValidatorBase |
||||
|
{ |
||||
|
public int MinValue |
||||
|
{ |
||||
|
get => (this["MinValue"] ?? "0").To<int>(); |
||||
|
set => this["MinValue"] = value; |
||||
|
} |
||||
|
|
||||
|
public int MaxValue |
||||
|
{ |
||||
|
get => (this["MaxValue"] ?? "0").To<int>(); |
||||
|
set => this["MaxValue"] = value; |
||||
|
} |
||||
|
|
||||
|
public NumericValueValidator() |
||||
|
{ |
||||
|
|
||||
|
} |
||||
|
|
||||
|
public NumericValueValidator(int minValue = int.MinValue, int maxValue = int.MaxValue) |
||||
|
{ |
||||
|
MinValue = minValue; |
||||
|
MaxValue = maxValue; |
||||
|
} |
||||
|
|
||||
|
public override bool IsValid(object value) |
||||
|
{ |
||||
|
if (value == null) |
||||
|
{ |
||||
|
return false; |
||||
|
} |
||||
|
|
||||
|
if (value is int) |
||||
|
{ |
||||
|
return IsValidInternal((int)value); |
||||
|
} |
||||
|
|
||||
|
if (value is string) |
||||
|
{ |
||||
|
int intValue; |
||||
|
if (int.TryParse(value as string, out intValue)) |
||||
|
{ |
||||
|
return IsValidInternal(intValue); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
return false; |
||||
|
} |
||||
|
|
||||
|
protected virtual bool IsValidInternal(int value) |
||||
|
{ |
||||
|
return value.IsBetween(MinValue, MaxValue); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,22 @@ |
|||||
|
using System; |
||||
|
|
||||
|
namespace Volo.Abp.Validation.StringValues |
||||
|
{ |
||||
|
[Serializable] |
||||
|
[StringValueType("SELECTION")] |
||||
|
public class SelectionStringValueType : StringValueTypeBase |
||||
|
{ |
||||
|
public ISelectionStringValueItemSource ItemSource { get; set; } |
||||
|
|
||||
|
public SelectionStringValueType() |
||||
|
{ |
||||
|
|
||||
|
} |
||||
|
|
||||
|
public SelectionStringValueType(IValueValidator validator) |
||||
|
: base(validator) |
||||
|
{ |
||||
|
|
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,14 @@ |
|||||
|
using System.Collections.Generic; |
||||
|
|
||||
|
namespace Volo.Abp.Validation.StringValues |
||||
|
{ |
||||
|
public class StaticSelectionStringValueItemSource : ISelectionStringValueItemSource |
||||
|
{ |
||||
|
public ICollection<ISelectionStringValueItem> Items { get; } |
||||
|
|
||||
|
public StaticSelectionStringValueItemSource(params ISelectionStringValueItem[] items) |
||||
|
{ |
||||
|
Items = Check.NotNullOrEmpty(items, nameof(items)); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,27 @@ |
|||||
|
using System; |
||||
|
using System.Linq; |
||||
|
using System.Reflection; |
||||
|
|
||||
|
namespace Volo.Abp.Validation.StringValues |
||||
|
{ |
||||
|
[AttributeUsage(AttributeTargets.Class)] |
||||
|
public class StringValueTypeAttribute : Attribute |
||||
|
{ |
||||
|
public string Name { get; set; } |
||||
|
|
||||
|
public StringValueTypeAttribute(string name) |
||||
|
{ |
||||
|
Name = name; |
||||
|
} |
||||
|
|
||||
|
public static string GetName(Type type) |
||||
|
{ |
||||
|
if (type.IsDefined(typeof(StringValueTypeAttribute))) |
||||
|
{ |
||||
|
return type.GetCustomAttributes(typeof(StringValueTypeAttribute)).Cast<StringValueTypeAttribute>().First().Name; |
||||
|
} |
||||
|
|
||||
|
return type.Name; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,31 @@ |
|||||
|
using System.Collections.Generic; |
||||
|
|
||||
|
namespace Volo.Abp.Validation.StringValues |
||||
|
{ |
||||
|
public abstract class StringValueTypeBase : IStringValueType |
||||
|
{ |
||||
|
public virtual string Name => ValueValidatorAttribute.GetName(GetType()); |
||||
|
|
||||
|
public object this[string key] |
||||
|
{ |
||||
|
get => Properties.GetOrDefault(key); |
||||
|
set => Properties[key] = value; |
||||
|
} |
||||
|
|
||||
|
public Dictionary<string, object> Properties { get; } |
||||
|
|
||||
|
public IValueValidator Validator { get; set; } |
||||
|
|
||||
|
protected StringValueTypeBase() |
||||
|
: this(new AlwaysValidValueValidator()) |
||||
|
{ |
||||
|
|
||||
|
} |
||||
|
|
||||
|
protected StringValueTypeBase(IValueValidator validator) |
||||
|
{ |
||||
|
Validator = validator; |
||||
|
Properties = new Dictionary<string, object>(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,79 @@ |
|||||
|
using System; |
||||
|
using System.Text.RegularExpressions; |
||||
|
|
||||
|
namespace Volo.Abp.Validation.StringValues |
||||
|
{ |
||||
|
[Serializable] |
||||
|
[ValueValidator("STRING")] |
||||
|
public class StringValueValidator : ValueValidatorBase |
||||
|
{ |
||||
|
public bool AllowNull |
||||
|
{ |
||||
|
get => (this["AllowNull"] ?? "false").To<bool>(); |
||||
|
set => this["AllowNull"] = value.ToString().ToLowerInvariant(); |
||||
|
} |
||||
|
|
||||
|
public int MinLength |
||||
|
{ |
||||
|
get => (this["MinLength"] ?? "0").To<int>(); |
||||
|
set => this["MinLength"] = value; |
||||
|
} |
||||
|
|
||||
|
public int MaxLength |
||||
|
{ |
||||
|
get => (this["MaxLength"] ?? "0").To<int>(); |
||||
|
set => this["MaxLength"] = value; |
||||
|
} |
||||
|
|
||||
|
public string RegularExpression |
||||
|
{ |
||||
|
get => this["RegularExpression"] as string; |
||||
|
set => this["RegularExpression"] = value; |
||||
|
} |
||||
|
|
||||
|
public StringValueValidator() |
||||
|
{ |
||||
|
|
||||
|
} |
||||
|
|
||||
|
public StringValueValidator(int minLength = 0, int maxLength = 0, string regularExpression = null, bool allowNull = false) |
||||
|
{ |
||||
|
MinLength = minLength; |
||||
|
MaxLength = maxLength; |
||||
|
RegularExpression = regularExpression; |
||||
|
AllowNull = allowNull; |
||||
|
} |
||||
|
|
||||
|
public override bool IsValid(object value) |
||||
|
{ |
||||
|
if (value == null) |
||||
|
{ |
||||
|
return AllowNull; |
||||
|
} |
||||
|
|
||||
|
if (!(value is string)) |
||||
|
{ |
||||
|
return false; |
||||
|
} |
||||
|
|
||||
|
var strValue = value as string; |
||||
|
|
||||
|
if (MinLength > 0 && strValue.Length < MinLength) |
||||
|
{ |
||||
|
return false; |
||||
|
} |
||||
|
|
||||
|
if (MaxLength > 0 && strValue.Length > MaxLength) |
||||
|
{ |
||||
|
return false; |
||||
|
} |
||||
|
|
||||
|
if (!RegularExpression.IsNullOrEmpty()) |
||||
|
{ |
||||
|
return Regex.IsMatch(strValue, RegularExpression); |
||||
|
} |
||||
|
|
||||
|
return true; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,21 @@ |
|||||
|
using System; |
||||
|
|
||||
|
namespace Volo.Abp.Validation.StringValues |
||||
|
{ |
||||
|
[Serializable] |
||||
|
[StringValueType("TOGGLE")] |
||||
|
public class ToggleStringValueType : StringValueTypeBase |
||||
|
{ |
||||
|
public ToggleStringValueType() |
||||
|
: this(new BooleanValueValidator()) |
||||
|
{ |
||||
|
|
||||
|
} |
||||
|
|
||||
|
public ToggleStringValueType(IValueValidator validator) |
||||
|
: base(validator) |
||||
|
{ |
||||
|
|
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,27 @@ |
|||||
|
using System; |
||||
|
using System.Linq; |
||||
|
using System.Reflection; |
||||
|
|
||||
|
namespace Volo.Abp.Validation.StringValues |
||||
|
{ |
||||
|
[AttributeUsage(AttributeTargets.Class)] |
||||
|
public class ValueValidatorAttribute : Attribute |
||||
|
{ |
||||
|
public string Name { get; set; } |
||||
|
|
||||
|
public ValueValidatorAttribute(string name) |
||||
|
{ |
||||
|
Name = name; |
||||
|
} |
||||
|
|
||||
|
public static string GetName(Type type) |
||||
|
{ |
||||
|
if (type.IsDefined(typeof(ValueValidatorAttribute))) |
||||
|
{ |
||||
|
return type.GetCustomAttributes(typeof(ValueValidatorAttribute)).Cast<ValueValidatorAttribute>().First().Name; |
||||
|
} |
||||
|
|
||||
|
return type.Name; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,26 @@ |
|||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
|
||||
|
namespace Volo.Abp.Validation.StringValues |
||||
|
{ |
||||
|
[Serializable] |
||||
|
public abstract class ValueValidatorBase : IValueValidator |
||||
|
{ |
||||
|
public virtual string Name => ValueValidatorAttribute.GetName(GetType()); |
||||
|
|
||||
|
public object this[string key] |
||||
|
{ |
||||
|
get => Properties.GetOrDefault(key); |
||||
|
set => Properties[key] = value; |
||||
|
} |
||||
|
|
||||
|
public IDictionary<string, object> Properties { get; } |
||||
|
|
||||
|
protected ValueValidatorBase() |
||||
|
{ |
||||
|
Properties = new Dictionary<string, object>(); |
||||
|
} |
||||
|
|
||||
|
public abstract bool IsValid(object value); |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue