mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-02-24 11:14:53 +01:00
139 lines
4.6 KiB
C#
139 lines
4.6 KiB
C#
// Copyright (c) .NET Foundation. All rights reserved.
|
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using JetBrains.Annotations;
|
|
|
|
// Copied from https://github.com/aspnet/EntityFramework/blob/dev/src/Shared/Check.cs
|
|
namespace WireMock.Validation
|
|
{
|
|
// [ExcludeFromCodeCoverage]
|
|
[DebuggerStepThrough]
|
|
internal static class Check
|
|
{
|
|
[ContractAnnotation("value:null => halt")]
|
|
public static T Condition<T>([NoEnumeration] T value, [ValidatedNotNull, NotNull] Predicate<T> condition, [InvokerParameterName, ValidatedNotNull, NotNull] string parameterName)
|
|
{
|
|
NotNull(condition, nameof(condition));
|
|
NotNull(value, nameof(value));
|
|
|
|
if (!condition(value))
|
|
{
|
|
NotNullOrEmpty(parameterName, nameof(parameterName));
|
|
|
|
throw new ArgumentOutOfRangeException(parameterName);
|
|
}
|
|
|
|
return value;
|
|
}
|
|
|
|
[ContractAnnotation("value:null => halt")]
|
|
public static T NotNull<T>([NoEnumeration] T value, [InvokerParameterName, ValidatedNotNull, NotNull] string parameterName)
|
|
{
|
|
if (ReferenceEquals(value, null))
|
|
{
|
|
NotNullOrEmpty(parameterName, nameof(parameterName));
|
|
|
|
throw new ArgumentNullException(parameterName);
|
|
}
|
|
|
|
return value;
|
|
}
|
|
|
|
[ContractAnnotation("value:null => halt")]
|
|
public static T NotNull<T>([NoEnumeration] T value, [InvokerParameterName, ValidatedNotNull, NotNull] string parameterName, [ValidatedNotNull, NotNull] string propertyName)
|
|
{
|
|
if (ReferenceEquals(value, null))
|
|
{
|
|
NotNullOrEmpty(parameterName, nameof(parameterName));
|
|
NotNullOrEmpty(propertyName, nameof(propertyName));
|
|
|
|
throw new ArgumentException(CoreStrings.ArgumentPropertyNull(propertyName, parameterName));
|
|
}
|
|
|
|
return value;
|
|
}
|
|
|
|
[ContractAnnotation("value:null => halt")]
|
|
public static IList<T> NotNullOrEmpty<T>(IList<T> value, [InvokerParameterName, ValidatedNotNull, NotNull] string parameterName)
|
|
{
|
|
NotNull(value, parameterName);
|
|
|
|
if (value.Count == 0)
|
|
{
|
|
NotNullOrEmpty(parameterName, nameof(parameterName));
|
|
|
|
throw new ArgumentException(CoreStrings.CollectionArgumentIsEmpty(parameterName));
|
|
}
|
|
|
|
return value;
|
|
}
|
|
|
|
[ContractAnnotation("value:null => halt")]
|
|
public static string NotNullOrEmpty(string value, [InvokerParameterName, ValidatedNotNull, NotNull] string parameterName)
|
|
{
|
|
Exception e = null;
|
|
if (ReferenceEquals(value, null))
|
|
{
|
|
e = new ArgumentNullException(parameterName);
|
|
}
|
|
else if (value.Trim().Length == 0)
|
|
{
|
|
e = new ArgumentException(CoreStrings.ArgumentIsEmpty(parameterName));
|
|
}
|
|
|
|
if (e != null)
|
|
{
|
|
NotNullOrEmpty(parameterName, nameof(parameterName));
|
|
|
|
throw e;
|
|
}
|
|
|
|
return value;
|
|
}
|
|
|
|
//public static string NullButNotEmpty(string value, [InvokerParameterName, ValidatedNotNull, NotNull] string parameterName)
|
|
//{
|
|
// if (!ReferenceEquals(value, null)
|
|
// && (value.Length == 0))
|
|
// {
|
|
// NotNullOrEmpty(parameterName, nameof(parameterName));
|
|
|
|
// throw new ArgumentException(CoreStrings.ArgumentIsEmpty(parameterName));
|
|
// }
|
|
|
|
// return value;
|
|
//}
|
|
|
|
public static IList<T> HasNoNulls<T>(IList<T> value, [InvokerParameterName, ValidatedNotNull, NotNull] string parameterName)
|
|
where T : class
|
|
{
|
|
NotNull(value, parameterName);
|
|
|
|
if (value.Any(e => e == null))
|
|
{
|
|
NotNullOrEmpty(parameterName, nameof(parameterName));
|
|
|
|
throw new ArgumentException(parameterName);
|
|
}
|
|
|
|
return value;
|
|
}
|
|
|
|
//public static Type ValidEntityType(Type value, [InvokerParameterName, ValidatedNotNull, NotNull] string parameterName)
|
|
//{
|
|
// if (!value.GetTypeInfo().IsClass)
|
|
// {
|
|
// NotNullOrEmpty(parameterName, nameof(parameterName));
|
|
|
|
// throw new ArgumentException(CoreStrings.InvalidEntityType(value, parameterName));
|
|
// }
|
|
|
|
// return value;
|
|
//}
|
|
}
|
|
} |