VAB: IgnoreNulls for String.Empty

Topics: Validation Application Block
Mar 1, 2007 at 7:40 AM
The IgnoreNulls attribute should be expanded to ingore empty strings. This is especially important for WCF/DataContractSerializer, which will deserialize a missing optional string element DataMember(IsRequired=false) using the default value String.Empty which is not the same as null. Now it returns "value must be null" as the validation result for left-out data contract elements.

Also, what is the scenario for the NullValidator attribute ?

My current work-around is this:

StringLengthValidator(1, 64)
System.Runtime.Serialization.DataMemberAttribute(IsRequired = false, Name = "Revision")
public System.String Revision
get { return String.IsNullOrEmpty(_docRevision) ? null : _docRevision; }
set { _docRevision = value; }
Mar 13, 2007 at 4:53 PM
We are interested in something along these lines as well - maybe not making the IgnoreNulls include empty strings, but maybe an additional one IgnoreEmpty or IgnoreNullOrEmpty
Mar 13, 2007 at 4:59 PM
Yes, I have suggested adding IgnoreNullOrEmpty in the issue tracker item, where you can also find a "workaround" by TomH:
Mar 13, 2007 at 5:47 PM
Edited Mar 13, 2007 at 5:48 PM
No that workaround does not work. The reason is that the workaround suggests using the Or Validator Composition and then add a negated not null validator along with a 0 length string operator and then your normal validator.

So while that will handle the basic case, it does not handle the case where a value can be null/blank, but if not then there are complex ANDed conditions which must be met.

From previous conversation with Tom Hollander (, the point of the IgnoreNulls attribute was to "get around the limited flexibility for Boolean composition in attributes".

However, as pointed out at the top of this thread there are occasions where empty should be treated similarly to null hence it seems that there is a need not to replace the IgnoreNulls attribute, but to also add an IgnoreNullsOrEmpty attribute

(especially since .NET has String.IsNullOrEmpty function which shows that there are often cases (although not all) where empty and null should be treated similarly with strings)