Adding Validation Rules Using Configuration
Validation definitions can be defined using XML configuration. The mapping document is designed
to be readable and hand-editable.
Let's kick off with an example mapping:
XML Namespace
All XML mappings should declare the XML namespace shown. The schema is shipped with the deployed zip or
may be found in the ValidationFramework\validation-definition.xsd file in the solution folder.
Tip: to enable IntelliSense in VS.Net for mapping and configuration files copy the validation-definition.xsd
to <VS.NET installation directory>\Common7\Packages\schemas\xml.
Elements
-
validationMapping Element
The root element of any xml validation configuration.
Child Elements
-
class (at least 1 required) : one class element should exists for each type that required validation.
-
class Element
The xml definition of how to validate a class.
Attributes
-
typeName (required): The fully qualified .NET class name of the class to validate, including its assembly name.
Child Elements
-
property (optional) : one property element should exists for each property that required validation.
-
method (optional) : one method element should exists for each method that required validation.
-
property Element
The xml definition of how to validate a property.
Attributes
-
name (required): the name of the property of your class.
Child Elements
-
validator (at lease one required) : one rule element should exists for each that is being applied to the property.
-
method Element
The xml definition of how to validate a method.
Attributes
-
name (required): the name of the method of your class.
Child Elements
-
parameter (at lease one required) : parameter elements should only exists for each parameter that are being applied to.
-
overloadType (option) : if there are multiple method with the same name overloadType elements can be used to uniquely identify the methods.
-
parameter Element
The xml definition of how to validate a parameter.
Attributes
-
name (required): the name of the parameter of the method.
Child Elements
-
validator (at lease one required) : one rule element should exists for each that is being applied to the parameter.
-
overloadType Element
A helper element used to uniquely identify a method.
Attributes
-
typeName (required): the name of the method of your class.
-
validator Element
The xml definition of what to create.
Attributes
-
typeName (required): The fully qualified .NET class name of the IRuleConfigReader used to generate the IRule, including its assembly name.
-
errorMessage (optional): the error message.
-
ruleSet (optional): the rule set.
-
ANY ATTRIBUTE (optional): extra attributes can be defined which are then passed to the IRuleConfigReader for generate the IRule.
Child Elements
-
ANY ELEMENT (optional): extra elements can be defined which are then passed to the IRuleConfigReader for generate the IRule.
Included IRuleConfigReaders
-
ComparePropertyRuleConfigReader
Used to create a ComparePropertyRule
Note: this IRuleConfigReader can only be applied to properties. If it is applied to a member an System.InvalidOperationException will be thrown.
Sample
Extra Attributes
-
propertyToCompare (required): Used to populate ComparePropertyRule.PropertyToCompare.
-
compareOperator (required): Used to populate ComparePropertyRule.CompareOperator.
-
CompareStringRuleConfigReader
Used to create a CompareRule<T>
Sample
Extra Attributes
-
valueToCompare (required): Used to populate CompareRule<T>.ValueToCompare.
-
compareOperator (required): Used to populate CompareRule<T>.CompareOperator.
-
CompareRuleConfigReader
Used to create a CompareRule<T>
Sample
Extra Attributes
-
valueToCompare (required): Used to populate CompareRule<T>.ValueToCompare.
-
compareOperator (required): Used to populate CompareRule<T>.CompareOperator.
-
CustomRuleConfigReader
Used to create a CustomRule
Sample
Extra Attributes
-
ruleInterpretation (required): Used to populate CustomRule.RuleInterpretation.
-
validationMethod (required): Used to populate CustomRule.Handler.
-
validationTypeName (required): Used to populate CustomRule.Handler.
-
DateTimeConversionRuleConfigReader
Used to create a DateTimeConversionRule
Sample
Extra Attributes
-
dateTimeFormatInfoTypeName (optional): The name of the type to get the DateTimeFormatInfo from. Used to populate DateTimeConversionRule.DateTimeFormatInfo.
-
dateTimeFormatInfoPropertyName (optional): The name of property to get the DateTimeFormatInfo from. Used to populate DateTimeConversionRule.DateTimeFormatInfo.
-
dateTimeStyles (optional): Used to populate DateTimeConversionRule.DateTimeStyles. Defaults to DateTimeStyles.None.
-
EnumerableDuplicateRuleConfigReader
Used to create a EnumerableDuplicateRule
Sample
Extra Attributes
-
equalityComparerTypeName (optional): The type name for the type to get from. Used to populate EnumerableDuplicateRule.Comparer.
-
equalityComparerPropertyName (optional): The name of the static property to get from. Used to populate EnumerableDuplicateRule.Comparer.
-
EnumConversionRuleConfigReader
Used to create a EnumConversionRule
Sample
Extra Attributes
-
enumTypeName (required): The enum that will attempted to be converted to.
-
ignoreCase (optional): Whether to ignore case.
-
LengthCollectionRuleConfigReader
Used to create a LengthCollectionRule
Sample
Extra Attributes
-
excludeDuplicatesFromCount (optional): Used to populate LengthCollectionRule.ExcludeDuplicatesFromCount.
-
minimum (optional): Used to populate LengthRule.Maximum.
-
maximum (required): Used to populate LengthRule.Minimum.
-
LengthStringRuleConfigReader
Used to create a LengthStringRule
Sample
Extra Attributes
-
minimum (optional): Used to populate LengthRule.Maximum.
-
maximum (required): Used to populate LengthRule.Minimum.
-
trimWhiteSpace (required): Used to populate LengthStringRule.TrimWhiteSpace. Defaults to true.
-
NumberConversionRuleConfigReader
Used to create a NumberConversionRule
Sample
Extra Attributes
-
typeCode (required): Used to populate LengthRule.Maximum.
-
numberFormatInfoTypeName (optional): The name of the type to get the NumberFormatInfo from. Used to populate NumberConversionRule.NumberFormatInfo.
-
numberFormatInfoPropertyName (optional): The name of property to get the NumberFormatInfo from. Used to populate NumberConversionRule.NumberFormatInfo.
-
numberStyles (optional): Used to populate NumberConversionRule.NumberStyles. Defaults to null.
-
RangeStringRuleConfigReader
Used to create a RangeRule<T>
Sample
Extra Attributes
-
minimum (required): Used to populate RangeRule<T>.Maximum.
-
maximum (required): Used to populate RangeRule<T>.Minimum.
-
RangeRuleConfigReader
Used to create a RangeRule<T>
Sample
Extra Attributes
-
minimum (required): Used to populate RangeRule<T>.Maximum.
-
maximum (required): Used to populate RangeRule<T>.Minimum.
-
RegexRuleConfigReader
Used to create a RegexRule
Sample
Extra Attributes
-
validationExpression (required): Used to populate RegexRule.ValidationExpression.
-
regexOptions (optional): Used to populate RegexRule.RegexOptions.
-
RegexRuleConfigReader
Used to create a RegexRule
Sample
Extra Attributes
-
validationExpression (required): Used to populate RegexRule.ValidationExpression.
-
regexOptions (optional): Used to populate RegexRule.RegexOptions.
-
RequiredBoolRuleConfigReader
Used to create a RequiredBoolRule
Sample
-
RequiredEnumRuleConfigReader
Used to create a RequiredRule<T> that will validate an enum.
Sample
Extra Attributes
-
initialValue (optional): Used to populate RequiredRule<T>.InitialValue.
-
RequiredObjectRuleConfigReader
Used to create a RequiredRule<T> that will be validate an object.
Sample
Extra Elements
-
Any element (optional): Only 1 allowed. Used to populate RequiredRule<T>.InitialValue. It is converted to an object using a System.Xml.Serialization.XmlSerializer.
-
RequiredStringRuleConfigReader
Used to create a RequiredStringRule
Sample
Extra Attributes
-
initialValue (optional): Used to populate RequiredRule<T>.InitialValue.
-
trimWhiteSpace (optional): Used to populate RequiredStringRule.TrimWhiteSpace. Defaults to true.
-
ignoreCase (optional): Used to populate RequiredStringRule.IgnoreCase. Defaults to true.
-
RequiredRuleConfigReader
Used to create a RequiredRule<T>
Sample
Extra Attributes
-
initialValue (optional): Used to populate RequiredRule<T>.InitialValue.
Adding Xml Mapping Document
Adding any xml segment is done through the ConfigurationService. For a full list of the input types supported see the documentation of ConfigurationService.
The recommended way of adding an xml segment is by including it as an embedded resource. Then ConfigurationService.AddAssembly is called on the assembly in question. The ConfigurationService will then add the contents each resource that end with ".validation.xml".
It is also recommended that a ".validation.xml" file exists for each type that requires validation.
So the type "Person" would have a matching embedded resource file called "Person.validation.xml".
When you application starts you should call ConfigurationService.AddAssembly and pass the Assembly that "Person" exists in.
For example.
ConfigurationService.AddAssembly(Assembly.GetExecutingAssembly());
This example assumes that the "Person" embedded resources exists is the current executing assembly.