Skip to content

Commit 65aa57c

Browse files
committed
Fixing c# code for powershell 5.1 so it works properly.
1 parent 34317c1 commit 65aa57c

2 files changed

Lines changed: 95 additions & 51 deletions

File tree

PSFluentObjectValidation/PSFluentObjectValidation.psd1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
@{
22
RootModule = 'PSFluentObjectValidation.psm1'
3-
ModuleVersion = '1.0.0'
3+
ModuleVersion = '1.0.1'
44
GUID = '90ac3c83-3bd9-4da5-8705-7b82b21963c8'
55
Author = 'Joshua Wilson'
66
CompanyName = 'PwshDevs'

PSFluentObjectValidation/Private/PSFluentObjectValidation.ps1

Lines changed: 94 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,15 @@ public static class PSFluentObjectValidation
7070
{
7171
// First get the property (should be an array)
7272
if (!HasProperty(currentObject, propertyName))
73-
throw new InvalidOperationException($"Property '{propertyName}' does not exist");
73+
throw new InvalidOperationException(String.Format("Property '{0}' does not exist", propertyName));
7474
7575
object arrayObject = GetProperty(currentObject, propertyName);
7676
7777
if (arrayObject == null)
78-
throw new InvalidOperationException($"Property '{propertyName}' is null");
78+
throw new InvalidOperationException(String.Format("Property '{0}' is null", propertyName));
7979
8080
if (!IsArrayLike(arrayObject))
81-
throw new InvalidOperationException($"Property '{propertyName}' is not an array");
81+
throw new InvalidOperationException(String.Format("Property '{0}' is not an array", propertyName));
8282
8383
// Handle wildcard [*] - means all elements must exist and be valid
8484
if (indexStr == "*")
@@ -87,20 +87,21 @@ public static class PSFluentObjectValidation
8787
}
8888
8989
// Handle numerical index [0], [1], etc.
90-
if (int.TryParse(indexStr, out int index))
90+
int index;
91+
if (int.TryParse(indexStr, out index))
9192
{
9293
return ProcessNumericalAccess(arrayObject, propertyName, index);
9394
}
9495
95-
throw new InvalidOperationException($"Invalid array index '{indexStr}' for property '{propertyName}'");
96+
throw new InvalidOperationException(String.Format("Invalid array index '{0}' for property '{1}'", indexStr, propertyName));
9697
}
9798
9899
private static object ProcessWildcardAccess(object arrayObject, string propertyName)
99100
{
100101
int count = GetCount(arrayObject);
101102
102103
if (count == 0)
103-
throw new InvalidOperationException($"Array '{propertyName}' is empty - cannot validate [*]");
104+
throw new InvalidOperationException(String.Format("Array '{0}' is empty - cannot validate [*]", propertyName));
104105
105106
// For wildcard, we return the array object itself
106107
// The next part in the chain will validate against all elements
@@ -112,18 +113,25 @@ public static class PSFluentObjectValidation
112113
int count = GetCount(arrayObject);
113114
114115
if (index < 0 || index >= count)
115-
throw new InvalidOperationException($"Array index [{index}] is out of bounds for '{propertyName}' (length: {count})");
116+
throw new InvalidOperationException(String.Format("Array index [{0}] is out of bounds for '{1}' (length: {2})", index, propertyName, count));
116117
117118
// Get the specific element
118-
if (arrayObject is Array array)
119+
if (arrayObject is Array)
120+
{
121+
Array array = (Array)arrayObject;
119122
return array.GetValue(index);
123+
}
120124
121-
if (arrayObject is IList list)
125+
if (arrayObject is IList)
126+
{
127+
IList list = (IList)arrayObject;
122128
return list[index];
129+
}
123130
124131
// For IEnumerable, we need to iterate to the index
125-
if (arrayObject is IEnumerable enumerable)
132+
if (arrayObject is IEnumerable)
126133
{
134+
IEnumerable enumerable = (IEnumerable)arrayObject;
127135
int currentIndex = 0;
128136
foreach (object item in enumerable)
129137
{
@@ -133,22 +141,23 @@ public static class PSFluentObjectValidation
133141
}
134142
}
135143
136-
throw new InvalidOperationException($"Cannot access index [{index}] on array '{propertyName}'");
144+
throw new InvalidOperationException(String.Format("Cannot access index [{0}] on array '{1}'", index, propertyName));
137145
}
138146
139147
private static object ProcessPropertyWithValidation(object currentObject, string propertyName, char validator)
140148
{
141149
// Handle wildcard array wrapper first
142-
if (currentObject is WildcardArrayWrapper wrapper)
150+
if (currentObject is WildcardArrayWrapper)
143151
{
152+
WildcardArrayWrapper wrapper = (WildcardArrayWrapper)currentObject;
144153
return ProcessWildcardPropertyAccess(wrapper.ArrayObject, propertyName + validator);
145154
}
146155
147156
if (validator == '?')
148157
{
149158
// Handle object/array validation: key? - ONLY check existence, allow null values
150159
if (!HasProperty(currentObject, propertyName))
151-
throw new InvalidOperationException($"Property '{propertyName}' does not exist");
160+
throw new InvalidOperationException(String.Format("Property '{0}' does not exist", propertyName));
152161
153162
object value = GetProperty(currentObject, propertyName);
154163
@@ -158,7 +167,7 @@ public static class PSFluentObjectValidation
158167
{
159168
// For arrays, check that it's not empty
160169
if (GetCount(value) == 0)
161-
throw new InvalidOperationException($"Array '{propertyName}' is empty");
170+
throw new InvalidOperationException(String.Format("Array '{0}' is empty", propertyName));
162171
}
163172
// Note: We don't check IsObjectLike here because ? only validates existence
164173
@@ -168,27 +177,28 @@ public static class PSFluentObjectValidation
168177
{
169178
// Validate non-empty: key!
170179
if (!HasProperty(currentObject, propertyName))
171-
throw new InvalidOperationException($"Property '{propertyName}' does not exist");
180+
throw new InvalidOperationException(String.Format("Property '{0}' does not exist", propertyName));
172181
173182
object value = GetProperty(currentObject, propertyName);
174183
175184
if (value == null)
176-
throw new InvalidOperationException($"Property '{propertyName}' is null");
185+
throw new InvalidOperationException(String.Format("Property '{0}' is null", propertyName));
177186
178187
if (IsEmpty(value))
179-
throw new InvalidOperationException($"Property '{propertyName}' is empty or whitespace");
188+
throw new InvalidOperationException(String.Format("Property '{0}' is empty or whitespace", propertyName));
180189
181190
return value;
182191
}
183192
184-
throw new InvalidOperationException($"Unknown validator '{validator}'");
193+
throw new InvalidOperationException(String.Format("Unknown validator '{0}'", validator));
185194
}
186195
187196
private static object ProcessRegularProperty(object currentObject, string propertyName)
188197
{
189198
// Handle wildcard array wrapper - validate property exists on ALL elements
190-
if (currentObject is WildcardArrayWrapper wrapper)
199+
if (currentObject is WildcardArrayWrapper)
191200
{
201+
WildcardArrayWrapper wrapper = (WildcardArrayWrapper)currentObject;
192202
return ProcessWildcardPropertyAccess(wrapper.ArrayObject, propertyName);
193203
}
194204
@@ -204,7 +214,7 @@ public static class PSFluentObjectValidation
204214
205215
// Regular property navigation - allow null values (only ! operator should reject nulls)
206216
if (!HasProperty(currentObject, propertyName))
207-
throw new InvalidOperationException($"Property '{propertyName}' does not exist");
217+
throw new InvalidOperationException(String.Format("Property '{0}' does not exist", propertyName));
208218
209219
object value = GetProperty(currentObject, propertyName);
210220
@@ -227,72 +237,74 @@ public static class PSFluentObjectValidation
227237
}
228238
229239
// Validate ALL elements have this property
230-
if (arrayObject is Array array)
240+
if (arrayObject is Array)
231241
{
242+
Array array = (Array)arrayObject;
232243
for (int i = 0; i < array.Length; i++)
233244
{
234245
object element = array.GetValue(i);
235246
if (element == null)
236-
throw new InvalidOperationException($"Array element [{i}] is null");
247+
throw new InvalidOperationException(String.Format("Array element [{0}] is null", i));
237248
if (!HasProperty(element, actualPropertyName))
238-
throw new InvalidOperationException($"Array element [{i}] does not have property '{actualPropertyName}'");
249+
throw new InvalidOperationException(String.Format("Array element [{0}] does not have property '{1}'", i, actualPropertyName));
239250
240251
if (validator.HasValue)
241252
{
242253
object elementValue = GetProperty(element, actualPropertyName);
243254
if (elementValue == null)
244-
throw new InvalidOperationException($"Property '{actualPropertyName}' in element [{i}] is null");
255+
throw new InvalidOperationException(String.Format("Property '{0}' in element [{1}] is null", actualPropertyName, i));
245256
if (validator == '!' && IsEmpty(elementValue))
246-
throw new InvalidOperationException($"Property '{actualPropertyName}' in element [{i}] is empty");
257+
throw new InvalidOperationException(String.Format("Property '{0}' in element [{1}] is empty", actualPropertyName, i));
247258
}
248259
}
249260
return GetProperty(array.GetValue(0), actualPropertyName);
250261
}
251262
252-
if (arrayObject is IList list)
263+
if (arrayObject is IList)
253264
{
265+
IList list = (IList)arrayObject;
254266
for (int i = 0; i < list.Count; i++)
255267
{
256268
object element = list[i];
257269
if (element == null)
258-
throw new InvalidOperationException($"Array element [{i}] is null");
270+
throw new InvalidOperationException(String.Format("Array element [{0}] is null", i));
259271
if (!HasProperty(element, actualPropertyName))
260-
throw new InvalidOperationException($"Array element [{i}] does not have property '{actualPropertyName}'");
272+
throw new InvalidOperationException(String.Format("Array element [{0}] does not have property '{1}'", i, actualPropertyName));
261273
262274
if (validator.HasValue)
263275
{
264276
object elementValue = GetProperty(element, actualPropertyName);
265277
if (elementValue == null)
266-
throw new InvalidOperationException($"Property '{actualPropertyName}' in element [{i}] is null");
278+
throw new InvalidOperationException(String.Format("Property '{0}' in element [{1}] is null", actualPropertyName, i));
267279
if (validator == '!' && IsEmpty(elementValue))
268-
throw new InvalidOperationException($"Property '{actualPropertyName}' in element [{i}] is empty");
280+
throw new InvalidOperationException(String.Format("Property '{0}' in element [{1}] is empty", actualPropertyName, i));
269281
}
270282
}
271283
return GetProperty(list[0], actualPropertyName);
272284
}
273285
274-
throw new InvalidOperationException($"Cannot validate wildcard array");
286+
throw new InvalidOperationException(String.Format("Cannot validate wildcard array"));
275287
}
276288
277289
private static void ValidatePropertyValue(object value, string propertyName, char validator, int? arrayIndex = null)
278290
{
279-
string context = arrayIndex.HasValue ? $" in array element [{arrayIndex}]" : "";
291+
string context = arrayIndex.HasValue ? String.Format(" in array element [{0}]", arrayIndex) : "";
280292
281293
if (validator == '?')
282294
{
283295
if (value == null)
284-
throw new InvalidOperationException($"Property '{propertyName}'{context} is null");
296+
throw new InvalidOperationException(String.Format("Property '{0}'{1} is null", propertyName, context));
285297
286298
if (IsArrayLike(value) && GetCount(value) == 0)
287-
throw new InvalidOperationException($"Array '{propertyName}'{context} is empty");
299+
throw new InvalidOperationException(String.Format("Array '{0}'{1} is empty", propertyName, context));
288300
}
289301
else if (validator == '!')
290302
{
291303
if (value == null)
292-
throw new InvalidOperationException($"Property '{propertyName}'{context} is null");
304+
throw new InvalidOperationException(String.Format("Property '{0}'{1} is null", propertyName, context));
293305
294306
if (IsEmpty(value))
295-
throw new InvalidOperationException($"Property '{propertyName}'{context} is empty or whitespace");
307+
throw new InvalidOperationException(String.Format("Property '{0}'{1} is empty or whitespace", propertyName, context));
296308
}
297309
}
298310
@@ -301,14 +313,23 @@ public static class PSFluentObjectValidation
301313
{
302314
if (obj == null) return false;
303315
304-
if (obj is Hashtable hashtable)
316+
if (obj is Hashtable)
317+
{
318+
Hashtable hashtable = (Hashtable)obj;
305319
return hashtable.ContainsKey(propertyName);
320+
}
306321
307-
if (obj is IDictionary dictionary)
322+
if (obj is IDictionary)
323+
{
324+
IDictionary dictionary = (IDictionary)obj;
308325
return dictionary.Contains(propertyName);
326+
}
309327
310-
if (obj is PSObject psObj)
328+
if (obj is PSObject)
329+
{
330+
PSObject psObj = (PSObject)obj;
311331
return psObj.Properties[propertyName] != null;
332+
}
312333
313334
var type = obj.GetType();
314335
return type.GetProperty(propertyName) != null || type.GetField(propertyName) != null;
@@ -318,16 +339,23 @@ public static class PSFluentObjectValidation
318339
{
319340
if (obj == null) return null;
320341
321-
if (obj is Hashtable hashtable)
342+
if (obj is Hashtable)
343+
{
344+
Hashtable hashtable = (Hashtable)obj;
322345
return hashtable[propertyName];
346+
}
323347
324-
if (obj is IDictionary dictionary)
348+
if (obj is IDictionary)
349+
{
350+
IDictionary dictionary = (IDictionary)obj;
325351
return dictionary[propertyName];
352+
}
326353
327-
if (obj is PSObject psObj)
354+
if (obj is PSObject)
328355
{
356+
PSObject psObj = (PSObject)obj;
329357
var prop = psObj.Properties[propertyName];
330-
return prop?.Value;
358+
return prop != null ? prop.Value : null;
331359
}
332360
333361
var type = obj.GetType();
@@ -359,14 +387,21 @@ public static class PSFluentObjectValidation
359387
{
360388
if (obj == null) return 0;
361389
362-
if (obj is Array array)
390+
if (obj is Array)
391+
{
392+
Array array = (Array)obj;
363393
return array.Length;
394+
}
364395
365-
if (obj is ICollection collection)
396+
if (obj is ICollection)
397+
{
398+
ICollection collection = (ICollection)obj;
366399
return collection.Count;
400+
}
367401
368-
if (obj is IEnumerable enumerable)
402+
if (obj is IEnumerable)
369403
{
404+
IEnumerable enumerable = (IEnumerable)obj;
370405
int count = 0;
371406
foreach (var item in enumerable)
372407
count++;
@@ -380,14 +415,23 @@ public static class PSFluentObjectValidation
380415
{
381416
if (value == null) return true;
382417
383-
if (value is string str)
418+
if (value is string)
419+
{
420+
string str = (string)value;
384421
return string.IsNullOrWhiteSpace(str);
422+
}
385423
386-
if (value is Array array)
424+
if (value is Array)
425+
{
426+
Array array = (Array)value;
387427
return array.Length == 0;
428+
}
388429
389-
if (value is ICollection collection)
430+
if (value is ICollection)
431+
{
432+
ICollection collection = (ICollection)value;
390433
return collection.Count == 0;
434+
}
391435
392436
return value.Equals("");
393437
}
@@ -396,7 +440,7 @@ public static class PSFluentObjectValidation
396440
// Wrapper class to handle wildcard array processing
397441
public class WildcardArrayWrapper
398442
{
399-
public object ArrayObject { get; }
443+
public object ArrayObject { get; set; }
400444
401445
public WildcardArrayWrapper(object arrayObject)
402446
{

0 commit comments

Comments
 (0)