https://github.com/fiseni/pozitron.validations
Nuget package for validating various inputs against invalid values. If validation fails appropriate exception is thrown.
https://github.com/fiseni/pozitron.validations
inline-validation invalid-argument pozitrondev validations
Last synced: 16 days ago
JSON representation
Nuget package for validating various inputs against invalid values. If validation fails appropriate exception is thrown.
- Host: GitHub
- URL: https://github.com/fiseni/pozitron.validations
- Owner: fiseni
- License: mit
- Created: 2019-11-27T13:52:11.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2021-02-15T17:08:00.000Z (about 4 years ago)
- Last Synced: 2025-04-11T17:09:56.603Z (19 days ago)
- Topics: inline-validation, invalid-argument, pozitrondev, validations
- Language: C#
- Homepage:
- Size: 88.9 KB
- Stars: 5
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
[](https://www.nuget.org/packages/PozitronDev.Validations)[](https://www.nuget.org/packages/PozitronDev.Validations)
[](https://dev.azure.com/pozitrondev/PozitronDev.Validations/_build/latest?definitionId=4&branchName=master)
[](https://dev.azure.com/pozitrondev/PozitronDev.Validations/_build/latest?definitionId=4&branchName=master)
# PozitronDev Validations
Nuget package for validating various inputs against invalid values. If validation fails appropriate exception is thrown.
## Usage
Validation rules are implemented as extension methods to `ValidateFor` (`IValidate`) clause. Therefore, distinct validation methods are available based on the type of the object being extended.
All validation methods accept optional `parameterName` argument. If the argument is ommited, the name of the underlying type is used to construct the exception messages.
Inline usages are possible too, since all extensions return the input object.
- Argument validation example
```c#
public void CreateCart(Customer customer)
{
customer.ValidateFor().Null();// create cart here
}
```- Inline usage example:
```c#
public async Task> GetCompanies()
{
return (await dbContext.Companies
.Where(x => x.Name == "MyCompany")
.OrderBy(x => x.Name)
.ToListAsync())
.ValidateFor().NullOrEmpty();
}
``````c#
public Address GetCustomerAddress(Customer customer)
{
return customer.ValidateFor().Null().Address;
}
```- Various validations based on the type:
```c#
object myObject;
myObject.ValidateFor().Null();
myObject.ValidateFor().Null(nameof(myObject));string myString;
myString.ValidateFor().NullOrEmpty();
myString.ValidateFor().NullOrWhiteSpace();
myString.ValidateFor().NullOrEmpty(nameof(myString));
myString.ValidateFor().NullOrWhiteSpace(nameof(myString));int myInt;
myInt.ValidateFor().Default();
myInt.ValidateFor().Default(nameof(myInt));IEnumerable myList;
myList.ValidateFor().NullOrEmpty();
myList.ValidateFor().NullOrEmpty(nameof(myList));Customer myCustomer = repo.GetByID(1);
myCustomer.ValidateFor().NotFound(1);
myCustomer.ValidateFor().NotFound(1, nameof(myCustomer));```
## Supported Validation Rules
- `T.IValidate.Null(string parameterName = null)`
- For T as class or nullable ValueType
- Throws if input is null- `T.IValidate.NullOrEmpty(string parameterName = null)`
- For T as string
- Throws if input is null or empty string- `T.IValidate.NullOrWhiteSpace(string parameterName = null)`
- For T as string
- Throws if input is null, empty string or whitespace only- `T.IValidate.Default(string parameterName = null)`
- Throws if input is default value for type T- `IEnumerable.IValidate>.NullOrEmpty(string parameterName = null)`
- Throws if input is null or empty collection- `T.IValidate.NotFound(TKey key, string parameterName = null)`
- For T as class, TKey as not nullable struct
- Throws if input (the queried object by key value) is null.
- Throws NotFoundException, custom exception defined in this package.## Extending with your own validation rules
To extend IValidate with your own rules, you can do the following:
```c#
namespace PozitronDev.Validations
{
public static class MyExtension
{
public static int MinQuantity(this IValidate validateClause)
{
if (validateClause.Input < 1)
{
throw new ArgumentException($"Required quantity should not be less than 1.");
}return validateClause.Input;
}
}
}
```
```c#
// Usage
public void Buy(int quantity, decimal price)
{
quantity.ValidateFor().MinQuantity();var total = price * quantity;
// Some processing
}
```
```c#
// Inline usage
public void Buy(int quantity, decimal price)
{
var total = price * quantity.ValidateFor().MinQuantity();
// Some processing
}
```## Give a Star! :star:
If you like or are using this project please give it a star. Thanks!