Browse Source

Added validation and type for features.

pull/859/head
Halil ibrahim Kalkan 8 years ago
parent
commit
8f84e72ecf
  1. 1
      framework/src/Volo.Abp.Features/Volo.Abp.Features.csproj
  2. 4
      framework/src/Volo.Abp.Features/Volo/Abp/Features/AbpFeaturesModule.cs
  3. 20
      framework/src/Volo.Abp.Features/Volo/Abp/Features/FeatureDefinition.cs
  4. 3
      framework/src/Volo.Abp.Features/Volo/Abp/Features/FeatureGroupDefinition.cs
  5. 14
      framework/src/Volo.Abp.Validation/Volo/Abp/Validation/StringValues/AlwaysValidValueValidator.cs
  6. 24
      framework/src/Volo.Abp.Validation/Volo/Abp/Validation/StringValues/BooleanValueValidator.cs
  7. 19
      framework/src/Volo.Abp.Validation/Volo/Abp/Validation/StringValues/FreeTextStringValueType.cs
  8. 9
      framework/src/Volo.Abp.Validation/Volo/Abp/Validation/StringValues/ISelectionStringValueItem.cs
  9. 9
      framework/src/Volo.Abp.Validation/Volo/Abp/Validation/StringValues/ISelectionStringValueItemSource.cs
  10. 18
      framework/src/Volo.Abp.Validation/Volo/Abp/Validation/StringValues/IStringValueType.cs
  11. 18
      framework/src/Volo.Abp.Validation/Volo/Abp/Validation/StringValues/IValueValidator.cs
  12. 9
      framework/src/Volo.Abp.Validation/Volo/Abp/Validation/StringValues/LocalizableSelectionStringValueItem.cs
  13. 15
      framework/src/Volo.Abp.Validation/Volo/Abp/Validation/StringValues/LocalizableStringInfo.cs
  14. 61
      framework/src/Volo.Abp.Validation/Volo/Abp/Validation/StringValues/NumericValueValidator.cs
  15. 22
      framework/src/Volo.Abp.Validation/Volo/Abp/Validation/StringValues/SelectionStringValueType.cs
  16. 14
      framework/src/Volo.Abp.Validation/Volo/Abp/Validation/StringValues/StaticSelectionStringValueItemSource.cs
  17. 27
      framework/src/Volo.Abp.Validation/Volo/Abp/Validation/StringValues/StringValueTypeAttribute.cs
  18. 31
      framework/src/Volo.Abp.Validation/Volo/Abp/Validation/StringValues/StringValueTypeBase.cs
  19. 79
      framework/src/Volo.Abp.Validation/Volo/Abp/Validation/StringValues/StringValueValidator.cs
  20. 21
      framework/src/Volo.Abp.Validation/Volo/Abp/Validation/StringValues/ToggleStringValueType.cs
  21. 27
      framework/src/Volo.Abp.Validation/Volo/Abp/Validation/StringValues/ValueValidatorAttribute.cs
  22. 26
      framework/src/Volo.Abp.Validation/Volo/Abp/Validation/StringValues/ValueValidatorBase.cs

1
framework/src/Volo.Abp.Features/Volo.Abp.Features.csproj

@ -16,6 +16,7 @@
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\Volo.Abp.Localization.Abstractions\Volo.Abp.Localization.Abstractions.csproj" /> <ProjectReference Include="..\Volo.Abp.Localization.Abstractions\Volo.Abp.Localization.Abstractions.csproj" />
<ProjectReference Include="..\Volo.Abp.MultiTenancy.Abstractions\Volo.Abp.MultiTenancy.Abstractions.csproj" /> <ProjectReference Include="..\Volo.Abp.MultiTenancy.Abstractions\Volo.Abp.MultiTenancy.Abstractions.csproj" />
<ProjectReference Include="..\Volo.Abp.Validation\Volo.Abp.Validation.csproj" />
</ItemGroup> </ItemGroup>
</Project> </Project>

4
framework/src/Volo.Abp.Features/Volo/Abp/Features/AbpFeaturesModule.cs

