Attributes for validation during model binding in ASP.NET Core MVC.
| Attribute | Description | Example |
|---|---|---|
[Whitelist(<whitelist>)] |
Whitelists a field to ensure only a specific set of characters are allowed in that field | [Whitelist("abc123")] allows only the characters 'a', 'b', 'c', '1', '2', and '3' in the field. |
[FileExtensions()] |
Validates that the file extension of any uploaded files is among a specified set of allowed extensions. | [FileExtensions("jpg,jpeg,png")] allows only files with the extensions .jpg, .jpeg, and .png. |
dotnet add package Gibe.Validation
To use the validation attributes, simply apply them to the properties of your model classes. For example:
public class UserViewModel
{
[Whitelist("abc123")]
public string Username { get; set; }
[FileExtensions("jpg,jpeg,png")]
public IFormFile ProfilePicture { get; set; }
}In this example, the Username property will only accept characters 'a', 'b', 'c', '1', '2', and '3', while the ProfilePicture property will only accept files with the extensions .jpg, .jpeg, and .png.
Gibe.Validation also provides predefined whitelist sets for common character groups. You can use these sets instead of specifying individual characters. For example:
| Predefined Set | Description |
|---|---|
| SingleText | Accepts most Latin characters, numbers, and common punctuation. Does not allow line breaks . |
| MultiText | Accepts most Latin characters, numbers, and common punctuation. Allows line breaks. |
| Title | Accepts most Latin characters, spaces, and common punctuation. Does not allow line breaks. |
| Name | Accepts most Latin characters, numbers, and limited punctuation. Does not allow line breaks. |
| Int | Accepts only integer digits. |
| Decimal | Accepts only decimal digits and decimal points. |
| PhoneNumber | Accepts only numbers, plus, brackets, spaces and dashes. |
| Accepts only characters allowed in email addresses |
Contributions to Gibe.Validation are welcome! If you have an idea for a new validation attribute or an improvement to an existing one, please submit a pull request.