333
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using Newtonsoft.Json.Serialization;
|
||||
|
||||
namespace Swashbuckle.Swagger
|
||||
{
|
||||
public static class SchemaExtensions
|
||||
{
|
||||
public static Schema WithValidationProperties(this Schema schema, JsonProperty jsonProperty)
|
||||
{
|
||||
var propInfo = jsonProperty.PropertyInfo();
|
||||
if (propInfo == null)
|
||||
return schema;
|
||||
|
||||
foreach (var attribute in propInfo.GetCustomAttributes(false))
|
||||
{
|
||||
var regex = attribute as RegularExpressionAttribute;
|
||||
if (regex != null)
|
||||
schema.pattern = regex.Pattern;
|
||||
|
||||
var range = attribute as RangeAttribute;
|
||||
if (range != null)
|
||||
{
|
||||
int maximum;
|
||||
if (Int32.TryParse(range.Maximum.ToString(), out maximum))
|
||||
schema.maximum = maximum;
|
||||
|
||||
int minimum;
|
||||
if (Int32.TryParse(range.Minimum.ToString(), out minimum))
|
||||
schema.minimum = minimum;
|
||||
}
|
||||
|
||||
var length = attribute as StringLengthAttribute;
|
||||
if (length != null)
|
||||
{
|
||||
schema.maxLength = length.MaximumLength;
|
||||
schema.minLength = length.MinimumLength;
|
||||
}
|
||||
}
|
||||
|
||||
if (!jsonProperty.Writable)
|
||||
schema.readOnly = true;
|
||||
|
||||
return schema;
|
||||
}
|
||||
|
||||
public static void PopulateFrom(this PartialSchema partialSchema, Schema schema)
|
||||
{
|
||||
if (schema == null) return;
|
||||
|
||||
partialSchema.type = schema.type;
|
||||
partialSchema.format = schema.format;
|
||||
partialSchema.vendorExtensions = schema.vendorExtensions;
|
||||
|
||||
if (schema.items != null)
|
||||
{
|
||||
// TODO: Handle jagged primitive array and error on jagged object array
|
||||
partialSchema.items = new PartialSchema();
|
||||
partialSchema.items.PopulateFrom(schema.items);
|
||||
}
|
||||
|
||||
partialSchema.@default = schema.@default;
|
||||
partialSchema.maximum = schema.maximum;
|
||||
partialSchema.exclusiveMaximum = schema.exclusiveMaximum;
|
||||
partialSchema.minimum = schema.minimum;
|
||||
partialSchema.exclusiveMinimum = schema.exclusiveMinimum;
|
||||
partialSchema.maxLength = schema.maxLength;
|
||||
partialSchema.minLength = schema.minLength;
|
||||
partialSchema.pattern = schema.pattern;
|
||||
partialSchema.maxItems = schema.maxItems;
|
||||
partialSchema.minItems = schema.minItems;
|
||||
partialSchema.uniqueItems = schema.uniqueItems;
|
||||
partialSchema.@enum = schema.@enum;
|
||||
partialSchema.multipleOf = schema.multipleOf;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user