WIP: A high-performance validation library with support of C# 8.0 nullable reference types
A high-performance validation library with support of C# 8.0 nullable reference types.
using ThrowIf;
Throw.ArgumentNullException()
.If(guid.IsNull(), nameof(guid))
.If(text.IsNull(), nameof(text));
Throw.ArgumentException(name => $"Invalid argument '{name}'")
.If(text.IsEmpty(), nameof(text))
.If(text.Length > 1, nameof(text.Length))
.If(guid.IsEmpty(), nameof(guid));
Default exceptions:
Default validators:
Default message templates:
To use any other exception define factory:
using ThrowIf;
Throw.Exception((name, message) => new CustomException(message), name => $"Wrong parameter '{name}'")
.If(text.IsNull() || text.StartsWith('A'), nameof(text))
.If(text.Length < 100, nameof(text.Length), name => $"{name} is too small");
To reduce the amount of code implement an IConditionGroup interface:
using ThrowIf;
using static ThrowIf.MessageTemplates;
Throw.ArgumentException()
.If(condition: text.IsNull(), name: nameof(text), messageTemplate: CanNotBeNull)
.If(conditionGroup: new TextValidator(), value: text);
public sealed class TextValidator : IConditionGroup<string>
{
public void Verify(in ThrowContext context, string text)
{
context
.If(text.Length < 100, nameof(text.Length), name => $"{name} can not be less than 100")
.If(text.StartsWith('A'), nameof(text), name => $"{name} can not start with char 'A'")
.If(text.EndsWith('B'), nameof(text), name => $"{name} can not end with char 'B'");
}
}
To add new methods extend ThrowContext with extension (impossible to use nullable reference types attributes in some cases):
using ThrowIf;
Throw.ArgumentException()
.If(condition: text.IsNull(), name: nameof(text), messageTemplate: MessageTemplates.CanNotBeNull)
.IfEmpty(value: text, name: nameof(text));
public static class ThrowExtensions
{
public static ref readonly ThrowContext IfEmpty(this in ThrowContext context,
string value, string name)
{
return ref context.If(value.IsEmpty(), name, MessageTemplates.CanNotBeEmpty);
}
}