Extensible method arguments validation library. Supports .NET 3.5-9.0 and .NET Standard 2.0.
$ dotnet add package Trustsoft.Conditions// --- Requires.That() ---
// Requires.That will throw an exception, when some condition is not held
public void FirstMethod(int arg1, int arg2)
{
// This line will throw an exception when arg1 >= arg2
Requires.That(() => arg1).IsLessThan(arg2);
// This will check that arg2
// - is in range 1..46
Requires.That(arg2, "arg2").IsInRange(1,46);
// Several checks can be added.
Requires.That(arg1).IsInRange(100,1000).IsEven().IsTrue(x => x > 50, "Must be over 500");
// Do something
}
// --- Validate.That() ---
// Validate.That makes possible to get a list of all error conditions
public void SecondMethod(string arg1)
{
// Get a list of errors
IEnumerable<KeyValuePair<ViolationType, string>> errors = Validate.That(() => arg1).IsNotNull().GetErrors();
}
The following checks are available. New checks can easily be made by creating an extension method.
For object:
For Nullable:
For Guid:
For bool and bool?:
For int and long (and other numeric types):
For string:
For IComparable (Int32, Double, String, Char, DateTime and other classes implementing the interface)
For IEnumerable: