From 0698514b796ca687ab7862be16f58b8699490224 Mon Sep 17 00:00:00 2001 From: maliming Date: Wed, 27 Apr 2022 11:45:25 +0800 Subject: [PATCH] Add validation rules for this property. Resolve #12282 --- .../Modeling/PropertyApiDescriptionModel.cs | 20 +++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/framework/src/Volo.Abp.Http/Volo/Abp/Http/Modeling/PropertyApiDescriptionModel.cs b/framework/src/Volo.Abp.Http/Volo/Abp/Http/Modeling/PropertyApiDescriptionModel.cs index 87449956cd..bd3852098b 100644 --- a/framework/src/Volo.Abp.Http/Volo/Abp/Http/Modeling/PropertyApiDescriptionModel.cs +++ b/framework/src/Volo.Abp.Http/Volo/Abp/Http/Modeling/PropertyApiDescriptionModel.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel.DataAnnotations; +using System.Linq; using System.Reflection; using Volo.Abp.Http.ProxyScripting.Configuration; @@ -18,16 +19,31 @@ public class PropertyApiDescriptionModel public bool IsRequired { get; set; } - //TODO: Validation rules for this property + public int? MinLength { get; set; } + + public int? MaxLength { get; set; } + + public string Minimum { get; set; } + + public string Maximum { get; set; } + + public string Regex { get; set; } + public static PropertyApiDescriptionModel Create(PropertyInfo propertyInfo) { + var customAttributes = propertyInfo.GetCustomAttributes(true); return new PropertyApiDescriptionModel { Name = propertyInfo.Name, JsonName = AbpApiProxyScriptingConfiguration.PropertyNameGenerator.Invoke(propertyInfo), Type = ApiTypeNameHelper.GetTypeName(propertyInfo.PropertyType), TypeSimple = ApiTypeNameHelper.GetSimpleTypeName(propertyInfo.PropertyType), - IsRequired = propertyInfo.IsDefined(typeof(RequiredAttribute), true) + IsRequired = customAttributes.OfType().Any(), + Minimum = customAttributes.OfType().Select(x => x.Minimum).FirstOrDefault()?.ToString(), + Maximum = customAttributes.OfType().Select(x => x.Maximum).FirstOrDefault()?.ToString(), + MinLength = customAttributes.OfType().FirstOrDefault()?.Length ?? customAttributes.OfType().FirstOrDefault()?.MinimumLength, + MaxLength = customAttributes.OfType().FirstOrDefault()?.Length ?? customAttributes.OfType().FirstOrDefault()?.MaximumLength, + Regex= customAttributes.OfType().Select(x => x.Pattern).FirstOrDefault() }; } }