Welcome, dev! 👋
This package helps you create cleaner and more robust domain models by handling validation in a smart way. It combines the power of C# DataAnnotations with your own custom validation rules. The result? A single place to manage and check all domain-related validations. No more clutter. 🚀
RomBaseDomain is an abstract base class designed for your domain models. By inheriting from it, you automatically get:
- ✅ Validation using
[Required],[Range], and other DataAnnotations - 🧠 Support for custom validation logic
- 📋 Friendly, serialized validation error lists for frontends or APIs
- 🛡️ Internal handling that won’t expose methods or internal validation when converting to JSON
It’s built with a naming convention:
- Prefixes like
Listfor anything that's a list - Suffix
Domainfor domain models - Suffix
DataTransferfor DTOs
You define your domain model like this:
public class UserDomain : RomBaseDomain
{
[Required]
public string Name { get; set; }
[Range(18, 120)]
public int Age { get; set; }
[EmailAddress]
public string Email { get; set; }
[StringLength(12, MinimumLength = 6)]
public string Username { get; set; }
[Phone]
public string PhoneNumber { get; set; }
[Url]
public string Website { get; set; }
[Compare("Password")]
public string ConfirmPassword { get; set; }
public string Password { get; set; }
}var user = new UserDomain
{
Name = "", // Required field is empty
Age = 15, // Too young!
Email = "not-an-email", // Invalid format
Username = "usr", // Too short
PhoneNumber = "abc", // Invalid phone
Website = "not-a-url", // Invalid URL
Password = "123456",
ConfirmPassword = "654321" // Doesn’t match
};
bool isValid = user.IsValidDomain; // false
var errors = user.ListValidationError;
foreach (var error in errors)
{
Console.WriteLine($"Field: {error.Field} | Message: {error.Message}");
}This will output all the field-level and global errors in a nice, serializable format.
You can also add your own validation logic by overriding the Validate method:
protected override void Validate()
{
if (string.IsNullOrEmpty(Username))
{
AddValidationError("Username", "Username cannot be empty.");
}
if (Password != ConfirmPassword)
{
AddValidationError("ConfirmPassword", "Passwords do not match.");
}
}This will add custom validation errors to the list, which can be serialized and returned to the client.
The validation errors are serialized in a way that’s easy to consume by frontends or APIs. You can use them directly in your response models.
public class ApiResponse
{
public bool Success { get; set; }
public List<ValidationError> Errors { get; set; }
}
public class ValidationError
{
public string Field { get; set; }
public string Message { get; set; }
}{
"Success": false,
"Errors": [
{
"Field": "Name",
"Message": "The Name field is required."
},
{
"Field": "Age",
"Message": "The field Age must be between 18 and 120."
},
{
"Field": "Email",
"Message": "The Email field is not a valid e-mail address."
},
{
"Field": "Username",
"Message": "The field Username must be a string or array type with a minimum length of '6'."
},
{
"Field": "PhoneNumber",
"Message": "The PhoneNumber field is not a valid phone number."
},
{
"Field": "Website",
"Message": "The Website field is not a valid fully-qualified http, https, or ftp URL."
},
{
"Field": "",
"Message": "'ConfirmPassword' and 'Password' do not match."
}
]
}This response can be easily consumed by any frontend framework or API client.
- The
ListValidationErrorproperty is a list of all validation errors, both field-level and global. - The
IsValidDomainproperty returnstrueif there are no validation errors, andfalseotherwise. - The
Validatemethod is called automatically when you access theIsValidDomainproperty, so you don’t need to call it manually. - The
AddValidationErrormethod is used to add custom validation errors to the list. You can use it to add any validation errors that are not covered by the DataAnnotations. - The
ValidationErrorclass is used to represent a validation error. It has two properties:FieldandMessage. TheFieldproperty is the name of the field that caused the validation error, and theMessageproperty is the error message. - The
ValidationErrorclass is serializable, so you can use it directly in your API responses.
RomBaseDomain is a powerful tool for managing domain validation in your C# applications. By using this package, you can ensure that your domain models are clean, maintainable, and easy to validate. It’s a great way to keep your code organized and reduce the risk of validation errors in your application.
You can install the RomBaseDomain package via NuGet:
dotnet add package RomBaseDomainOr by using the NuGet Package Manager in Visual Studio.
Install-Package RomBaseDomainWe welcome contributions! If you have suggestions, bug reports, or feature requests, please open an issue or submit a pull request.
This project is licensed under the MIT License. See the LICENSE file for details.
Romulo Ribeiro
instagram: @romuloar