Browse Source

initial

pull/4524/head
Ahmet Çotur 6 years ago
parent
commit
4d3a54f4a3
  1. 5
      framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/DataAnnotations/AbpValidationAttributeAdapterProvider.cs
  2. 45
      framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/DataAnnotations/DynamicMaxLengthAttributeAdapter.cs
  3. 39
      framework/src/Volo.Abp.Core/Volo/Abp/Validation/DynamicMaxLengthAttribute.cs
  4. 11
      framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/Validation/ValidationTestController.cs
  5. 9
      framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/Validation/ValidationTestController_Tests.cs

5
framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/DataAnnotations/AbpValidationAttributeAdapterProvider.cs

@ -22,6 +22,11 @@ namespace Volo.Abp.AspNetCore.Mvc.DataAnnotations
{
return new DynamicStringLengthAttributeAdapter((DynamicStringLengthAttribute) attribute, stringLocalizer);
}
if (type == typeof(DynamicMaxLengthAttribute))
{
return new DynamicMaxLengthAttributeAdapter((DynamicMaxLengthAttribute) attribute, stringLocalizer);
}
return _defaultAdapter.GetAttributeAdapter(attribute, stringLocalizer);
}

45
framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/DataAnnotations/DynamicMaxLengthAttributeAdapter.cs

@ -0,0 +1,45 @@
using System.Globalization;
using Microsoft.AspNetCore.Mvc.DataAnnotations;
using Microsoft.AspNetCore.Mvc.ModelBinding.Validation;
using Microsoft.Extensions.Localization;
using Volo.Abp.Validation;
namespace Volo.Abp.AspNetCore.Mvc.DataAnnotations
{
public class DynamicMaxLengthAttributeAdapter : AttributeAdapterBase<DynamicMaxLengthAttribute>
{
private readonly string _max;
public DynamicMaxLengthAttributeAdapter(
DynamicMaxLengthAttribute attribute,
IStringLocalizer stringLocalizer)
: base(attribute, stringLocalizer)
{
_max = Attribute.Length.ToString(CultureInfo.InvariantCulture);
}
public override string GetErrorMessage(ModelValidationContextBase validationContext)
{
Check.NotNull(validationContext, nameof(validationContext));
return GetErrorMessage(
validationContext.ModelMetadata,
validationContext.ModelMetadata.GetDisplayName(),
Attribute.Length
);
}
public override void AddValidation(ClientModelValidationContext context)
{
Check.NotNull(context, nameof(context));
MergeAttribute(context.Attributes, "data-val", "true");
MergeAttribute(context.Attributes, "data-val-length", GetErrorMessage(context));
if (Attribute.Length != int.MaxValue)
{
MergeAttribute(context.Attributes, "data-val-length-max", _max);
}
}
}
}

39
framework/src/Volo.Abp.Core/Volo/Abp/Validation/DynamicMaxLengthAttribute.cs

@ -0,0 +1,39 @@
using System;
using System.ComponentModel.DataAnnotations;
using System.Diagnostics;
using System.Reflection;
using JetBrains.Annotations;
namespace Volo.Abp.Validation
{
public class DynamicMaxLengthAttribute : MaxLengthAttribute
{
private static readonly FieldInfo MaximumLengthField;
static DynamicMaxLengthAttribute()
{
MaximumLengthField = typeof(MaxLengthAttribute).GetField(
"<MaximumLength>k__BackingField",
BindingFlags.Instance | BindingFlags.NonPublic
);
Debug.Assert(MaximumLengthField != null, nameof(MaximumLengthField) + " != null");
}
public DynamicMaxLengthAttribute(
[NotNull] Type sourceType,
[CanBeNull] string maximumLengthPropertyName)
{
Check.NotNull(sourceType, nameof(sourceType));
if (maximumLengthPropertyName != null)
{
var maximumLengthProperty = sourceType.GetProperty(
maximumLengthPropertyName,
BindingFlags.Static | BindingFlags.Public
);
Debug.Assert(maximumLengthProperty != null, nameof(maximumLengthProperty) + " != null");
MaximumLengthField.SetValue(this, (int) maximumLengthProperty.GetValue(null));
}
}
}
}

11
framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/Validation/ValidationTestController.cs

@ -50,13 +50,18 @@ namespace Volo.Abp.AspNetCore.Mvc.Validation
public class ValidationDynamicTestModel
{
[DynamicStringLength(typeof(Consts), nameof(Consts.MaxValue2Length), nameof(Consts.MinValue2Length))]
[DynamicStringLength(typeof(Consts), nameof(Consts.MaxValue1Length), nameof(Consts.MinValue1Length))]
public string Value1 { get; set; }
[DynamicMaxLength(typeof(Consts), nameof(Consts.MaxValue2Length))]
public string Value2 { get; set; }
public static class Consts
{
public static int MinValue2Length { get; set; } = 2;
public static int MaxValue2Length { get; set; } = 7;
public static int MinValue1Length { get; set; } = 2;
public static int MaxValue1Length { get; set; } = 7;
public static int MaxValue2Length { get; set; } = 4;
}
}

9
framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/Validation/ValidationTestController_Tests.cs

@ -63,15 +63,20 @@ namespace Volo.Abp.AspNetCore.Mvc.Validation
{
var result = await GetResponseAsStringAsync("/api/validation-test/object-result-action-dynamic-length?value1=hello");
result.ShouldBe("hello");
}
[Fact]
public async Task Should_Validate_Dynamic_Length_Object_Result_Failing()
{
var result = await GetResponseAsObjectAsync<RemoteServiceErrorResponse>("/api/validation-test/object-result-action-dynamic-length?value1=a", HttpStatusCode.BadRequest); //value1 has min length of 2 chars.
var result = await GetResponseAsObjectAsync<RemoteServiceErrorResponse>("/api/validation-test/object-result-action-dynamic-length?value1=a", HttpStatusCode.BadRequest); //value1 has min string length of 2 chars.
result.Error.ValidationErrors.Length.ShouldBeGreaterThan(0);
result = await GetResponseAsObjectAsync<RemoteServiceErrorResponse>("/api/validation-test/object-result-action-dynamic-length?value1=12345678", HttpStatusCode.BadRequest); //value1 has max string length of 7 chars.
result.Error.ValidationErrors.Length.ShouldBeGreaterThan(0);
result = await GetResponseAsObjectAsync<RemoteServiceErrorResponse>("/api/validation-test/object-result-action-dynamic-length?value1=12345678", HttpStatusCode.BadRequest); //value1 has max length of 7 chars.
result = await GetResponseAsObjectAsync<RemoteServiceErrorResponse>("/api/validation-test/object-result-action-dynamic-length?value1=123458&value2=12345", HttpStatusCode.BadRequest); //value2 has max length of 5 chars.
result.Error.ValidationErrors.Length.ShouldBeGreaterThan(0);
}
}

Loading…
Cancel
Save