@ -4,12 +4,14 @@ using System.Collections.Generic;
using Volo.Abp.Localization; using Volo.Abp.Localization;
using Volo.Abp.Modularity; using Volo.Abp.Modularity;
using Volo.Abp.MultiTenancy; using Volo.Abp.MultiTenancy;
using Volo.Abp.Validation;
namespace Volo.Abp.Features namespace Volo.Abp.Features
{ {
[DependsOn( [DependsOn(
typeof(AbpLocalizationAbstractionsModule), typeof(AbpLocalizationAbstractionsModule),
typeof(AbpMultiTenancyAbstractionsModule) typeof(AbpMultiTenancyAbstractionsModule),
typeof(AbpValidationModule)
)] )]
public class AbpFeaturesModule : AbpModule public class AbpFeaturesModule : AbpModule
{ {

20
framework/src/Volo.Abp.Features/Volo/Abp/Features/FeatureDefinition.cs

@ -3,6 +3,7 @@ using System.Collections.Immutable;
using System.Linq; using System.Linq;
using JetBrains.Annotations; using JetBrains.Annotations;
using Volo.Abp.Localization; using Volo.Abp.Localization;
using Volo.Abp.Validation.StringValues;
namespace Volo.Abp.Features namespace Volo.Abp.Features
{ {
@ -29,6 +30,7 @@ namespace Volo.Abp.Features
/// Parent of this feature, if one exists. /// Parent of this feature, if one exists.
/// If set, this feature can be enabled only if the parent is enabled. /// If set, this feature can be enabled only if the parent is enabled.
/// </summary> /// </summary>
[CanBeNull]
public FeatureDefinition Parent { get; private set; } public FeatureDefinition Parent { get; private set; }
/// <summary> /// <summary>
@ -53,6 +55,7 @@ namespace Volo.Abp.Features
/// A list of allowed providers to get/set value of this feature. /// A list of allowed providers to get/set value of this feature.
/// An empty list indicates that all providers are allowed. /// An empty list indicates that all providers are allowed.
/// </summary> /// </summary>
[NotNull]
public List<string> AllowedProviders { get; } public List<string> AllowedProviders { get; }
/// <summary> /// <summary>
@ -63,6 +66,7 @@ namespace Volo.Abp.Features
/// Returns the value in the <see cref="Properties"/> dictionary by given <see cref="name"/>. /// Returns the value in the <see cref="Properties"/> dictionary by given <see cref="name"/>.
/// Returns null if given <see cref="name"/> is not present in the <see cref="Properties"/> dictionary. /// Returns null if given <see cref="name"/> is not present in the <see cref="Properties"/> dictionary.
/// </returns> /// </returns>
[CanBeNull]
public object this[string name] public object this[string name]
{ {
get => Properties.GetOrDefault(name); get => Properties.GetOrDefault(name);
@ -75,20 +79,28 @@ namespace Volo.Abp.Features
[NotNull] [NotNull]
public Dictionary<string, object> Properties { get; } public Dictionary<string, object> Properties { get; }
//TODO: Implement input type like old ABP! /// <summary>
/// Input type.
/// This can be used to prepare an input for changing this feature's value.
/// Default: <see cref="ToggleStringValueType"/>.
/// </summary>
[CanBeNull]
public IStringValueType ValueType { get; set; }
public FeatureDefinition( public FeatureDefinition(
string name, string name,
string defaultValue = null, string defaultValue = null,
ILocalizableString displayName = null, ILocalizableString displayName = null,
ILocalizableString description = null, ILocalizableString description = null,
IStringValueType valueType = null,
bool isVisibleToClients = true) bool isVisibleToClients = true)
{ {
Name = name; Name = name;
DefaultValue = defaultValue; DefaultValue = defaultValue;
IsVisibleToClients = isVisibleToClients;
DisplayName = displayName ?? new FixedLocalizableString(name); DisplayName = displayName ?? new FixedLocalizableString(name);
Description = description; Description = description;
ValueType = valueType;
IsVisibleToClients = isVisibleToClients;
Properties = new Dictionary<string, object>(); Properties = new Dictionary<string, object>();
AllowedProviders = new List<string>(); AllowedProviders = new List<string>();
@ -128,13 +140,15 @@ namespace Volo.Abp.Features
string defaultValue = null, string defaultValue = null,
ILocalizableString displayName = null, ILocalizableString displayName = null,
ILocalizableString description = null, ILocalizableString description = null,
IStringValueType valueType = null,
bool isVisibleToClients = true) bool isVisibleToClients = true)
{ {
var feature = new FeatureDefinition( var feature = new FeatureDefinition(
name, name,
defaultValue, defaultValue,
displayName, displayName,
description, description,
valueType,
isVisibleToClients) isVisibleToClients)
{ {
Parent = this Parent = this

3
framework/src/Volo.Abp.Features/Volo/Abp/Features/FeatureGroupDefinition.cs

@ -1,6 +1,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.Immutable; using System.Collections.Immutable;
using Volo.Abp.Localization; using Volo.Abp.Localization;
using Volo.Abp.Validation.StringValues;
namespace Volo.Abp.Features namespace Volo.Abp.Features
{ {
@ -53,6 +54,7 @@ namespace Volo.Abp.Features
string defaultValue = null, string defaultValue = null,
ILocalizableString displayName = null, ILocalizableString displayName = null,
ILocalizableString description = null, ILocalizableString description = null,
IStringValueType valueType = null,
bool isVisibleToClients = true) bool isVisibleToClients = true)
{ {
var feature = new FeatureDefinition( var feature = new FeatureDefinition(
@ -60,6 +62,7 @@ namespace Volo.Abp.Features
defaultValue, defaultValue,
displayName, displayName,
description, description,
valueType,
isVisibleToClients isVisibleToClients
); );

14
framework/src/Volo.Abp.Validation/Volo/Abp/Validation/StringValues/AlwaysValidValueValidator.cs

@ -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;
}
}
}

24
framework/src/Volo.Abp.Validation/Volo/Abp/Validation/StringValues/BooleanValueValidator.cs

@ -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 _);
}
}
}

19
framework/src/Volo.Abp.Validation/Volo/Abp/Validation/StringValues/FreeTextStringValueType.cs

@ -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)
{
}
}
}

9
framework/src/Volo.Abp.Validation/Volo/Abp/Validation/StringValues/ISelectionStringValueItem.cs

@ -0,0 +1,9 @@
namespace Volo.Abp.Validation.StringValues
{
public interface ISelectionStringValueItem
{
string Value { get; set; }
LocalizableStringInfo DisplayText { get; set; }
}
}

9
framework/src/Volo.Abp.Validation/Volo/Abp/Validation/StringValues/ISelectionStringValueItemSource.cs

@ -0,0 +1,9 @@
using System.Collections.Generic;
namespace Volo.Abp.Validation.StringValues
{
public interface ISelectionStringValueItemSource
{
ICollection<ISelectionStringValueItem> Items { get; }
}
}

18
framework/src/Volo.Abp.Validation/Volo/Abp/Validation/StringValues/IStringValueType.cs

@ -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; }
}
}

18
framework/src/Volo.Abp.Validation/Volo/Abp/Validation/StringValues/IValueValidator.cs

@ -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);
}
}

