23 lines
787 B
C#
Raw Normal View History

2020-10-20 16:20:38 +03:00
using System;
using System.ComponentModel.DataAnnotations;
2020-10-26 17:06:29 +02:00
namespace MalwareMultiScan.Api.Attributes
2020-10-20 16:20:38 +03:00
{
/// <summary>
/// Validate URI to be an absolute http(s) URL.
/// </summary>
2020-10-29 12:17:09 +02:00
public class IsHttpUrlAttribute : ValidationAttribute
2020-10-20 16:20:38 +03:00
{
/// <inheritdoc />
2020-10-20 16:20:38 +03:00
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
if (!Uri.TryCreate((string)value, UriKind.Absolute, out var uri)
|| !uri.IsAbsoluteUri
2020-10-29 12:17:09 +02:00
|| uri.Scheme.ToLowerInvariant() != "http"
&& uri.Scheme.ToLowerInvariant() != "https")
2020-10-26 17:06:29 +02:00
return new ValidationResult("Only absolute http(s) URLs are supported");
2020-10-20 16:20:38 +03:00
return ValidationResult.Success;
}
}
}