I have correctly configured validation block using Enterprise Library Configuration Console.
My app.config file looks like the following (I only show here validation section):
<validation>
<type name="XFRop.Settings" defaultRuleset="All Settings Validation Ruleset"
assemblyName="XFRop, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
<ruleset name="All Settings Validation Ruleset">
<properties>
<property name="RemoteIPAddress">
<validator type="Microsoft.Practices.EnterpriseLibrary.Validation.Validators.RegexValidator, Microsoft.Practices.EnterpriseLibrary.Validation, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
pattern="\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b"
tag="RemoteIpAddress_Validator" name="Regular Expression Validator" />
</property>
<property name="RemotePort">
<validator type="Microsoft.Practices.EnterpriseLibrary.Validation.Validators.RangeValidator, Microsoft.Practices.EnterpriseLibrary.Validation, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
culture="es-ES" lowerBound="0" lowerBoundType="Inclusive"
upperBound="65535" tag="RemotePort_Range_Validator" name="Range Validator" />
</property>
<property name="Culture">
<validator type="Microsoft.Practices.EnterpriseLibrary.Validation.Validators.RegexValidator, Microsoft.Practices.EnterpriseLibrary.Validation, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
pattern="^[A-Za-z]{1,8}(-[A-Za-z0-9]{1,8})*$" tag="Culture_Validator"
name="Regular Expression Validator" />
</property>
</properties>
</ruleset>
</type>
</validation>
I have the following class in VB.NET (I will simplify my class into three properties here):
Public NotInheritable Class Settings
Private Shared _RemoteIPAddress As String
Private Shared _RemotePort As UShort
Private Shared _Culture As String
Public Shared Property RemoteIPAddress() As String
Get
Return _RemoteIPAddress
End Get
Set(ByVal value As String)
_RemoteIPAddress = value
End Set
End Property
Public Shared Property RemotePort() As UShort
Get
Return _RemotePort
End Get
Set(ByVal value As UShort)
_RemotePort = value
End Set
End Property
Public Shared Property Culture() As String
Get
Return _Culture
End Get
Set(ByVal value As String)
_Culture = value
End Set
End Property
End Class
Note that:
RemoteIPAddress is an IPv4 address so I build the following regular expressions to check it:
\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b
RemotePort is a property containing a port that ranges from 0-65535.
Culture is a property containing a language culture name so I build regular expression:
^[A-Za-z]{1,8}(-[A-Za-z0-9]{1,8})*$
Now, when my application starts I initialize my settings class as below:
Private Sub InitSettings()
Settings.RemoteIPAddress = My.Settings.RemoteIPAddress
Settings.RemotePort = My.Settings.RemotePort
Settings.Culture = My.Settings.Culture
End Sub
And after that I validate the settings in "Settings" class by using below validation code:
Public Sub Main()
' Initialize application settings
Me.InitSettings()
' Validate current application settings
results = Validation.Validate(New Settings)
If results.IsValid Then
' Initiate Background worker thread
Me.DoTask()
End If
End Sub
I have two problems with validation:
Validation is returning true (results.IsValid = true) but RemoteIPAddress property contains empty string. Why validation returns true if RemoteIPAddress does not complain regular expression? It is an empty string so it should return false instead of true. Note: I have configured Regular expression validator from console with Negated property set to the default: false.
results object contains the following information:
- Count has value 0
- IsValid has value true
- validationResults has value Count=0
- Expanding "Results" produces no results
It seems like validation is not being correctly applied so What am I doing wrong?