Skip to content

Commit

Permalink
Implemented support for LengthAttribute domaindrivendev#2756 for #3
Browse files Browse the repository at this point in the history
  • Loading branch information
Havunen committed Feb 18, 2024
1 parent ecb378b commit 8679f9e
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ public static void ApplyValidationAttributes(this OpenApiSchema schema, IEnumera
if (attribute is DataTypeAttribute dataTypeAttribute)
ApplyDataTypeAttribute(schema, dataTypeAttribute);

else if (attribute is LengthAttribute lengthAttribute)
ApplyLengthAttribute(schema, lengthAttribute);

else if (attribute is MinLengthAttribute minLengthAttribute)
ApplyMinLengthAttribute(schema, minLengthAttribute);

Expand Down Expand Up @@ -113,6 +116,20 @@ private static void ApplyDataTypeAttribute(OpenApiSchema schema, DataTypeAttribu
}
}

private static void ApplyLengthAttribute(OpenApiSchema schema, LengthAttribute lengthAttribute)
{
if (schema.Type == "array")
{
schema.MinItems = lengthAttribute.MinimumLength;
schema.MaxItems = lengthAttribute.MaximumLength;
}
else
{
schema.MinLength = lengthAttribute.MinimumLength;
schema.MaxLength = lengthAttribute.MaximumLength;
}
}

private static void ApplyMinLengthAttribute(OpenApiSchema schema, MinLengthAttribute minLengthAttribute)
{
if (schema.Type == "array")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,12 @@ public void GenerateSchema_SetsValidationProperties_IfComplexTypeHasValidationAt
Assert.Equal(3, schema.Properties["StringWithMinMaxLength"].MaxLength);
Assert.Equal(1, schema.Properties["ArrayWithMinMaxLength"].MinItems);
Assert.Equal(3, schema.Properties["ArrayWithMinMaxLength"].MaxItems);

Assert.Equal(1, schema.Properties["ArrayWithLength"].MinItems);
Assert.Equal(3, schema.Properties["ArrayWithLength"].MaxItems);
Assert.Equal(1, schema.Properties["StringWithLength"].MinLength);
Assert.Equal(3, schema.Properties["StringWithLength"].MaxLength);

Assert.Equal(1, schema.Properties["IntWithRange"].Minimum);
Assert.Equal(10, schema.Properties["IntWithRange"].Maximum);
Assert.Equal("^[3-6]?\\d{12,15}$", schema.Properties["StringWithRegularExpression"].Pattern);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ public class TypeWithValidationAttributes
[MinLength(1), MaxLength(3)]
public string[] ArrayWithMinMaxLength { get; set; }

[Length(1, 3)]
public string[] ArrayWithLength { get; set; }

[Length(1, 3)]
public string StringWithLength { get; set; }

[Range(1, 10)]
public int IntWithRange { get; set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ public class TypeWithValidationAttributesViaMetadataType

public string[] ArrayWithMinMaxLength { get; set; }

public string[] ArrayWithLength { get; set; }

public string StringWithLength { get; set; }

public int IntWithRange { get; set; }

public string StringWithRegularExpression { get; set; }
Expand All @@ -37,6 +41,12 @@ public class MetadataType
[MinLength(1), MaxLength(3)]
public string[] ArrayWithMinMaxLength { get; set; }

[Length(1, 3)]
public string[] ArrayWithLength { get; set; }

[Length(1, 3)]
public string StringWithLength { get; set; }

[Range(1, 10)]
public int IntWithRange { get; set; }

Expand Down

0 comments on commit 8679f9e

Please sign in to comment.