Skip to content

Commit

Permalink
Fix nullability annotations for HasType and DerivesFrom (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
flobernd authored Aug 4, 2023
1 parent c357ac4 commit e7645f7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 17 deletions.
32 changes: 16 additions & 16 deletions ZySharp.Validation/ValidateArgument.Types.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static partial class ValidateArgument
/// <param name="type">The expected type.</param>
/// <returns>The unmodified validator context.</returns>
[ExcludeFromCodeCoverage]
public static IValidatorContext<T> HasType<T>(this IValidatorContext<T> validator, Type type)
public static IValidatorContext<T?> HasType<T>(this IValidatorContext<T?> validator, Type type)
where T : class
{
ValidationInternals.ValidateNotNull(type, nameof(type));
Expand All @@ -39,14 +39,14 @@ public static IValidatorContext<T> HasType<T>(this IValidatorContext<T> validato
/// <param name="types">The allowed types.</param>
/// <returns>The unmodified validator context.</returns>
[ExcludeFromCodeCoverage]
public static IValidatorContext<T> HasType<T>(this IValidatorContext<T> validator, params Type[] types)
public static IValidatorContext<T?> HasType<T>(this IValidatorContext<T?> validator, params Type[] types)
{
ValidationInternals.ValidateNotNull(types, nameof(types));

return validator.Perform(() =>
{
var type = validator.Value?.GetType();
var isValidValue = types.Any(x => (type == x));
var isValidValue = Array.Exists(types, x => (type == x));
if (!isValidValue)
{
validator.SetArgumentException(
Expand All @@ -65,7 +65,7 @@ public static IValidatorContext<T> HasType<T>(this IValidatorContext<T> validato
/// <param name="type">The blacklisted type.</param>
/// <returns>The unmodified validator context.</returns>
[ExcludeFromCodeCoverage]
public static IValidatorContext<T> NotHasType<T>(this IValidatorContext<T> validator, Type type)
public static IValidatorContext<T?> NotHasType<T>(this IValidatorContext<T?> validator, Type type)
where T : class
{
ValidationInternals.ValidateNotNull(type, nameof(type));
Expand All @@ -89,14 +89,14 @@ public static IValidatorContext<T> NotHasType<T>(this IValidatorContext<T> valid
/// <param name="types">The blacklisted types.</param>
/// <returns>The unmodified validator context.</returns>
[ExcludeFromCodeCoverage]
public static IValidatorContext<T> NotHasType<T>(this IValidatorContext<T> validator, params Type[] types)
public static IValidatorContext<T?> NotHasType<T>(this IValidatorContext<T?> validator, params Type[] types)
{
ValidationInternals.ValidateNotNull(types, nameof(types));

return validator.Perform(() =>
{
var type = validator.Value?.GetType();
var isInvalidValue = types.Any(x => (type == x));
var isInvalidValue = Array.Exists(types, x => (type == x));
if (isInvalidValue)
{
validator.SetArgumentException(
Expand All @@ -115,7 +115,7 @@ public static IValidatorContext<T> NotHasType<T>(this IValidatorContext<T> valid
/// <param name="type">The parent type.</param>
/// <returns>The unmodified validator context.</returns>
[ExcludeFromCodeCoverage]
public static IValidatorContext<T> DerivesFrom<T>(this IValidatorContext<T> validator, Type type)
public static IValidatorContext<T?> DerivesFrom<T>(this IValidatorContext<T?> validator, Type type)
where T : class
{
ValidationInternals.ValidateNotNull(type, nameof(type));
Expand All @@ -129,7 +129,7 @@ public static IValidatorContext<T> DerivesFrom<T>(this IValidatorContext<T> vali
ValidationInternals.FormatName(validator.Path, null),
type.FullName));
}
});
}, false);
}

/// <summary>
Expand All @@ -140,22 +140,22 @@ public static IValidatorContext<T> DerivesFrom<T>(this IValidatorContext<T> vali
/// <param name="types">The allowed parent types.</param>
/// <returns>The unmodified validator context.</returns>
[ExcludeFromCodeCoverage]
public static IValidatorContext<T> DerivesFrom<T>(this IValidatorContext<T> validator, params Type[] types)
public static IValidatorContext<T?> DerivesFrom<T>(this IValidatorContext<T?> validator, params Type[] types)
{
ValidationInternals.ValidateNotNull(types, nameof(types));

return validator.Perform(() =>
{
var type = validator.Value?.GetType();
var isValidValue = types.Any(x => x.IsAssignableFrom(type));
var isValidValue = Array.Exists(types, x => x.IsAssignableFrom(type));
if (!isValidValue)
{
validator.SetArgumentException(
string.Format(CultureInfo.InvariantCulture, Resources.ArgumentMustBeDerivedFromTypes,
ValidationInternals.FormatName(validator.Path, null),
string.Join(", ", types.Select(x => $"'{x?.FullName ?? "null"}'").ToList())));
}
});
}, false);
}

/// <summary>
Expand All @@ -166,7 +166,7 @@ public static IValidatorContext<T> DerivesFrom<T>(this IValidatorContext<T> vali
/// <param name="type">The blacklisted parent types.</param>
/// <returns>The unmodified validator context.</returns>
[ExcludeFromCodeCoverage]
public static IValidatorContext<T> NotDerivesFrom<T>(this IValidatorContext<T> validator, Type type)
public static IValidatorContext<T?> NotDerivesFrom<T>(this IValidatorContext<T?> validator, Type type)
where T : class
{
ValidationInternals.ValidateNotNull(type, nameof(type));
Expand All @@ -180,7 +180,7 @@ public static IValidatorContext<T> NotDerivesFrom<T>(this IValidatorContext<T> v
ValidationInternals.FormatName(validator.Path, null),
type.FullName));
}
});
}, false);
}

/// <summary>
Expand All @@ -191,21 +191,21 @@ public static IValidatorContext<T> NotDerivesFrom<T>(this IValidatorContext<T> v
/// <param name="types">The blacklisted types.</param>
/// <returns>The unmodified validator context.</returns>
[ExcludeFromCodeCoverage]
public static IValidatorContext<T> NotDerivesFrom<T>(this IValidatorContext<T> validator, params Type[] types)
public static IValidatorContext<T?> NotDerivesFrom<T>(this IValidatorContext<T?> validator, params Type[] types)
{
ValidationInternals.ValidateNotNull(types, nameof(types));

return validator.Perform(() =>
{
var type = validator.Value?.GetType();
var isInvalidValue = types.Any(x => x.IsAssignableFrom(type));
var isInvalidValue = Array.Exists(types, x => x.IsAssignableFrom(type));
if (isInvalidValue)
{
validator.SetArgumentException(
string.Format(CultureInfo.InvariantCulture, Resources.ArgumentMustNotBeDerivedFromTypes,
ValidationInternals.FormatName(validator.Path, null),
string.Join(", ", types.Select(x => $"'{x?.FullName ?? "null"}'").ToList())));
}
});
}, false);
}
}
2 changes: 1 addition & 1 deletion ZySharp.Validation/ValidateArgument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static partial class ValidateArgument
/// <param name="name">The name of the argument to validate.</param>
/// <param name="action">The action to perform for the selected parameter.</param>
/// <returns>The value of the validated argument.</returns>
[return: NotNullIfNotNull("value")]
[return: NotNullIfNotNull(nameof(value))]
public static T? For<T>([ValidatedNotNull][NoEnumeration] T? value, string name, Action<IValidatorContext<T?>> action)
{
ValidationInternals.ValidateNotNull(name, nameof(name));
Expand Down

0 comments on commit e7645f7

Please sign in to comment.