/// <summary>
 /// Get attribute of a specific type.
 /// </summary>
 /// <typeparam name="TAttribute">The attribute <see cref="Type" />.</typeparam>
 /// <param name="modelMetadata">The <see cref="ModelMetadata"/> instance.</param>
 /// <returns>The attribute.</returns>
 public static TAttribute GetAttribute <TAttribute>(this ModelMetadata modelMetadata) where TAttribute : Attribute
 {
     return(modelMetadata.GetAttributes <TAttribute>().FirstOrDefault());
 }
 /// <summary>
 /// Get attribute of a specific type.
 /// </summary>
 /// <typeparam name="TAttribute">The attribute <see cref="Type" />.</typeparam>
 /// <param name="modelMetadata">The <see cref="ModelMetadata"/> instance.</param>
 /// <param name="predicate">A function to test an element for a condition.</param>
 /// <returns>The attribute.</returns>
 public static TAttribute GetAttribute <TAttribute>(this ModelMetadata modelMetadata, Func <TAttribute, bool> predicate) where TAttribute : Attribute
 {
     return(modelMetadata.GetAttributes <TAttribute>().FirstOrDefault(predicate));
 }
Exemple #3
0
        private static void SetAttributes(Schema schema, ModelMetadata modelMetadata, string propertyName = null)
        {
            var attributes = propertyName == null
                ? modelMetadata.GetAttributes <Attribute>()
                : modelMetadata.GetAttributes <Attribute>(propertyName);

            if (attributes.Count == 0)
            {
                return;
            }
            if (string.IsNullOrEmpty(schema.Description))
            {
                schema.Description = attributes.OfType <DescriptionAttribute>().LastOrDefault()?.Description;
            }
            if (schema.Default == null)
            {
                schema.Default = attributes.OfType <DefaultValueAttribute>().LastOrDefault()?.Value;
            }
            if (string.IsNullOrEmpty(schema.Pattern))
            {
                schema.Pattern = attributes.OfType <RegularExpressionAttribute>().LastOrDefault()?.Pattern;
            }
            if (schema.Maximum == null && schema.Minimum == null)
            {
                var range = attributes.OfType <RangeAttribute>().LastOrDefault();
                if (range != null)
                {
                    if (Int32.TryParse(range.Maximum.ToString(), out int maximum))
                    {
                        schema.Maximum = maximum;
                    }

                    if (Int32.TryParse(range.Minimum.ToString(), out int minimum))
                    {
                        schema.Minimum = minimum;
                    }
                }
            }
            if (schema.MinLength == null)
            {
                schema.MinLength = attributes.OfType <MinLengthAttribute>().LastOrDefault()?.Length;
            }
            if (schema.MaxLength == null)
            {
                schema.MaxLength = attributes.OfType <MaxLengthAttribute>().LastOrDefault()?.Length;
            }
            if (schema.MinLength == null && schema.MaxLength == null)
            {
                var stringLengthAttribute = attributes.OfType <StringLengthAttribute>().LastOrDefault();
                if (stringLengthAttribute != null)
                {
                    schema.MinLength = stringLengthAttribute.MinimumLength;
                    schema.MaxLength = stringLengthAttribute.MaximumLength;
                }
            }
            if (string.IsNullOrEmpty(schema.Title))
            {
                schema.Title = attributes.OfType <DisplayNameAttribute>().LastOrDefault()?.DisplayName;
            }
            if (string.IsNullOrEmpty(schema.Format) && schema.Type == "string")
            {
                var dataTypeAttribute = attributes.OfType <DataTypeAttribute>().LastOrDefault();

                if (dataTypeAttribute != null && DataTypeFormatMap.TryGetValue(dataTypeAttribute.DataType, out string format))
                {
                    schema.Format = format;
                }
            }
        }