https://github.com/stefh/stef.validation
Guard methods for argument validation (NotNull, NotEmpty, ...)
https://github.com/stefh/stef.validation
Last synced: about 1 year ago
JSON representation
Guard methods for argument validation (NotNull, NotEmpty, ...)
- Host: GitHub
- URL: https://github.com/stefh/stef.validation
- Owner: StefH
- License: mit
- Created: 2021-01-28T10:17:57.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2025-03-26T09:51:57.000Z (about 1 year ago)
- Last Synced: 2025-05-06T22:28:08.800Z (about 1 year ago)
- Language: C#
- Size: 43.9 KB
- Stars: 5
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Stef.Validation
Guard methods for argument validation (NotNull, NotEmpty, ...)
## Packages
| NuGet | NuGet |
| - | - |
| Stef.Validation | [](https://www.nuget.org/packages/Stef.Validation)
| Stef.Validation.Options | [](https://www.nuget.org/packages/Stef.Validation.Options)
## Introduction
Here is a sample constructor that validates its arguments without Guard:
``` c#
public Person(string name, int age)
{
if (name == null)
{
throw new ArgumentNullException(nameof(name), "Name cannot be null.");
}
if (name.Length == 0)
{
throw new ArgumentException("Name cannot be empty.", nameof(name));
}
if (age < 0)
{
throw new ArgumentOutOfRangeException(nameof(age), age, "Age cannot be negative.");
}
}
```
And this is how we write the same constructor with Stef.Validation:
``` c#
using Stef.Validation;
public Person(string name, int age)
{
Guard.NotNullOrEmpty(name, nameof(name));
Guard.NotNullOrEmpty(name); // It's also possible to omit the `nameof(...)`-statement because CallerArgumentExpression is used internally.
Guard.Condition(age, a => a > 0, nameof(age));
}
```