-
Notifications
You must be signed in to change notification settings - Fork 0
Quick Start
Sebastian Cygan edited this page Nov 8, 2020
·
4 revisions
First step to get started with Forvalidate is to provide some data model classes we want to be validated. Below, there is a vary simple sample of such a class.
Public Class Request
Private _createDate As Date
Public Property CreateDateR() As Date
Get
Return _createDate
End Get
Set(ByVal value As Date)
_createDate = value
End Set
End Property
Private _createUser As String
Public Property CreateUserR() As String
Get
Return _createUser
End Get
Set(ByVal value As String)
_createUser = value
End Set
End Property
Private _priority As Integer
Public Property Priority() As Integer
Get
Return _priority
End Get
Set(ByVal value As Integer)
_priority = value
End Set
End Property
Public Sub New(ByVal createDate As Date, ByVal createUser As String, ByVal priority As Integer)
Me.CreateDateR = createDate
Me.CreateUserR = createUser
Me.Priority = priority
End Sub
End ClassNext step is to precise are rules and conditions we want to be used to validate instances of the class. Here is where creating of the first validator happens. I think that the sample code below with comments included is self-descriptive enough. In Developer's Guide there are descriptions for all built-in conditions.
'This is the main namespace of thr Forvalidate framework
Imports Fortedo.ForValidate
'This is a namespace which allows developers to use fluent syntax when defining some validators
Imports Fortedo.ForValidate.Extensions
'Our validator should always inherit from FvValidatorBase
Public Class RequestValidator
Inherits FvValidatorBase
'Validadtor should be configured before any use of it, so it is done in the constructor
Public Sub New()
'Any rule for a single property is added with AddRule extension method with the property name specified
'Next, there is some condition chain (it can be a single one ore several executed one after another)
'We can provide some custom message when a condition is not meet
AddRule("CreateDateR") _
.NotEqual(#3/3/2010#) _
.Msg("pl", "Błędne $propertyName$")
'For this property we provides two condition which should be met
'We can specify several message (in different languages) for each condition
AddRule("CreateUserR") _
.NotEqual("user11") _
.Msg("pl", "Błędne $propertyName$") _
.Msg("en", "Error in $propertyName$") _
.Matches("^[a-zA-Z]{10,20}$") _
.Msg("pl", "Nieprawidłowy format $propertyName$") _
.Msg("en", "$propertyName$ has an incorrect format")
'As with multi-language messages, we can specify several display names for configured properties
'We use here another type of condition which lets you to use come custom function to check the rules
AddRule("Priority") _
.WithName("pl", "Priorytet") _
.Ensure(Function(p) p < 100) _
.Msg("pl", "Priorytet musi być mniejszy niż 100")
End Sub
End ClassWhen we have a data model class and a validator class prepared to be executed, we can use them, finally.
Imports Fortedo.ForValidate
Imports Fortedo.ForValidate.Extensions
Module Module1
Sub Main()
'We create some sample instance of the Request class
Dim request = New Request(#3/3/2010#, "user11", 120)
'We create an instance of the RequestValidadtor class
'One instance of a validator can be used to validated several objects
Dim validator = New RequestValidator()
'We use one of the methods to validate the entire Request object
Dim result = validator.Validate(request)
WriteResults(result, "Request validadtion")
'Another method to validated only the specified property from the Request object
result = validator.Validate(request, "CreateDateR")
WriteResults(result, "Request validadtion")
Console.ReadKey()
End Sub
'This is an helper method listing (in console) all validation results
Sub WriteResults(ByVal result As FvResult, ByVal comment As String)
Console.WriteLine(comment)
For Each e In result.Errors
Console.WriteLine(" [{0}]: {1}", e.OriginalPath, e.OriginalContent)
Console.WriteLine(" [{0}]: {1}", e.Path, e.Content)
Next
End Sub
End Module