I have an Address object that I am trying to validate data against using EntLib:
Given the following method:
<ValidatorComposition(CompositionType.And, Ruleset:="FraudAnalysis")> _
<NotNullValidator(MessageTemplate:="Billing address is required.", Ruleset:="FraudAnalysis")> _
<TypeConversionValidator(GetType(Address), MessageTemplate:="Billing address must be an address object.", Ruleset:="FraudAnalysis")> _
Public Property BillingAddress() As Address
Get
Return _BillingAddress
End Get
Set(ByVal value As Address)
_BillingAddress = value
End Set
End Property
I create an address object:
Address thisAddress = new Address();
thisAddress.Address1 = "12312 Long Street";
thisAddress.City = "Los Angeles";
thisAddress.State = "CA";
thisAddress.Zip = "93322";
// set billing address to address
cardX.BillingAddress = thisAddress;
So now at cardX.billingAddress = thisAddress, the BillingAddress property validator (GetType(Address)) should fire. It seems to fire, but returns this error:
Value to validate is not of the expected type: expected System.String but got Address instead.
Can anyone see the issue here / suggest a fix?
Thanks.
I would just get rid of the
ValidatorCompositionandTypeConversionValidatordeclarations as I think they are redundant here. That would get rid of your error and a couple of lines of code too.The property is already strongly-typed to the
Addressclass so there is no way you can set it in code to an object that isn’t anAddressor isn’t polymorphic withAddress– having an extra validator to check this is redundant.The default composition of validators is logical AND anyway, you only need to specify validator composition when you want to OR a group, or use more complex groups that combine AND / OR.