Skip to content

Commit ab64d5a

Browse files
committed
created a generic Validators class for ValidateNotNull logic
1 parent caecaf7 commit ab64d5a

1 file changed

Lines changed: 35 additions & 77 deletions

File tree

src/SafeCrypt.Lib/Helpers/Validators.cs

Lines changed: 35 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -4,83 +4,7 @@
44
namespace SafeCrypt.Helpers
55
{
66
internal static class Validators
7-
{
8-
/// <summary>
9-
/// Validates that the specified ByteEncryptionParameters instance is not null
10-
/// and that its required properties (Data, SecretKey, IV) are not null.
11-
/// </summary>
12-
/// <param name="parameters">The ByteEncryptionParameters instance to validate.</param>
13-
/// <exception cref="ArgumentNullException">
14-
/// Thrown if the specified parameters instance is null or if any of its required properties are null.
15-
/// </exception>
16-
internal static void ValidateNotNull(EncryptionParameters parameters)
17-
{
18-
if (parameters == null)
19-
{
20-
throw new ArgumentNullException(nameof(parameters), "ByteEncryptionParameters instance cannot be null.");
21-
}
22-
23-
if (parameters.DataToEncrypt == null)
24-
{
25-
throw new ArgumentNullException(nameof(parameters.DataToEncrypt), "DataToEncrypt property cannot be null.");
26-
}
27-
28-
if (parameters.SecretKey == null)
29-
{
30-
throw new ArgumentNullException(nameof(parameters.SecretKey), "SecretKey property cannot be null.");
31-
}
32-
33-
if (parameters.IV == null)
34-
{
35-
throw new ArgumentNullException(nameof(parameters.IV), "IV property cannot be null.");
36-
}
37-
}
38-
internal static void ValidateNotNull(DecryptionParameters parameters)
39-
{
40-
if (parameters == null)
41-
{
42-
throw new ArgumentNullException(nameof(parameters), "ByteEncryptionParameters instance cannot be null.");
43-
}
44-
45-
if (parameters.DataToDecrypt == null)
46-
{
47-
throw new ArgumentNullException(nameof(parameters.DataToDecrypt), "DataToDecrypt property cannot be null.");
48-
}
49-
50-
if (parameters.SecretKey == null)
51-
{
52-
throw new ArgumentNullException(nameof(parameters.SecretKey), "SecretKey property cannot be null.");
53-
}
54-
55-
if (parameters.IV == null)
56-
{
57-
throw new ArgumentNullException(nameof(parameters.IV), "IV property cannot be null.");
58-
}
59-
}
60-
61-
internal static void ValidateNotNull(StringEncryptionParameters parameters)
62-
{
63-
if (parameters == null)
64-
{
65-
throw new ArgumentNullException(nameof(parameters), "StringEncryptionParameters instance cannot be null.");
66-
}
67-
68-
if (parameters.Data == null)
69-
{
70-
throw new ArgumentNullException(nameof(parameters.Data), "Data property cannot be null.");
71-
}
72-
73-
if (parameters.SecretKey == null)
74-
{
75-
throw new ArgumentNullException(nameof(parameters.SecretKey), "SecretKey property cannot be null.");
76-
}
77-
78-
if (parameters.IV == null)
79-
{
80-
throw new ArgumentNullException(nameof(parameters.IV), "IV property cannot be null.");
81-
}
82-
}
83-
7+
{
848
/// <summary>
859
/// Validates whether the provided string is a valid Base64-encoded key.
8610
/// </summary>
@@ -128,4 +52,38 @@ private static bool IsValidByteArray(byte[] byteArray)
12852
return byteArray != null && byteArray.Length > 0;
12953
}
13054
}
55+
56+
internal static class Validator<TParameters> where TParameters : BaseAesEncryptionParameters
57+
{
58+
/// <summary>
59+
/// Validates that the specified ByteEncryptionParameters instance is not null
60+
/// and that its required properties (Data, SecretKey, IV) are not null.
61+
/// </summary>
62+
/// <param name="parameters">The ByteEncryptionParameters instance to validate.</param>
63+
/// <exception cref="ArgumentNullException">
64+
/// Thrown if the specified parameters instance is null or if any of its required properties are null.
65+
/// </exception>
66+
internal static void ValidateNotNull(TParameters parameters)
67+
{
68+
if (parameters == null)
69+
{
70+
throw new ArgumentNullException(nameof(parameters), $"{typeof(TParameters).Name} instance cannot be null.");
71+
}
72+
73+
if (parameters.Data == null)
74+
{
75+
throw new ArgumentNullException(nameof(parameters.Data), "Data property cannot be null.");
76+
}
77+
78+
if (parameters.SecretKey == null)
79+
{
80+
throw new ArgumentNullException(nameof(parameters.SecretKey), "SecretKey property cannot be null.");
81+
}
82+
83+
if (parameters.IV == null)
84+
{
85+
throw new ArgumentNullException(nameof(parameters.IV), "IV property cannot be null.");
86+
}
87+
}
88+
}
13189
}

0 commit comments

Comments
 (0)