Tooark.Attributes 4.7.1

dotnet add package Tooark.Attributes --version 4.7.1
                    
NuGet\Install-Package Tooark.Attributes -Version 4.7.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Tooark.Attributes" Version="4.7.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Tooark.Attributes" Version="4.7.1" />
                    
Directory.Packages.props
<PackageReference Include="Tooark.Attributes" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Tooark.Attributes --version 4.7.1
                    
#r "nuget: Tooark.Attributes, 4.7.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Tooark.Attributes@4.7.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Tooark.Attributes&version=4.7.1
                    
Install as a Cake Addin
#tool nuget:?package=Tooark.Attributes&version=4.7.1
                    
Install as a Cake Tool

Tooark.Attributes

Library with attribute validators for properties, fields or parameters, integrated with System.ComponentModel.DataAnnotations.

πŸ“– Docs: Tooark.Attributes on the site Β· All packages Β· API reference

🌍 Languages: πŸ‡ΊπŸ‡Έ English (this file) Β· πŸ‡§πŸ‡· PortuguΓͺs


πŸ“‘ Contents


πŸ”§ Installation

dotnet add package Tooark.Attributes

βœ… Validation Attributes

Every attribute accepts propertyName, which sets the field name used in the error message.

1. Document Validation

Purpose: Validates the document by format, with a regular expression, and by check digits.

Parameters:

  • string type: Type of document to validate. Required.
  • string propertyName: Field name in the error message. Default: "Document".

The type is received as text because attribute arguments accept constants only β€” a parameter of type EDocumentType would prevent the attribute from being applied (CS0181). Accepted values, case-insensitive: CPF, RG, CNH, CNPJ, CPF_CNPJ, CPF_RG, CPF_RG_CNH and None.

An unrecognized type is a configuration error and makes the attribute throw InternalServerErrorException on the first validation, with the message Attributes.DocumentTypeUnknown;{type}.

Usage Example

2. Email Validation

Purpose: Validates whether the value is a valid email address.

Parameters:

  • string propertyName: Field name in the error message. Default: "Email".

Usage Example

Purpose: Validates whether the value is a video link from one of the enabled providers.

Parameters:

  • string propertyName: Field name in the error message. Default: "Link".
  • bool youtube: Allows YouTube links. Default: true.
  • bool vimeo: Allows Vimeo links. Default: true.
  • bool dailymotion: Allows Dailymotion links. Default: true.

Disabling all three providers is a configuration error β€” no link could be accepted β€” and makes the attribute throw InternalServerErrorException with the message Attributes.LinkVideoNoProvider;{field}.

Usage Example

4. Password Validation

Purpose: Validates whether the password meets the configured complexity criteria.

Parameters:

  • bool lowercase: Requires a lowercase character. Default: true.
  • bool uppercase: Requires an uppercase character. Default: true.
  • bool number: Requires a numeric character. Default: true.
  • bool symbol: Requires a special character. Default: true.
  • int length: Minimum length. Default: 8. A non-positive value assumes 1.
  • string propertyName: Field name in the error message. Default: "Password".

The criteria apply exactly as configured. Disabling all of them means requiring only the length, which is a legitimate policy: long passwords with no composition rule.

Usage Example

5. URL Validation

Purpose: Validates whether the value is a valid URL in the email (sending and receiving), FTP, HTTP or WebSocket protocols.

Parameters:

  • string propertyName: Field name in the error message. Default: "Url".

Usage Example

6. Zip Code Validation

Purpose: Validates whether the value is a valid zip code.

Parameters:

  • string propertyName: Field name in the error message. Default: "ZipCode".

Usage Example


🧠 Common behavior

Every attribute inherits from TooarkValidationAttribute and shares the rules below.

Error messages. The message is returned in the ValidationResult of each validation, and never written to ErrorMessage. That matters because the validation framework reuses the same attribute instance across every validation of that field, including concurrent ones: writing to the attribute would mix one request's message with another's.

Without configuration, the message is a translation key in the Key;Field format:

Situation Message
Field not provided Field.Required;{field}
Value does not match the rule Field.Invalid;{field}

If you configure ErrorMessage or ErrorMessageResourceName on the attribute, that message is used instead of the key.

Missing value. A null, empty or whitespace-only value is reported as Field.Required. In other words, the attributes imply being required β€” they do not follow the DataAnnotations convention, in which validators other than [Required] accept null. For an optional field, validate outside the attribute or apply it conditionally.

Where to apply. The attributes are valid on properties, fields and parameters, like the DataAnnotations ones. On a positional record, apply the attribute with no target, on the constructor parameter (record Dto([EmailValidation] string Email)). That is where MVC looks for a record's validation, and it throws InvalidOperationException when it finds it on the generated property ([property: EmailValidation]). An action parameter also accepts the attribute ([FromQuery][EmailValidation] string email). Validator.TryValidateObject goes the other way: it only reads properties, and ignores the attribute on the constructor parameter. A record validated outside MVC needs the attribute on the property, which MVC rejects, so the same record cannot serve both paths.