9
framework/src/Volo.Abp.Validation/Volo/Abp/Validation/StringValues/LocalizableSelectionStringValueItem.cs

@ -0,0 +1,9 @@
namespace Volo.Abp.Validation.StringValues
{
public class LocalizableSelectionStringValueItem : ISelectionStringValueItem
{
public string Value { get; set; }
public LocalizableStringInfo DisplayText { get; set; }
}
}

15
framework/src/Volo.Abp.Validation/Volo/Abp/Validation/StringValues/LocalizableStringInfo.cs

@ -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;
}
}
}

61
framework/src/Volo.Abp.Validation/Volo/Abp/Validation/StringValues/NumericValueValidator.cs

@ -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);
}
}
}

22
framework/src/Volo.Abp.Validation/Volo/Abp/Validation/StringValues/SelectionStringValueType.cs

@ -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)
{
}
}
}

14
framework/src/Volo.Abp.Validation/Volo/Abp/Validation/StringValues/StaticSelectionStringValueItemSource.cs

@ -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));
}
}
}

27
framework/src/Volo.Abp.Validation/Volo/Abp/Validation/StringValues/StringValueTypeAttribute.cs

@ -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;
}
}
}

31
framework/src/Volo.Abp.Validation/Volo/Abp/Validation/StringValues/StringValueTypeBase.cs

@ -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>();
}
}
}

79
framework/src/Volo.Abp.Validation/Volo/Abp/Validation/StringValues/StringValueValidator.cs

@ -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;
}
}
}

21
framework/src/Volo.Abp.Validation/Volo/Abp/Validation/StringValues/ToggleStringValueType.cs

@ -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)
{
}
}
}

27
framework/src/Volo.Abp.Validation/Volo/Abp/Validation/StringValues/ValueValidatorAttribute.cs

@ -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;
}
}
}

26
framework/src/Volo.Abp.Validation/Volo/Abp/Validation/StringValues/ValueValidatorBase.cs

@ -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…
Cancel
Save