Add red star in swagger schema for custom validation attributes

68 Views Asked by At

I use ASP.NET Core web api, when I use [Required] attribute for my view model properties, swagger will show a red star in schema like below

public class InsertCircularFrm
{
    [Required]
    public Guid RoleId { get; set; }
}

enter image description here

Now i have a custom validation attribute for prevent entering empty guid:

[AttributeUsage(AttributeTargets.Property)]
public class GuidNotEmptyAttribute : ValidationAttribute
{
    protected override ValidationResult? IsValid(object? value, ValidationContext validationContext)
    {
        if (value is Guid guidValue)
        {
            if (guidValue == Guid.Empty)
            {
                return new ValidationResult(ErrorMessage ?? $"{validationContext.DisplayName} is required");
            }
        }

        return ValidationResult.Success;
    }
}
public class InsertCircularFrm
{
    [GuidNotEmpty]
    public Guid RoleId { get; set; }
}

But now it doesn't show red star in schema when I use my custom validation attribute GuidNotEmpty

enter image description here

Guid is value type and because of that if client doesn't send value for that, it gives defalut value and required attribute doesn't work for that, now with above codes i check for emtpy guid, but the main question is how can I config swagger to show red star for my custom validation attribute in swagger ui ?

0

There are 0 best solutions below