question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Mark property as required without the [Required] attribute

See original GitHub issue

I 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:closed
  • Created 4 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

5reactions
Snazziecommented, Jul 8, 2022

@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

0reactions
hlovdalcommented, Jan 31, 2023

@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?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found