| | | 1 | | // Copyright (c) .NET Foundation. All rights reserved. |
| | | 2 | | // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.Collections.Generic; |
| | | 6 | | using System.Diagnostics; |
| | | 7 | | using System.Linq; |
| | | 8 | | using System.Reflection; |
| | | 9 | | using JetBrains.Annotations; |
| | | 10 | | |
| | | 11 | | // Copied from https://github.com/aspnet/EntityFramework/blob/dev/src/Shared/Check.cs |
| | | 12 | | namespace WireMock.Validation |
| | | 13 | | { |
| | | 14 | | // [ExcludeFromCodeCoverage] |
| | | 15 | | [DebuggerStepThrough] |
| | | 16 | | internal static class Check |
| | | 17 | | { |
| | | 18 | | [ContractAnnotation("value:null => halt")] |
| | | 19 | | public static T Condition<T>([NoEnumeration] T value, [NotNull] Predicate<T> condition, [InvokerParameterName] [ |
| | 1 | 20 | | { |
| | 1 | 21 | | NotNull(condition, nameof(condition)); |
| | 1 | 22 | | NotNull(value, nameof(value)); |
| | | 23 | | |
| | 1 | 24 | | if (!condition(value)) |
| | 0 | 25 | | { |
| | 0 | 26 | | NotEmpty(parameterName, nameof(parameterName)); |
| | | 27 | | |
| | 0 | 28 | | throw new ArgumentOutOfRangeException(parameterName); |
| | | 29 | | } |
| | | 30 | | |
| | 1 | 31 | | return value; |
| | 1 | 32 | | } |
| | | 33 | | |
| | | 34 | | [ContractAnnotation("value:null => halt")] |
| | | 35 | | public static T NotNull<T>([NoEnumeration] T value, [InvokerParameterName] [NotNull] string parameterName) |
| | 499 | 36 | | { |
| | 499 | 37 | | if (ReferenceEquals(value, null)) |
| | 0 | 38 | | { |
| | 0 | 39 | | NotEmpty(parameterName, nameof(parameterName)); |
| | | 40 | | |
| | 0 | 41 | | throw new ArgumentNullException(parameterName); |
| | | 42 | | } |
| | | 43 | | |
| | 499 | 44 | | return value; |
| | 499 | 45 | | } |
| | | 46 | | |
| | | 47 | | [ContractAnnotation("value:null => halt")] |
| | | 48 | | public static T NotNull<T>( |
| | | 49 | | [NoEnumeration] T value, |
| | | 50 | | [InvokerParameterName] [NotNull] string parameterName, |
| | | 51 | | [NotNull] string propertyName) |
| | 0 | 52 | | { |
| | 0 | 53 | | if (ReferenceEquals(value, null)) |
| | 0 | 54 | | { |
| | 0 | 55 | | NotEmpty(parameterName, nameof(parameterName)); |
| | 0 | 56 | | NotEmpty(propertyName, nameof(propertyName)); |
| | | 57 | | |
| | 0 | 58 | | throw new ArgumentException(CoreStrings.ArgumentPropertyNull(propertyName, parameterName)); |
| | | 59 | | } |
| | | 60 | | |
| | 0 | 61 | | return value; |
| | 0 | 62 | | } |
| | | 63 | | |
| | | 64 | | [ContractAnnotation("value:null => halt")] |
| | | 65 | | public static IList<T> NotEmpty<T>(IList<T> value, [InvokerParameterName] [NotNull] string parameterName) |
| | 62 | 66 | | { |
| | 62 | 67 | | NotNull(value, parameterName); |
| | | 68 | | |
| | 62 | 69 | | if (value.Count == 0) |
| | 0 | 70 | | { |
| | 0 | 71 | | NotEmpty(parameterName, nameof(parameterName)); |
| | | 72 | | |
| | 0 | 73 | | throw new ArgumentException(CoreStrings.CollectionArgumentIsEmpty(parameterName)); |
| | | 74 | | } |
| | | 75 | | |
| | 62 | 76 | | return value; |
| | 62 | 77 | | } |
| | | 78 | | |
| | | 79 | | [ContractAnnotation("value:null => halt")] |
| | | 80 | | public static string NotEmpty(string value, [InvokerParameterName] [NotNull] string parameterName) |
| | 0 | 81 | | { |
| | 0 | 82 | | Exception e = null; |
| | 0 | 83 | | if (ReferenceEquals(value, null)) |
| | 0 | 84 | | { |
| | 0 | 85 | | e = new ArgumentNullException(parameterName); |
| | 0 | 86 | | } |
| | 0 | 87 | | else if (value.Trim().Length == 0) |
| | 0 | 88 | | { |
| | 0 | 89 | | e = new ArgumentException(CoreStrings.ArgumentIsEmpty(parameterName)); |
| | 0 | 90 | | } |
| | | 91 | | |
| | 0 | 92 | | if (e != null) |
| | 0 | 93 | | { |
| | 0 | 94 | | NotEmpty(parameterName, nameof(parameterName)); |
| | | 95 | | |
| | 0 | 96 | | throw e; |
| | | 97 | | } |
| | | 98 | | |
| | 0 | 99 | | return value; |
| | 0 | 100 | | } |
| | | 101 | | |
| | | 102 | | public static string NullButNotEmpty(string value, [InvokerParameterName] [NotNull] string parameterName) |
| | 0 | 103 | | { |
| | 0 | 104 | | if (!ReferenceEquals(value, null) |
| | 0 | 105 | | && (value.Length == 0)) |
| | 0 | 106 | | { |
| | 0 | 107 | | NotEmpty(parameterName, nameof(parameterName)); |
| | | 108 | | |
| | 0 | 109 | | throw new ArgumentException(CoreStrings.ArgumentIsEmpty(parameterName)); |
| | | 110 | | } |
| | | 111 | | |
| | 0 | 112 | | return value; |
| | 0 | 113 | | } |
| | | 114 | | |
| | | 115 | | public static IList<T> HasNoNulls<T>(IList<T> value, [InvokerParameterName] [NotNull] string parameterName) |
| | | 116 | | where T : class |
| | 0 | 117 | | { |
| | 0 | 118 | | NotNull(value, parameterName); |
| | | 119 | | |
| | 0 | 120 | | if (value.Any(e => e == null)) |
| | 0 | 121 | | { |
| | 0 | 122 | | NotEmpty(parameterName, nameof(parameterName)); |
| | | 123 | | |
| | 0 | 124 | | throw new ArgumentException(parameterName); |
| | | 125 | | } |
| | | 126 | | |
| | 0 | 127 | | return value; |
| | 0 | 128 | | } |
| | | 129 | | |
| | | 130 | | public static Type ValidEntityType(Type value, [InvokerParameterName] [NotNull] string parameterName) |
| | 0 | 131 | | { |
| | 0 | 132 | | if (!value.GetTypeInfo().IsClass) |
| | 0 | 133 | | { |
| | 0 | 134 | | NotEmpty(parameterName, nameof(parameterName)); |
| | | 135 | | |
| | 0 | 136 | | throw new ArgumentException(CoreStrings.InvalidEntityType(value, parameterName)); |
| | | 137 | | } |
| | | 138 | | |
| | 0 | 139 | | return value; |
| | 0 | 140 | | } |
| | | 141 | | } |
| | | 142 | | } |