ASP.NET Core implicit required. With <Nullable>enable</Nullable>, MVC treats every non-nullable reference type as if it had [Required], with the framework's message (The Email field is required.), which is not a translation key and reaches the client as is. Since the attribute already reports the missing value, a missing non-nullable field gets both messages. To keep only the attribute's key, register AddTooarkValidationAttributes() from Tooark.AspNetCore. MVC stops inferring [Required] only on members with a Tooark attribute, and keeps the inference on the others and the declared [Required]. Without Tooark.AspNetCore, there are two manual ways out:

  • declare the field as nullable ([EmailValidation] string? Email). MVC does not infer [Required], and the attribute still rejects the missing value. It applies to that field only; the cost is the nullable type in the code that reads the DTO;
  • or turn the inference off for the whole application, with SuppressImplicitRequiredAttributeForNonNullableReferenceTypes = true in MvcOptions. It reaches every DTO: a field without a Tooark attribute is no longer required until it gets an explicit [Required].

Non-text values. The value is converted with ToString() before validation, so a Uri or a custom type with a suitable ToString() works.

Regular expression timeout. Every regular expression runs with a 300 ms limit. Input that causes excessive backtracking is rejected, and the exception does not escape the attribute.

Configuration error. An impossible configuration β€” unknown document type, video link with no provider β€” throws InternalServerErrorException on the first validation. It is not a failure of the data but of the attribute applied in code; the keys are in Tooark.Attributes.Messages.AttributeErrorMessages.


πŸ“ Usage Examples

Document Validation

using Tooark.Attributes;

public class Person
{
  [DocumentValidation("CPF")]
  public string Cpf { get; set; } = null!;

  [DocumentValidation("CPF_CNPJ", propertyName: "Document")]
  public string Document { get; set; } = null!;
}

Email Validation

using Tooark.Attributes;

public class Contact
{
  [EmailValidation]
  public string Email { get; set; } = null!;

  // With a custom message, which takes precedence over the default key
  [EmailValidation(ErrorMessage = "Provide a corporate email")]
  public string CorporateEmail { get; set; } = null!;
}
using Tooark.Attributes;

public class Lesson
{
  [LinkVideoValidation]
  public string Video { get; set; } = null!;

  // YouTube only, with a custom field name in the message
  [LinkVideoValidation("Presentation", youtube: true, vimeo: false, dailymotion: false)]
  public string Presentation { get; set; } = null!;
}

Password Validation

using Tooark.Attributes;

public class Credential
{
  [PasswordValidation]
  public string Password { get; set; } = null!;

  // Passphrase: no composition rule, minimum length of 20
  [PasswordValidation(false, false, false, false, 20)]
  public string Passphrase { get; set; } = null!;
}

URL Validation

using Tooark.Attributes;

public class Site
{
  [UrlValidation]
  public string Address { get; set; } = null!;
}

Zip Code Validation

using Tooark.Attributes;

public class Address
{
  [ZipCodeValidation]
  public string ZipCode { get; set; } = null!;
}

Records and action parameters

using Microsoft.AspNetCore.Mvc;
using Tooark.Attributes;

// Positional record: the attribute goes on the constructor parameter, with no target
public sealed record CreateContactDto(
  [EmailValidation] string Email,
  [DocumentValidation("CPF")] string Cpf
);

[ApiController]
[Route("contacts")]
public sealed class ContactController : ControllerBase
{
  [HttpPost]
  public IActionResult Create([FromBody] CreateContactDto dto) => Ok();

  // Action parameter validated directly by the attribute
  [HttpGet]
  public IActionResult Find([FromQuery][EmailValidation] string email) => Ok();
}

Reading the validation result

using System.ComponentModel.DataAnnotations;

var person = new Person { Cpf = "11111111111" };
var results = new List<ValidationResult>();

Validator.TryValidateObject(person, new ValidationContext(person), results, true);

foreach (var result in results)
{
  // result.ErrorMessage -> "Field.Invalid;Document"
  // result.MemberNames  -> ["Cpf"]
}

πŸ“‹ Dependencies


🀝 Contributing

Contributions are welcome! Start with CONTRIBUTING.md β€” it covers the development workflow, the coding and commit conventions and the pull request checklist. Bugs and feature requests go through the issue templates of the Tooark repository.

By participating you agree to the Code of Conduct.


πŸ†˜ Help & Security

  • ❓ Questions, bugs, feature ideas β€” see SUPPORT.md for the right channel
  • πŸ”’ Security vulnerabilities β€” do not open a public issue; follow SECURITY.md

πŸ’– Support

If Tooark helps your projects, consider supporting its development:

Every contribution helps keep the project maintained and improving. Thank you! πŸ™


πŸ“„ License

This project is licensed under the BSD 3-Clause License.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on Tooark.Attributes:

Package Downloads
Tooark

Package with all Tooark resources for .NET applications.

Tooark.AspNetCore

Package with the ASP.NET Core specific pieces of Tooark for .NET applications.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
4.7.1 38 10/17/2026
4.7.0 311 9/30/2026
4.6.0 145 9/29/2026
4.5.0 120 9/29/2026
4.4.0 110 9/28/2026
4.3.0 112 9/26/2026
4.2.1 114 9/25/2026
4.2.0 124 9/20/2026
4.1.0 110 9/18/2026
4.0.1 125 8/25/2026
4.0.0 131 8/25/2026
3.3.6 114 9/18/2026
3.3.5 122 8/25/2026
3.3.4 129 8/12/2026
3.3.3 139 7/27/2026
3.3.2 280 6/11/2026
3.3.1 158 6/6/2026
2.3.5 102 9/18/2026
2.3.4 117 8/25/2026
2.3.3 125 7/27/2026
Loading failed