Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions src/Immediate.Validations.Shared/ValidationException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ internal ValidationException(string message, IReadOnlyList<ValidationError> erro
/// </summary>
public IReadOnlyList<ValidationError> Errors { get; }

#pragma warning disable CS8777 // Parameter must have a non-null value when exiting.
// These methods throw on `null`, but indirectly.

/// <summary>
/// Validates an object and throws a <see cref="ValidationException"/> is there are any validation errors.
/// </summary>
Expand All @@ -56,7 +59,7 @@ internal ValidationException(string message, IReadOnlyList<ValidationError> erro
/// <exception cref="ValidationException">
/// Thrown if there are any errors.
/// </exception>
public static void ThrowIfInvalid<T>(T obj)
public static void ThrowIfInvalid<T>([System.Diagnostics.CodeAnalysis.NotNull] T? obj)
where T : IValidationTarget<T>
{
ThrowIfInvalid(T.Validate(obj));
Expand All @@ -77,7 +80,7 @@ public static void ThrowIfInvalid<T>(T obj)
/// <exception cref="ValidationException">
/// Thrown if there are any errors.
/// </exception>
public static void ThrowIfInvalid<T>(T obj, string message)
public static void ThrowIfInvalid<T>([System.Diagnostics.CodeAnalysis.NotNull] T? obj, string message)
where T : IValidationTarget<T>
{
ThrowIfInvalid(T.Validate(obj), message);
Expand All @@ -98,7 +101,7 @@ public static void ThrowIfInvalid<T>(T obj, string message)
/// <exception cref="ValidationException">
/// Thrown if there are any errors.
/// </exception>
public static void ThrowIfInvalid<T>(T obj, Func<T, string> messageFunc)
public static void ThrowIfInvalid<T>([System.Diagnostics.CodeAnalysis.NotNull] T? obj, Func<T?, string> messageFunc)
where T : IValidationTarget<T>
{
ArgumentNullException.ThrowIfNull(messageFunc);
Expand All @@ -108,6 +111,8 @@ public static void ThrowIfInvalid<T>(T obj, Func<T, string> messageFunc)
ThrowValidationException(errors.Errors, messageFunc(obj));
}

#pragma warning restore CS8777 // Parameter must have a non-null value when exiting.

/// <summary>
/// Throws a <see cref="ValidationException"/> if there are any errors.
/// </summary>
Expand Down Expand Up @@ -143,9 +148,11 @@ public static void ThrowIfInvalid(ValidationResult errors, string message)
ThrowValidationException(errors.Errors, message);
}

[DoesNotReturn]
private static void ThrowValidationException(IReadOnlyList<ValidationError> errors) =>
throw new ValidationException(errors);

[DoesNotReturn]
private static void ThrowValidationException(IReadOnlyList<ValidationError> errors, string message) =>
throw new ValidationException(message, errors);
}