Browse Source

Merge pull request #12377 from abpframework/PropertyApiDescriptionModel

Add validation rules for this property.
pull/12471/merge
Halil İbrahim Kalkan 4 years ago
committed by GitHub
parent
commit
ba7bcfd538
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 20
      framework/src/Volo.Abp.Http/Volo/Abp/Http/Modeling/PropertyApiDescriptionModel.cs

20
framework/src/Volo.Abp.Http/Volo/Abp/Http/Modeling/PropertyApiDescriptionModel.cs

@ -1,5 +1,6 @@
using System; using System;
using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Reflection; using System.Reflection;
using Volo.Abp.Http.ProxyScripting.Configuration; using Volo.Abp.Http.ProxyScripting.Configuration;
@ -18,16 +19,31 @@ public class PropertyApiDescriptionModel
public bool IsRequired { get; set; } 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) public static PropertyApiDescriptionModel Create(PropertyInfo propertyInfo)
{ {
var customAttributes = propertyInfo.GetCustomAttributes(true);
return new PropertyApiDescriptionModel return new PropertyApiDescriptionModel
{ {
Name = propertyInfo.Name, Name = propertyInfo.Name,
JsonName = AbpApiProxyScriptingConfiguration.PropertyNameGenerator.Invoke(propertyInfo), JsonName = AbpApiProxyScriptingConfiguration.PropertyNameGenerator.Invoke(propertyInfo),
Type = ApiTypeNameHelper.GetTypeName(propertyInfo.PropertyType), Type = ApiTypeNameHelper.GetTypeName(propertyInfo.PropertyType),
TypeSimple = ApiTypeNameHelper.GetSimpleTypeName(propertyInfo.PropertyType), TypeSimple = ApiTypeNameHelper.GetSimpleTypeName(propertyInfo.PropertyType),
IsRequired = propertyInfo.IsDefined(typeof(RequiredAttribute), true) IsRequired = customAttributes.OfType<RequiredAttribute>().Any(),
Minimum = customAttributes.OfType<RangeAttribute>().Select(x => x.Minimum).FirstOrDefault()?.ToString(),
Maximum = customAttributes.OfType<RangeAttribute>().Select(x => x.Maximum).FirstOrDefault()?.ToString(),
MinLength = customAttributes.OfType<MinLengthAttribute>().FirstOrDefault()?.Length ?? customAttributes.OfType<StringLengthAttribute>().FirstOrDefault()?.MinimumLength,
MaxLength = customAttributes.OfType<MaxLengthAttribute>().FirstOrDefault()?.Length ?? customAttributes.OfType<StringLengthAttribute>().FirstOrDefault()?.MaximumLength,
Regex= customAttributes.OfType<RegularExpressionAttribute>().Select(x => x.Pattern).FirstOrDefault()
}; };
} }
} }

Loading…
Cancel
Save