The obj == null check at the start of your method already covers the case when the root obj itself is null.
However, if by "root of obj", you mean that obj can be a complex object and some of its properties could be null or uninitialized and you want to check that recursively, then your current method only checks the top-level object obj itself.
Clarification:
- If
obj == null, you already do:
if (obj == null)
{
AddErrorWithTopic(path, ValidationErrorMessage.FieldRequired);
return;
}
- If
obj is a reference type but some of its properties are null, your method does not currently check the properties.
If you want to check nested properties of obj to verify if any required nested properties are null, then you must:
- Detect if
obj is a complex object (class or struct)
- Iterate over its properties
- Recursively call
CheckRequiredField for each property, passing a proper path (e.g., "root.PropertyName")
Example Recursive Approach
Here is a simple example how you could augment your method to recursively check nested properties to catch null properties "inside" the object:
private void CheckRequiredField(string path, object obj)
{
if (obj == null)
{
AddErrorWithTopic(path, ValidationErrorMessage.FieldRequired);
return;
}
Type type = obj.GetType();
// Handle different types accordingly
if (type == typeof(string))
{
string strValue = (string)obj;
if (string.IsNullOrEmpty(strValue))
{
AddErrorWithTopic(path, ValidationErrorMessage.FieldRequired);
}
}
else if (IsList(obj))
{
var list = (System.Collections.IList)obj;
if (list.Count == 0)
{
AddErrorWithTopic(path, ValidationErrorMessage.FieldRequired);
}
else
{
// Check each element in the list recursively
for (int i = 0; i < list.Count; i++)
{
CheckRequiredField($"{path}[{i}]", list[i]);
}
}
}
else if (IsNumericType(type))
{
dynamic value = Convert.ChangeType(obj, type);
if (value.Equals(Activator.CreateInstance(type)))
{
AddErrorWithTopic(path, ValidationErrorMessage.FieldRequired);
}
}
else if (type.IsClass || type.IsValueType)
{
// For complex objects - check properties recursively
var properties = type.GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);
foreach (var property in properties)
{
object propValue = property.GetValue(obj);
string propPath = string.IsNullOrEmpty(path) ? property.Name : $"{path}.{property.Name}";
CheckRequiredField(propPath, propValue);
}
}
else
{
// Unknown type, fallback error
AddErrorWithTopic(path, ValidationErrorMessage.FieldRequired);
}
}
Explanation
- When
obj is a complex object, you enumerate its public instance properties and check each recursively.
- If it's a list, you also check each element recursively.
- This way, a null property deep inside the object graph will also be detected and reported with the correct "path".
Summary
- Your current method does check if the root
obj is null.
- If you want to check nested properties for null, you need to recurse through the object graph as shown.
- This recursive approach prevents null exceptions when accessing nested properties.
- Make sure
IsList and IsNumericType methods are implemented correctly.
Let me know if you want me to help with implementations for IsList or IsNumericType!