Skip to content

[Length] Attribute isn't supported #2756

@satma0745

Description

@satma0745

This issue is a feature request for a support of the [Length] attribute. Judging by the example in the Contribution guide, this issue falls into the Feature Requests category.

The System.ComponentModel.DataAnnotations namespace has 3 (actually even more, but it is not within the scope of this Issue) Attributes responsible for the string length validation:

  1. [MinLength(length)] - MS Docs
  2. [MaxLength(length)] - MS Docs
  3. [Length(minimumLength, maximumLength)] - MS Docs

And while the first two are supported, the latter isn't.
Suppose, we have the following DTO definition:

class Dto
{
    [MinLength(2), MaxLength(10)]
    public string Prop1 { get; set; }

    [Length(2, 10)]
    public string Prop2 { get; set; }
}

Having such model SwaggerGen produces the following OpenApi specification:

"Dto": {
  "type": "object",
  "properties": {
    "prop1": {
      "maxLength": 10,  
      "minLength": 2,
      "type": "string",
      "nullable": true
    },
    "prop2": {
      "type": "string",
      "nullable": true
    }
  },
  "additionalProperties": false
}

Where the prop1 field has both maxLength and minLength values specified, but the prop2 field doesn't have them at all.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions