You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Dotnet-WebAPI/API/Validation/NameAttribute.cs

28 lines
790 B

using System.ComponentModel.DataAnnotations;
using System.Text.RegularExpressions;
namespace API.Validation;
public partial class NameAttribute : ValidationAttribute
{
protected override ValidationResult? IsValid(object? value, ValidationContext context)
{
var name = context.DisplayName;
if (value is not string str)
{
return new ValidationResult($"{name} should be a string.");
}
if (NameRegex().Match(str).Success)
{
return ValidationResult.Success;
}
return new ValidationResult(
$"{name} should only contain numbers, letters, accents, spaces, _ and - characters.");
}
[GeneratedRegex("^[0-9a-zA-Zà-üÀ-Ü _-]*$")]
private static partial Regex NameRegex();
}