//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
namespace Microsoft.Kusto.ServiceLayer.Utility
{
using System;
using System.Collections.Generic;
using System.ComponentModel;
///
/// Represents validation utilities.
///
public static class ValidationUtils
{
///
/// Validates whether an argument is not null.
///
/// The parameter.
/// The parameter name.
public static void IsArgumentNotNull(object param, [Localizable(false)] string paramName)
{
if (param == null)
{
throw new ArgumentNullException(paramName);
}
}
///
/// Validates whether a string argument is not null or white space.
///
/// The parameter.
/// The parameter name.
public static void IsArgumentNotNullOrWhiteSpace(string param, [Localizable(false)] string paramName)
{
if (string.IsNullOrWhiteSpace(param))
{
throw new ArgumentNullException(paramName, $"{paramName} cannot be null or white space.");
}
}
///
/// Validates whether a string argument is null or empty.
///
/// The parameter.
/// The parameter name.
public static void IsArgumentNullOrEmpty(string param, [Localizable(false)] string paramName)
{
if (!string.IsNullOrEmpty(param))
{
throw new ArgumentException($"{paramName} must be null or empty.", paramName);
}
}
///
/// Validates whether an argument equals an expected value.
///
/// The type of the argument.
///
///
///
///
public static void IsArgumentNotEqual(T actual, T expected, [Localizable(false)] string paramName, IEqualityComparer comparer = null)
{
comparer = comparer ?? EqualityComparer.Default;
if (comparer.Equals(actual, expected))
{
throw new ArgumentException($"{paramName} is unsupported: '{actual}'.", paramName);
}
}
///
/// Validates whether an argument equals an expected value.
///
/// The type of the argument.
///
///
///
///
public static void IsArgumentEqual(T actual, T expected, [Localizable(false)] string paramName, IEqualityComparer comparer = null)
{
comparer = comparer ?? EqualityComparer.Default;
if (!comparer.Equals(actual, expected))
{
throw new ArgumentException($"{paramName} value is unexpected: actual '{actual}', expected '{expected}'.", paramName);
}
}
///
/// Validates whether an object is not null.
///
/// The object value.
/// The object name. May optionally include an exception message.
/// The object is null.
public static void IsNotNull(object value, [Localizable(false)] string name)
{
if (value == null)
{
throw new InvalidOperationException($"{name} cannot be null.");
}
}
///
/// Validates whether an object is not null.
///
/// The type of the exception to throw.
/// The object value.
/// The exception message.
public static void IsNotNull(object value, [Localizable(false)] string message)
where TException : Exception, new()
{
if (value == null)
{
throw (TException)Activator.CreateInstance(typeof(TException), message);
}
}
///
/// Validates whether a string is not null or white space.
///
/// The string value.
/// The string name. May optionally include an exception message.
/// The value is null or white-space.
public static void IsNotNullOrWhitespace(string value, [Localizable(false)] string name)
{
if (string.IsNullOrWhiteSpace(value))
{
throw new InvalidOperationException($"{name} cannot be null or white space.");
}
}
///
/// Validates whether a condition is true. Throws an exception if not.
///
/// The type of the exception.
/// The condition.
/// The exception message.
public static void IsTrue(bool condition, string message)
where T : Exception
{
if (!condition)
{
throw (T)Activator.CreateInstance(typeof(T), message);
}
}
}
}