Mark property as required without the [Required] attribute
See original GitHub issueI understand that Swashbuckle supports the [Required] attribute on request model properties, i.e.
public class ChangeLineNumberViewModel
{
/// <summary>
/// The domain user who is changing the line number, i.e. TPWR\\johnd
/// </summary>
[Required]
public string RequestedBy { get; set; }
}
However, [Required]
signals to MVC model binding that the field is Required, and model binding intercepts the requests and gets in the way of my own internal validation which I wish to use (so that I can return a Problem details).
I guess what I’d like is perhaps a [SwaggerRequired]
attribute I can use instead, so that the property is marked as required in the swagger doc.
Issue Analytics
- State:
- Created 4 years ago
- Comments:6 (4 by maintainers)
Top Results From Across the Web
How to mark a property as required in Swagger, without ...
Thanks to Mohsin, I solved my problem. The following I came up with, I created an attribute called SwaggerRequired. This attribute can be ......
Read more >Model not valid even though no properties have the ...
I am just using the ModelState which in theory should be valid as there are no [Required] attributes currently on any properties. Reply....
Read more >RequiredAttribute Class (System.ComponentModel. ...
The RequiredAttribute attribute specifies that when a field on a form is validated, the field must contain a value. A validation exception is...
Read more >required modifier - C# Reference
In this article. The required modifier indicates that the field or property it's applied to must be initialized by an object initializer.
Read more >HTML attribute: required - MDN Web Docs
The Boolean required attribute, if present, indicates that the user must specify a value for the input before the owning form can be ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@domaindrivendev That solution is not nice to use at all. Should there not be a way to set globally or a swagger variant of
[Required]
that doesnt affect .net model@Snazzie, @mattfrear There is a custom
[SwaggerRequired]
attribute presented in this answer, although I agree this should be something that comes standard and not something that needs custom extra code.Ideally there should also exist a
[SwaggerOptional]
attribute that does nothing but with some check than you then can configure to blow for anything that does not have one of the two attributes. Any suggestions how to achieve this?