<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://nhforge.org/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>Validator</title><link>http://nhforge.org/wikis/validator/default.aspx</link><description>Reference documentation for the NHibernate.Validator APIs.</description><dc:language>en-US</dc:language><generator>CommunityServer 2008.5 SP1 (Build: 31106.3070)</generator><item><title>NHibernate Validator 1.0.0 Documentation </title><link>http://nhforge.org/wikis/validator/nhibernate-validator-1-0-0-documentation/revision/0.aspx</link><pubDate>Wed, 18 Mar 2009 17:01:29 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:27</guid><dc:creator>Dario Quintana</dc:creator><description>Current revision posted to Validator by Dario Quintana on 18/03/2009 02:01:29 p.m.&lt;br /&gt;
&lt;h2&gt;NHibernate Validator 1.0.0 Documentation &lt;/h2&gt;
&lt;h1&gt;Preface&lt;/h1&gt;
&lt;div class="preface"&gt;
&lt;p&gt;Attributes are a very convenient and elegant way to specify invariant constraints for a domain model. You can, for example, express that a property should never be null, that the account balance should be strictly positive, etc. These domain model constraints are declared in the class itself by annotating its properties. A validator can then read them and check for constraint violations. The validation mechanism can be executed in different layers in your application without having to duplicate any of these rules (presentation layer, data access layer). Following the DRY principle, NHibernate Validator has been designed for that purpose. NHibernate Validator also lets you declare the same rules in XML files. Both approaches (attributes and XML) give us interesting possibilities for configuring our domain rules. &lt;/p&gt;
&lt;p&gt;NHibernate Validator works on two levels. First, it is able to check in-memory instances of a class for constraint violations. Second, it can apply the constraints to the NHibernate metamodel and incorporate them into the generated database schema.&lt;/p&gt;
&lt;p&gt;Each constraint attribute is associated to a validator implementation responsible for checking the constraint on the entity instance. A validator can also (optionally) apply the constraint to the NHibernate metamodel, allowing NHibernate to generate DDL that expresses the constraint. With the appropriate event listener, you can execute the checking operation on inserts and updates done by NHibernate. NHibernate Validator is not limited to only be used with NHibernate. You can easily use it anywhere in your application as well, indeed, you can use it at UI validation with Winforms or Asp.Net technologies.&lt;/p&gt;
&lt;p&gt;When checking instances at runtime, NHibernate Validator returns information about constraint violations in an array of &lt;i&gt;InvalidValue&lt;/i&gt;s. Among other information, the &lt;i&gt;InvalidValue&lt;/i&gt; contains an error description message that can embed the parameter values bundle with the annotation (eg. length limit), and message strings that may be externalized to a ResourceManager.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="chapter"&gt;
&lt;h1&gt;About the project&lt;/h1&gt;
&lt;p&gt;NHibernate Validator is a powerful and extensible framework to validate objects using the .Net Platform. Born as a port of &lt;span class="italic"&gt;Hibernate Validator 3.0.0&lt;/span&gt; project at december of 2007. Later on, many improvements and changes were made to the Core and API, and now we can say that NHibernate Validator is more than just a port. NHibernate Validator is entirely written in C#. The first release of NHibernate Validator was entirely developed by &lt;span class="italic"&gt;Dario Quintana&lt;/span&gt;, &lt;span class="italic"&gt;Gustavo Ringel&lt;/span&gt; and &lt;span class="italic"&gt;Fabio Maulo&lt;/span&gt;. We appreciate the help of &lt;span class="italic"&gt;Davy Brion&lt;/span&gt; in the revision of this document.&lt;/p&gt;
&lt;p&gt;NHibernate Validator is part of the &lt;a href="http://sourceforge.net/projects/nhcontrib/" target="_top"&gt;NHibernate Contrib&lt;/a&gt; project and you can download it&amp;nbsp;&lt;a href="http://sourceforge.net/project/showfiles.php?group_id=216446&amp;amp;package_id=275108&amp;amp;release_id=621966"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;You can ask questions about the development and utilization of NHibernate Validator on the following mailing lists:&lt;/p&gt;
&lt;p&gt;The development mailing list can be found &lt;a href="http://groups.google.com/group/nhcdevs" target="_top"&gt;here&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;The user forum can be found &lt;a href="http://groups.google.com/group/nhusers" target="_top"&gt;here&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;Bug and issues report should be sent to the development mailing list.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="chapter"&gt;
&lt;h1&gt;Using the Validator framework&lt;/h1&gt;
&lt;p&gt;NHibernate Validator is intended to be used to implement multi-layered data validation, where constraints are expressed in a single place and checked in various different layers of the application.&lt;/p&gt;
&lt;p&gt;This chapter will cover NHibernate Validator usage for different layers&lt;/p&gt;
&lt;div class="section"&gt;
&lt;h2 class="title" style="clear:both;"&gt;Database schema-level validation&lt;/h2&gt;
&lt;p&gt;Out of the box, NHibernate Validator will translate the constraints you have defined for your entities into mapping metadata. For example, if a property of your entity has the attribute [NotNull], its columns will be declared as &lt;i&gt;not null&lt;/i&gt; in the DDL schema generated by NHibernate.&lt;/p&gt;
&lt;p&gt;Using hbm2ddl, domain model constraints will be expressed into the database schema.&lt;/p&gt;
&lt;p&gt;If, for some reason, the feature needs to be disabled, set &lt;b&gt;&lt;i&gt;apply_to_ddl&lt;/i&gt;&lt;/b&gt; to false.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 class="title" style="clear:both;"&gt;ORM integration&lt;/h2&gt;
&lt;p&gt;NHibernate Validator integrates with NHibernate in a native way, and knows how to treat every persistent entity.&lt;/p&gt;
&lt;div class="section"&gt;
&lt;h3 class="title"&gt;NHibernate event-based validation&lt;/h3&gt;
&lt;p&gt;NHibernate Validator has two built-in NHibernate event listeners. Whenever a PreInsertEvent or PreUpdateEvent occurs, the listeners will verify all constraints of the entity instance and throw an exception if any of them are violated. Basically, objects will be checked before any insert and before any update triggered by NHibernate. This includes cascading changes! This is the most convenient and easiest way to activate the validation process. If a constraint is violated, the event will raise a runtime InvalidStateException which contains an array of &lt;i&gt;InvalidValue&lt;/i&gt;s describing each failure.&lt;/p&gt;
&lt;p&gt;If, for some reason, you want to disable this integration, set &lt;b&gt;autoregister_listeners&lt;/b&gt;&amp;nbsp;to false&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;span style="text-decoration:underline;"&gt;Note&lt;/span&gt;: If the entities are not annotated with validation attributes, there is no runtime performance cost.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;In case you wish to set manually the event listeners for NHibernate Core, use the following configuration in &lt;b&gt;hibernate.cfg.xml&lt;/b&gt;:&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    ...&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;event&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;type&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;pre-update&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;listener&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;NHibernate.Validator.Event.ValidatePreUpdateEventListener, NHibernate.Validator&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;event&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;event&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;type&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;pre-insert&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;listener&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;NHibernate.Validator.Event.ValidatePreInsertEventListener, NHibernate.Validator&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;event&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 class="title" style="clear:both;"&gt;Application-level validation&lt;/h2&gt;
&lt;p&gt;In order to interact with NHibernate Validator in your application you must use the main class: &lt;i&gt;ValidatorEngine&lt;/i&gt;. &lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;ValidatorEngine validator = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ValidatorEngine();&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; isValid = validator.IsValid(customer);&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;These two lines are enough to validate an object with its validation rules, which can be configured using Attributes or XML. The first line is the instantiation of the validator engine of NHibernate Validator. The second line is the validation of an object, and the result of the &lt;i&gt;IsValid(...)&lt;/i&gt;method indicates if the object is valid or not. In order to get the invalid values of an object, you can do:&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;ValidatorEngine validator = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ValidatorEngine();&lt;br /&gt;InvalidValue[] validationMessages = validator.GetInvalidValues(customer);&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Now instead of only knowing if the object is valid or not, we get an array of InvalidValues. If this array length is greater than zero then there are validation errors and you can traverse the InvalidValue array to get verbose information about the problems that cause the object to be invalid. &lt;/p&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 class="title" style="clear:both;"&gt;Presentation layer validation&lt;/h2&gt;
&lt;p&gt;[&lt;b&gt;&lt;span style="color:#008000;"&gt;TODO&lt;/span&gt;&lt;/b&gt;]&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 class="title" style="clear:both;"&gt;Validation informations&lt;/h2&gt;
&lt;p&gt;NHibernate Validator provides an array of InvalidValue. Each &lt;i&gt;InvalidValue&lt;/i&gt;&amp;nbsp;has several methods describing the reasons that caused the entity to be invalid.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;BeanClass&lt;/b&gt; retrieves the failing bean type&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Bean&amp;nbsp;&lt;/b&gt;retrieves the failing instance (if any ie not when using &lt;i&gt;GetPotentialInvalidValues()&lt;/i&gt;)&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Value&lt;/b&gt;&amp;nbsp;retrieves the failing value&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Message&lt;/b&gt;&amp;nbsp;retrieves the proper internationalized error message&lt;/p&gt;
&lt;p&gt;&lt;b&gt;RootBean&lt;/b&gt;&amp;nbsp;retrieves the root bean instance generating the issue (useful in conjunction with &lt;i&gt;[Valid]&lt;/i&gt;), is null if &lt;i&gt;GetPotentialInvalidValues(...)&lt;/i&gt; is used.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;PropertyPath&lt;/b&gt;&amp;nbsp;retrieves the dotted path of the failing property starting from the root bean&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class="chapter"&gt;
&lt;h1&gt;Defining constraints&lt;/h1&gt;
&lt;div class="section"&gt;
&lt;h2 class="title" style="clear:both;"&gt;What is a constraint?&lt;/h2&gt;
&lt;p&gt;A constraint is a rule that a given element (field, property or type) has to comply to. The rule semantic is expressed by an Attribute or configured in Xml files. A constraint may have some properties to configure the associated validator, for example a Min constraint will have a value property setting the minimum acceptable value for the element to be valid. &lt;/p&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 class="title" style="clear:both;"&gt;Built in constraints&lt;/h2&gt;
&lt;p&gt;Hibernate Validator comes with some built-in constraints, which cover most of the basic data checks. As we&amp;#39;ll see later, you&amp;#39;re not limited to them, you can literally in a minute write your own constraints.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Table Built-in constraints&lt;/b&gt;&lt;/p&gt;
&amp;lt;tbody&amp;gt;
&amp;lt;/tbody&amp;gt;
&lt;table bgcolor="#ffffc8" cellpadding="2" cellspacing="1"&gt;
&amp;lt;tbody&amp;gt;
&amp;lt;tbody&amp;gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;b&gt;Constraint&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;b&gt;Apply on&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;b&gt;Runtime checking&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;b&gt;NHibernate Metadata impact&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;b&gt;Length(Min=, Max=)&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property (String)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;checks if the string length is between the min-max range&lt;/span&gt;&lt;/td&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;Column length will be set to max&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;b&gt;Max(Value=)&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property (numeric or string representation of a numeric)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;checks if the value is less than or equals to max&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;Add a check constraint on the column&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;b&gt;Min(Value=)&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property (numeric or string representation of a numeric)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;checks if the value is more than or equals to min&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;Add a check constraint on the column&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;b&gt;NotNull&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;checks if the value is not null&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;Column(s) are not null&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;b&gt;NotEmpty&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;checks if the string is not empty.&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;b&gt;NotEmptyOrNull&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;Checks if the string is not null or not empty.&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;b&gt;Past&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property (DateTime)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;check if the date is in the past&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;b&gt;Future&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property (DateTime)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;check if the date is in the future&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;b&gt;Pattern(Regex=&amp;quot;regexp&amp;quot;, Flag=)&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property (string)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;check if the property matches the regular expression given a match flag. The &lt;span style="font-family:&amp;#39;Trebuchet MS&amp;#39;;"&gt;&amp;lt;tt&amp;gt;Flag&amp;lt;/tt&amp;gt; parameter is a &lt;i&gt;System.Text.RegularExpressions.RegexOptions&lt;/i&gt;&amp;nbsp;enum. You can also declare multiple Pattern attributes on the same member.&lt;/span&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;b&gt;Range(Min=, Max=)&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property (numeric or string representation of a numeric)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;check if the value is between Min and Max (included)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;b&gt;Size(Min=, Max=)&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property (array, collection, map)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;check if the element size is between Min and Max (included)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;b&gt;AssertFalse&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;check that the method evaluates to false (useful for constraints expressed in code rather than attributes)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;b&gt;AssertTrue&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;check that the method evaluates to true (useful for constraints expressed in code rather than attributes)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;b&gt;Valid&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property (object)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;perform validation recursively on the associated object. If the object is a Collection or an array, the elements are validated recursively. If the object is a Map, the value elements are validated recursively.&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;b&gt;Email&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property (String)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;check whether the string conforms to the email address specification&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;b&gt;Digits&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property (numeric or string representation of a numeric)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;checks whether the property is a number having up to&amp;nbsp;&lt;span style="font-family:&amp;#39;Trebuchet MS&amp;#39;;"&gt;&lt;i&gt;integerDigits&lt;/i&gt; integer digits and &lt;i&gt;fractionalDigits&lt;/i&gt;&amp;nbsp;fractonal digits&lt;/span&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;define column precision and scale&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&amp;lt;/tbody&amp;gt;
&amp;lt;/tbody&amp;gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 class="title" style="clear:both;"&gt;Error messages&lt;/h2&gt;
&lt;p&gt;NHibernate Validator comes with a default set of error messages translated in several languages (if yours is not present, please send us a patch). You can add your own additional set of messages while writing your validator Attributes or at XML. If NHibernate Validator cannot resolve a key from your ResourceManager nor from ValidatorMessage, it falls back to the default built-in values.&lt;/p&gt;
&lt;p&gt;Alternatively you can provide a ResourceManager while checking programmatically the validation rules on a bean or if you want a completly different interpolation mechanism, you can provide an implementation of NHibernate.Validator.Engine.IMessageInterpolator.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 class="title" style="clear:both;"&gt;Writing your own constraints&lt;/h2&gt;
&lt;p&gt;Extending the set of built-in constraints is extremely easy. Any constraint consists of two pieces: the constraint &lt;span class="emphasis"&gt;&lt;i&gt;descriptor&lt;/i&gt;&lt;/span&gt; (the attribute) and the constraint &lt;span class="emphasis"&gt;&lt;i&gt;validator&lt;/i&gt;&lt;/span&gt; (the implementation class). Here is a simple user-defined descriptor:&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]&lt;br /&gt;[ValidatorClass(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt;(PhoneValidator))]&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; PhoneAttribute : Attribute, IRuleArgs&lt;br /&gt;{&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; message = &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt;.Empty;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Message&lt;br /&gt;    {&lt;br /&gt;        get { &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; message; }&lt;br /&gt;        set { message = &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;; }&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;To link a descriptor to its validator implementation, we use the &lt;i&gt;[ValidatorClass]&lt;/i&gt;&amp;nbsp;meta-attribute. Here you can see an implementation of a constraint validator:&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; PhoneValidator : IValidator&lt;br /&gt;{&lt;br /&gt;     &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; IsValid(&lt;span style="color:#0000ff;"&gt;object&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;)&lt;br /&gt;     {&lt;br /&gt;         Regex regex = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Regex(&lt;span style="color:#006080;"&gt;@&amp;quot;^[2-9]\d{2}-\d{3}-\d{4}$&amp;quot;&lt;/span&gt;);&lt;br /&gt;         &lt;span style="color:#0000ff;"&gt;if&lt;/span&gt; (&lt;span style="color:#0000ff;"&gt;value&lt;/span&gt; == &lt;span style="color:#0000ff;"&gt;null&lt;/span&gt;) &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;;&lt;br /&gt;         &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; regex.IsMatch(&lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;.ToString());&lt;br /&gt;     }&lt;br /&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;This PhoneValidator class is simple. The method &lt;i&gt;IsValid()&lt;/i&gt;&amp;nbsp;receives a string that should be validated with a Regex expression. The &lt;i&gt;IsValid()&lt;/i&gt;&amp;nbsp;method should return false if the constraint has been violated.&lt;/p&gt;
&lt;p&gt;If you need to initialize the class validator you can implement the interface &lt;i&gt;IInitializableValidator&lt;/i&gt;&lt;i&gt;&amp;lt;A&amp;gt;&amp;nbsp;&lt;/i&gt;and the class should look like this:&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; PhoneValidator : IInitializableValidator&amp;lt;PhoneAttribute&amp;gt;&lt;br /&gt;{&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; Regex regex;&lt;br /&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; IsValid(&lt;span style="color:#0000ff;"&gt;object&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;)&lt;br /&gt;    {&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;if&lt;/span&gt; (&lt;span style="color:#0000ff;"&gt;value&lt;/span&gt; == &lt;span style="color:#0000ff;"&gt;null&lt;/span&gt;) &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;;&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; regex.IsMatch(&lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;.ToString());&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Initialize(PhoneAttribute parameters)&lt;br /&gt;    {&lt;br /&gt;        regex = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Regex(&lt;span style="color:#006080;"&gt;@&amp;quot;^[2-9]\d{2}-\d{3}-\d{4}$&amp;quot;&lt;/span&gt;);&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;As you can see, implementing the &lt;i&gt;IInitializableValidator&amp;lt;A&amp;gt;&lt;/i&gt;&amp;nbsp;interface we have to implement the method &lt;i&gt;Initialize(...)&lt;/i&gt; that initializes the validator, and it&amp;#39;s executed first of all.&lt;/p&gt;
&lt;p&gt;The &lt;i&gt;IsValid()&lt;/i&gt;&amp;nbsp;method should return &lt;i&gt;false&lt;/i&gt;&amp;nbsp;if the constraint has been violated. For more examples, refer to the built-in validator implementations.&lt;/p&gt;
&lt;p&gt;We now have to implement the validator (ie. the rule checking implementation). A validation implementation can check the value of the a property (by implementing PropertyConstraint ) and/or can modify the NHibernate mapping metadata to express the constraint at the database level (by implementing PersistentClassConstraint)&lt;/p&gt;
&lt;p&gt;We have only seen property level validation, but you can write a class level validation attribute. Instead of receiving the return instance of a property, the bean itself will be passed to the validator. To activate the validation checking, just put the validation attribute to the class itself instead of in a property. A small sample can be found in the unit test suite.&lt;/p&gt;
&lt;p&gt;If your constraint can be applied multiple times (with different parameters) on the same property or type, you can use set the &lt;i&gt;AllowMultiple&lt;/i&gt;&amp;nbsp;parameter to &lt;i&gt;true&lt;/i&gt;&amp;nbsp;on the &lt;i&gt;AttributeUsage&lt;/i&gt;&amp;nbsp;attribute. An example of this it&amp;#39;s the built-in validator Pattern, have a look:&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;[Serializable]&lt;br /&gt;[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = &lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;)]&lt;br /&gt;[ValidatorClass(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt; (PatternValidator))]&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; PatternAttribute : Attribute, IRuleArgs&lt;br /&gt;{&lt;br /&gt;    &amp;hellip;&lt;br /&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;pre class="programlisting"&gt;&amp;nbsp;&lt;/pre&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 class="title" style="clear:both;"&gt;Configuring your domain model with .Net Attributes&lt;/h2&gt;
&lt;p&gt;Since you are already familiar with attributes now, the syntax should be very familiar&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; Address&lt;br /&gt;{ &lt;br /&gt;    &lt;span style="color:#008000;"&gt;// a static non null field  &lt;/span&gt;&lt;br /&gt;    [NotNull]&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;static&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; blacklistedZipCode;&lt;br /&gt;    &lt;br /&gt;    &lt;span style="color:#008000;"&gt;// field a not null string of 20 characters maximum&lt;/span&gt;&lt;br /&gt;    [Length(Max = 20), NotNull]    &lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; country;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;long&lt;/span&gt; id;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; internalValid = &lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; line1;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; line2;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; state;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; zip;&lt;br /&gt;    &lt;br /&gt;    &lt;span style="color:#008000;"&gt;// a numeric between 1 and 2000&lt;/span&gt;&lt;br /&gt;    [Min(1), Range(Max = 2000)]&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;long&lt;/span&gt; Id&lt;br /&gt;    {&lt;br /&gt;        get { &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; id; }&lt;br /&gt;        set { id = &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;; }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;span style="color:#008000;"&gt;// respond to the constraint in the field: country&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Country&lt;br /&gt;    {&lt;br /&gt;        get { &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; country; }&lt;br /&gt;        set { country = &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;; }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;span style="color:#008000;"&gt;// a non null string&lt;/span&gt;&lt;br /&gt;    [NotNull]&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Line1&lt;br /&gt;    {&lt;br /&gt;        get { &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; line1; }&lt;br /&gt;        set { line1 = &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;; }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;span style="color:#008000;"&gt;// a not null string of 3 characters maximum&lt;/span&gt;&lt;br /&gt;    [Length(Max = 3), NotNull]&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; State&lt;br /&gt;    {&lt;br /&gt;        get { &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; state; }&lt;br /&gt;        set { state = &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;; }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;span style="color:#008000;"&gt;// a not null numeric string of 5 characters maximum&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#008000;"&gt;// if the string is longer, the message will&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#008000;"&gt;// be searched in the resource bundle at key &amp;#39;long&amp;#39;&lt;/span&gt;&lt;br /&gt;    [Length(Max = 5, Message = &lt;span style="color:#006080;"&gt;&amp;quot;{long}&amp;quot;&lt;/span&gt;)]&lt;br /&gt;    [Pattern(Regex = &lt;span style="color:#006080;"&gt;&amp;quot;[0-9]+&amp;quot;&lt;/span&gt;)]&lt;br /&gt;    [NotNull]&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Zip&lt;br /&gt;    {&lt;br /&gt;&lt;br /&gt;        get { &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; zip; }&lt;br /&gt;        set { zip = &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;; }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;span style="color:#008000;"&gt;// no constraint&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Line2&lt;br /&gt;    {&lt;br /&gt;        get { &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; line2; }&lt;br /&gt;        set { line2 = &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;; }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;span style="color:#008000;"&gt;// should always be true&lt;/span&gt;&lt;br /&gt;    [AssertTrue]&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; InternalValid&lt;br /&gt;    {&lt;br /&gt;        get { &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; internalValid; }&lt;br /&gt;        set { internalValid = &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;; }&lt;br /&gt;    }&lt;br /&gt;} &lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;While the example only shows public property validation, you can also annotate fields of any kind of visibility. In the example &lt;b&gt;MyBeanConstraint&lt;/b&gt; is a custom Class Validator, we will talk about this later.&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;[MyBeanConstraint(Max=45)]&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; Dog &lt;br /&gt;{&lt;br /&gt;    [AssertTrue] &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; IsMale;&lt;br /&gt;    [NotNull] &lt;span style="color:#0000ff;"&gt;protected&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Name { get {...} set{...} }&lt;br /&gt;    &amp;hellip;&lt;br /&gt;}     &lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;You can also annotate interfaces. NHibernate Validator will check all superclasses and interfaces extended or implemented by a given type to read the appropriate validator attributes.&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;interface&lt;/span&gt; Named &lt;br /&gt;{&lt;br /&gt;    [NotNull] &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Name {get; set;}&lt;br /&gt;    ...&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; Dog : Named &lt;br /&gt;{&lt;br /&gt;    [AssertTrue] &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; isMale;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Name { get {...} set{...} }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;The name property will be checked for nullity when the Dog type is validated.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 class="title" style="clear:both;"&gt;Configuring your domain model with XML&lt;/h2&gt;
&lt;p&gt;You also can configurate your domain model with constraints using xml files: the nhv.xml files. We prefer to embed these Xml files into the assemblies, and NHibernate Validator is prepared to inspect them and retrieve the resources with constrains, like NHibernate does with hbm.xml files.&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#800000;"&gt;xml&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;version&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;1.0&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;encoding&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;utf-8&amp;quot;&lt;/span&gt; ?&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;nhv-mapping&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;urn:nhibernate-validator-1.0&amp;quot;&lt;/span&gt;&lt;br /&gt;                        &lt;span style="color:#ff0000;"&gt;namespace&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;NHibernate.Validator.Tests.Base&amp;quot;&lt;/span&gt;&lt;br /&gt;                        &lt;span style="color:#ff0000;"&gt;assembly&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;NHibernate.Validator.Tests&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;class&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Address&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;blacklistedZipCode&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;                  &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;not-null&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;country&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;           &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;length&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;max&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;20&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;           &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;not-null&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;    &lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;floor&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;    &lt;br /&gt;          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;range&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;min&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;-2&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;max&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;50&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;message&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;{floor.out.of.range} (escaping #{el})&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Id&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;    &lt;br /&gt;          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;min&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;value&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;1&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;range&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;max&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;2000&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;max&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;value&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;2500&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Line1&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;    &lt;br /&gt;          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;not-null&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;State&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;    &lt;br /&gt;          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;not-null&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;length&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;max&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;3&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Zip&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;length&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;max&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;5&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;message&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;{long}&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;pattern&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;regex&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;[0-9]+&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;not-null&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;InternalValid&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;    &lt;br /&gt;          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;asserttrue&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;nhv-mapping&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;      &lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;span class="bold"&gt;&lt;b&gt;We strongly recommend&lt;/b&gt;&lt;/span&gt; to validate every nhv.xml file of your application with the respective XSD file. Every &lt;b&gt;nhv.xml&lt;/b&gt; file should be validated with the &lt;b&gt;nhv-mapping.xsd&lt;/b&gt;&amp;nbsp;file included within the NHibernate Validator distribution whether in source or binaries. Indeed, if you are using an IDE like Visual Studio it offers IntelliSense support in the &lt;b&gt;nhv.xml&lt;/b&gt; file if you add the schema to Visual Studio&amp;#39;s xml schemas folder.&lt;/p&gt;
&lt;p&gt;&lt;span class="bold"&gt;&lt;b&gt;Xml beauties:&lt;/b&gt;&lt;/span&gt; The nicest part of Xml for validation is that you can put xml validators in a different dll so you don&amp;#39;t need to recompile your domain when validation rules change. Only the dll with the Xml files..&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class="chapter"&gt;
&lt;h1&gt;Configuration&lt;/h1&gt;
&lt;p&gt;NHibernate Validator has global parameters that you can configure at the startup. This configuration can be done in differents ways: programmaticaly or using xml files. If you are involved with NHibernate this could be very familiar to you.&lt;/p&gt;
&lt;p&gt;Configuration properties:&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Table Configuration properties&lt;/b&gt;&lt;/p&gt;
&lt;div class="table"&gt;
&amp;lt;tbody&amp;gt;
&amp;lt;/tbody&amp;gt;
&lt;table bgcolor="#ffffc8" cellpadding="2" cellspacing="1"&gt;
&amp;lt;tbody&amp;gt;
&amp;lt;tbody&amp;gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;th align="middle"&gt;Property name&lt;/th&gt;
&lt;th&gt;Purpose&lt;/th&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="middle"&gt;&lt;b&gt;apply_to_ddl&lt;/b&gt;&lt;/td&gt;
&lt;td&gt;Configure NHibernate to apply the constraints to the DDL.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="middle"&gt;&lt;b&gt;autoregister_listeners&lt;/b&gt;&lt;/td&gt;
&lt;td&gt;Register the Event-Listeners automatically on Inserts and Updates.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="middle"&gt;&lt;b&gt;default_validator_mode&lt;/b&gt;&lt;/td&gt;
&lt;td&gt;Define what are the constraints sources are and how to proceed. There are some built-in configurations:
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;div class="itemizedlist"&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;b&gt;UseAttribute&lt;/b&gt; (default): Use Attributes to configurate the validators on types and/or members. Xml is ignored.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;b&gt;UseXml&lt;/b&gt;: Use Xml to configurate the validators on types and/or members. Attributes are ignored.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;b&gt;OverrideAttributeWithXml&lt;/b&gt;: If the same type/member is annotated with an Attribute and has a Xml rule the Attribute annotation will be used.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;b&gt;OverrideXmlWithAttribute&lt;/b&gt;: If the same type/member is annotated with an Attribute and has a Xml rule the Attribute annotation will be used.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="middle"&gt;&lt;b&gt;assembly&lt;/b&gt;&lt;/td&gt;
&lt;td&gt;Which are the assemblies with contains the nhv.xml files embedded. You can have more than one assembly.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="middle"&gt;&lt;b&gt;shared_engine_provider&lt;/b&gt;&lt;/td&gt;
&lt;td&gt;To inject a custom Shared Engine Provider.&lt;/td&gt;
&lt;/tr&gt;
&amp;lt;/tbody&amp;gt;
&amp;lt;/tbody&amp;gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 class="title" style="clear:both;"&gt;Programmatic configuration&lt;/h2&gt;
&lt;p&gt;You can use the following code to configure NHibernate.Validator programmatically:&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NHibernate.Validator.Cfg;&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NHibernate.Validator.Engine;&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; Environment=NHibernate.Validator.Cfg.Environment;&lt;br /&gt;&amp;hellip;&lt;br /&gt;ValidatorEngine ve = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ValidatorEngine();&lt;br /&gt;&lt;br /&gt;NHVConfiguration nhvc = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; NHVConfiguration();&lt;br /&gt;nhvc.Properties[Environment.ApplyToDDL] = &lt;span style="color:#006080;"&gt;&amp;quot;false&amp;quot;&lt;/span&gt;;&lt;br /&gt;nhvc.Properties[Environment.AutoregisterListeners] = &lt;span style="color:#006080;"&gt;&amp;quot;false&amp;quot;&lt;/span&gt;;&lt;br /&gt;nhvc.Properties[Environment.ValidatorMode] = GetMode();&lt;br /&gt;nhvc.Mappings.Add(&lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; MappingConfiguration(&lt;span style="color:#006080;"&gt;&amp;quot;NHibernate.Validator.Demo.Model&amp;quot;&lt;/span&gt;,&lt;span style="color:#0000ff;"&gt;null&lt;/span&gt;));&lt;br /&gt;ve.Configure(nhvc);  &lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;The &lt;i&gt;ValidatorEngine&lt;/i&gt;&amp;nbsp;will be set with the configuration. The NHibernate Validator configuration is represented by the &lt;i&gt;NHibernate.Validator.Cfg.NHVConfiguration&lt;/i&gt;&amp;nbsp;type. Other class used to configure all the stuff is the &lt;i&gt;NHibernate.Validator.Cfg.Environment&lt;/i&gt;&amp;nbsp;type, this class contain the string with the names of the properties of the configuration of NHibernate Validator.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 class="title" style="clear:both;"&gt;Declarative configuration: using Xml files&lt;/h2&gt;
&lt;p&gt;The declarative configuration keeps configuration out of the code. This means configuring the application using a different file, in this case a XML file. But the options don&amp;#39;t end here: you can choose to configure NHibernate Validator using a simple Xml file or you can put the configuration in the well known App.Config/Web.Config. &lt;/p&gt;
&lt;div class="section"&gt;
&lt;h3 class="title"&gt;Simple xml file&lt;/h3&gt;
&lt;p&gt;This is a sample of a simple configuration in a xml file:&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#800000;"&gt;xml&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;version&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;1.0&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;encoding&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;utf-8&amp;quot;&lt;/span&gt; ?&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;nhv-configuration&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;#39;urn:nhv-configuration-1.0&amp;#39;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;#39;apply_to_ddl&amp;#39;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;true&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;#39;autoregister_listeners&amp;#39;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;true&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;#39;default_validator_mode&amp;#39;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;UseXml&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;mapping&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;assembly&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;#39;NHibernate.Validator.Demo.Model&amp;#39;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;nhv-configuration&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;We encourage to name the configuration file &lt;b&gt;nhvalidator.cfg.xml&lt;/b&gt;, because NHibernate Validator will pick up this file from the application directory in order to get the configuration parameters, however you can name this file as you wish.&lt;/p&gt;
&lt;p&gt;In order to configurate NHibernate Validator with the &lt;b&gt;nhvalidator.cfg.xml&lt;/b&gt;&amp;nbsp;you can use the following codeו:&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;ValidatorEngine engine = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ValidatorEngine();&lt;br /&gt;engine.Configure(); &lt;span style="color:#008000;"&gt;// by convention reads the nhvalidator.cfg.xml file.&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;You can choose another name for your configuration file and proceed of this manner: &lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;ValidatorEngine engine = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ValidatorEngine();&lt;br /&gt;engine.Configure(&lt;span style="color:#006080;"&gt;&amp;quot;path/to/MyNHVConfigurationFile.xml&amp;quot;&lt;/span&gt;); &lt;span style="color:#008000;"&gt;// you can use whatever path to point to the configuration file&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h3 class="title"&gt;Configuration Application file: app.config/web.config&lt;/h3&gt;
&lt;p&gt;The configuration can be declared into our configuration application .Net files: app.config or web.config. In the below example you will see how to do it.&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#800000;"&gt;xml&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;version&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;1.0&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;encoding&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;utf-8&amp;quot;&lt;/span&gt; ?&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;configuration&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;configSections&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;section&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;nhv-configuration&amp;quot;&lt;/span&gt; &lt;br /&gt;                            &lt;span style="color:#ff0000;"&gt;type&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;NHibernate.Validator.Cfg.ConfigurationSectionHandler, NHibernate.Validator&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;        ...&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;configSections&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    ...&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;nhv-configuration&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;#39;urn:nhv-configuration-1.0&amp;#39;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;shared_engine_provider&lt;/span&gt; &lt;br /&gt;         &lt;span style="color:#ff0000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;#39;NHibernate.Validator.Event.NHibernateSharedEngineProvider, NHibernate.Validator&amp;#39;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;nhv-configuration&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    ...&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;configuration&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;      &lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;</description></item><item><title>NHibernate Validator 1.0.0 Documentation </title><link>http://nhforge.org/wikis/validator/nhibernate-validator-1-0-0-documentation/revision/10.aspx</link><pubDate>Mon, 09 Feb 2009 19:53:30 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:253</guid><dc:creator>Tobin Harris</dc:creator><description>Revision 10 posted to Validator by Tobin Harris on 09/02/2009 04:53:30 p.m.&lt;br /&gt;
&lt;h2&gt;NHibernate Validator 1.0.0 Documentation &lt;/h2&gt;
&lt;h1&gt;Preface&lt;/h1&gt;
&lt;div class="preface"&gt;
&lt;p&gt;Attributes are a very convenient and elegant way to specify invariant constraints for a domain model. You can, for example, express that a property should never be null, that the account balance should be strictly positive, etc. These domain model constraints are declared in the class itself by annotating its properties. A validator can then read them and check for constraint violations. The validation mechanism can be executed in different layers in your application without having to duplicate any of these rules (presentation layer, data access layer). Following the DRY principle, NHibernate Validator has been designed for that purpose. NHibernate Validator also lets you declare the same rules in XML files. Both approaches (attributes and XML) give us interesting possibilities for configuring our domain rules. &lt;/p&gt;
&lt;p&gt;NHibernate Validator works on two levels. First, it is able to check in-memory instances of a class for constraint violations. Second, it can apply the constraints to the NHibernate metamodel and incorporate them into the generated database schema.&lt;/p&gt;
&lt;p&gt;Each constraint attribute is associated to a validator implementation responsible for checking the constraint on the entity instance. A validator can also (optionally) apply the constraint to the NHibernate metamodel, allowing NHibernate to generate DDL that expresses the constraint. With the appropriate event listener, you can execute the checking operation on inserts and updates done by NHibernate. NHibernate Validator is not limited to only be used with NHibernate. You can easily use it anywhere in your application as well, indeed, you can use it at UI validation with Winforms or Asp.Net technologies.&lt;/p&gt;
&lt;p&gt;When checking instances at runtime, NHibernate Validator returns information about constraint violations in an array of &lt;i&gt;InvalidValue&lt;/i&gt;s. Among other information, the &lt;i&gt;InvalidValue&lt;/i&gt; contains an error description message that can embed the parameter values bundle with the annotation (eg. length limit), and message strings that may be externalized to a ResourceManager.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="chapter"&gt;
&lt;h1&gt;About the project&lt;/h1&gt;
&lt;p&gt;NHibernate Validator is a powerful and extensible framework to validate objects using the .Net Platform. Born as a port of &lt;span class="italic"&gt;Hibernate Validator 3.0.0&lt;/span&gt; project at december of 2007. Later on, many improvements and changes were made to the Core and API, and now we can say that NHibernate Validator is more than just a port. NHibernate Validator is entirely written in C#. The first release of NHibernate Validator was entirely developed by &lt;span class="italic"&gt;Dario Quintana&lt;/span&gt;, &lt;span class="italic"&gt;Gustavo Ringel&lt;/span&gt; and &lt;span class="italic"&gt;Fabio Maulo&lt;/span&gt;. We appreciate the help of &lt;span class="italic"&gt;Davy Brion&lt;/span&gt; in the revision of this document.&lt;/p&gt;
&lt;p&gt;NHibernate Validator is part of the &lt;a href="http://sourceforge.net/projects/nhcontrib/" target="_top"&gt;NHibernate Contrib&lt;/a&gt; project and you can download it&amp;nbsp;&lt;a href="http://sourceforge.net/project/showfiles.php?group_id=216446&amp;amp;package_id=275108&amp;amp;release_id=621966"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;You can ask questions about the development and utilization of NHibernate Validator on the following mailing lists:&lt;/p&gt;
&lt;p&gt;The development mailing list can be found &lt;a href="http://groups.google.com/group/nhcdevs" target="_top"&gt;here&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;The user forum can be found &lt;a href="http://groups.google.com/group/nhusers" target="_top"&gt;here&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;Bug and issues report should be sent to the development mailing list.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="chapter"&gt;
&lt;h1&gt;Using the Validator framework&lt;/h1&gt;
&lt;p&gt;NHibernate Validator is intended to be used to implement multi-layered data validation, where constraints are expressed in a single place and checked in various different layers of the application.&lt;/p&gt;
&lt;p&gt;This chapter will cover NHibernate Validator usage for different layers&lt;/p&gt;
&lt;div class="section"&gt;
&lt;h2 class="title" style="clear:both;"&gt;Database schema-level validation&lt;/h2&gt;
&lt;p&gt;Out of the box, NHibernate Validator will translate the constraints you have defined for your entities into mapping metadata. For example, if a property of your entity has the attribute [NotNull], its columns will be declared as &lt;i&gt;not null&lt;/i&gt; in the DDL schema generated by NHibernate.&lt;/p&gt;
&lt;p&gt;Using hbm2ddl, domain model constraints will be expressed into the database schema.&lt;/p&gt;
&lt;p&gt;If, for some reason, the feature needs to be disabled, set &lt;b&gt;&lt;i&gt;apply_to_ddl&lt;/i&gt;&lt;/b&gt; to false.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 class="title" style="clear:both;"&gt;ORM integration&lt;/h2&gt;
&lt;p&gt;NHibernate Validator integrates with NHibernate in a native way, and knows how to treat every persistent entity.&lt;/p&gt;
&lt;div class="section"&gt;
&lt;h3 class="title"&gt;NHibernate event-based validation&lt;/h3&gt;
&lt;p&gt;NHibernate Validator has two built-in NHibernate event listeners. Whenever a PreInsertEvent or PreUpdateEvent occurs, the listeners will verify all constraints of the entity instance and throw an exception if any of them are violated. Basically, objects will be checked before any insert and before any update triggered by NHibernate. This includes cascading changes! This is the most convenient and easiest way to activate the validation process. If a constraint is violated, the event will raise a runtime InvalidStateException which contains an array of &lt;i&gt;InvalidValue&lt;/i&gt;s describing each failure.&lt;/p&gt;
&lt;p&gt;If, for some reason, you want to disable this integration, set &lt;b&gt;autoregister_listeners&lt;/b&gt;&amp;nbsp;to false&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;span style="text-decoration:underline;"&gt;Note&lt;/span&gt;: If the entities are not annotated with validation attributes, there is no runtime performance cost.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;In case you wish to set manually the event listeners for NHibernate Core, use the following configuration in &lt;b&gt;hibernate.cfg.xml&lt;/b&gt;:&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    ...&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;event&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;type&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;pre-update&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;listener&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;NHibernate.Validator.Event.ValidatePreUpdateEventListener, NHibernate.Validator&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;event&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;event&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;type&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;pre-insert&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;listener&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;NHibernate.Validator.Event.ValidatePreInsertEventListener, NHibernate.Validator&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;event&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 class="title" style="clear:both;"&gt;Application-level validation&lt;/h2&gt;
&lt;p&gt;In order to interact with NHibernate Validator in your application you must use the main class: &lt;i&gt;ValidatorEngine&lt;/i&gt;. &lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;ValidatorEngine validator = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ValidatorEngine();&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; isValid = validator.IsValid(customer);&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;These two lines are enough to validate an object with its validation rules, which can be configured using Attributes or XML. The first line is the instantiation of the validator engine of NHibernate Validator. The second line is the validation of an object, and the result of the &lt;i&gt;IsValid(...)&lt;/i&gt;method indicates if the object is valid or not. In order to get the invalid values of an object, you can do:&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;ValidatorEngine validator = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ValidatorEngine();&lt;br /&gt;InvalidValue[] validationMessages = validator.GetInvalidValues(customer);&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Now instead of only knowing if the object is valid or not, we get an array of InvalidValues. If this array length is greater than zero then there are validation errors and you can traverse the InvalidValue array to get verbose information about the problems that cause the object to be invalid. &lt;/p&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 class="title" style="clear:both;"&gt;Presentation layer validation&lt;/h2&gt;
&lt;p&gt;[&lt;b&gt;&lt;span style="color:#008000;"&gt;TODO&lt;/span&gt;&lt;/b&gt;]&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 class="title" style="clear:both;"&gt;Validation informations&lt;/h2&gt;
&lt;p&gt;NHibernate Validator provides an array of InvalidValue. Each &lt;i&gt;InvalidValue&lt;/i&gt;&amp;nbsp;has several methods describing the reasons that caused the entity to be invalid.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;BeanClass&lt;/b&gt; retrieves the failing bean type&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Bean&amp;nbsp;&lt;/b&gt;retrieves the failing instance (if any ie not when using &lt;i&gt;GetPotentialInvalidValues()&lt;/i&gt;)&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Value&lt;/b&gt;&amp;nbsp;retrieves the failing value&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Message&lt;/b&gt;&amp;nbsp;retrieves the proper internationalized error message&lt;/p&gt;
&lt;p&gt;&lt;b&gt;RootBean&lt;/b&gt;&amp;nbsp;retrieves the root bean instance generating the issue (useful in conjunction with &lt;i&gt;[Valid]&lt;/i&gt;), is null if &lt;i&gt;GetPotentialInvalidValues(...)&lt;/i&gt; is used.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;PropertyPath&lt;/b&gt;&amp;nbsp;retrieves the dotted path of the failing property starting from the root bean&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class="chapter"&gt;
&lt;h1&gt;Defining constraints&lt;/h1&gt;
&lt;div class="section"&gt;
&lt;h2 class="title" style="clear:both;"&gt;What is a constraint?&lt;/h2&gt;
&lt;p&gt;A constraint is a rule that a given element (field, property or type) has to comply to. The rule semantic is expressed by an Attribute or configured in Xml files. A constraint may have some properties to configure the associated validator, for example a Min constraint will have a value property setting the minimum acceptable value for the element to be valid. &lt;/p&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 class="title" style="clear:both;"&gt;Built in constraints&lt;/h2&gt;
&lt;p&gt;Hibernate Validator comes with some built-in constraints, which cover most of the basic data checks. As we&amp;#39;ll see later, you&amp;#39;re not limited to them, you can literally in a minute write your own constraints.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Table Built-in constraints&lt;/b&gt;&lt;/p&gt;
&amp;lt;tbody&amp;gt;
&amp;lt;/tbody&amp;gt;
&lt;table bgcolor="#ffffc8" cellpadding="2" cellspacing="1"&gt;
&amp;lt;tbody&amp;gt;
&amp;lt;tbody&amp;gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;b&gt;Constraint&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;b&gt;Apply on&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;b&gt;Runtime checking&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;b&gt;NHibernate Metadata impact&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;b&gt;Length(Min=, Max=)&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property (String)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;checks if the string length is between the min-max range&lt;/span&gt;&lt;/td&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;Column length will be set to max&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;b&gt;Max(Value=)&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property (numeric or string representation of a numeric)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;checks if the value is less than or equals to max&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;Add a check constraint on the column&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;b&gt;Min(Value=)&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property (numeric or string representation of a numeric)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;checks if the value is more than or equals to min&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;Add a check constraint on the column&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;b&gt;NotNull&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;checks if the value is not null&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;Column(s) are not null&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;b&gt;NotEmpty&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;checks if the string is not empty.&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;b&gt;NotEmptyOrNull&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;Checks if the string is not null or not empty.&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;b&gt;Past&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property (DateTime)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;check if the date is in the past&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;b&gt;Future&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property (DateTime)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;check if the date is in the future&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;b&gt;Pattern(Regex=&amp;quot;regexp&amp;quot;, Flag=)&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property (string)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;check if the property matches the regular expression given a match flag. The &lt;span style="font-family:&amp;#39;Trebuchet MS&amp;#39;;"&gt;&amp;lt;tt&amp;gt;Flag&amp;lt;/tt&amp;gt; parameter is a &lt;i&gt;System.Text.RegularExpressions.RegexOptions&lt;/i&gt;&amp;nbsp;enum. You can also declare multiple Pattern attributes on the same member.&lt;/span&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;b&gt;Range(Min=, Max=)&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property (numeric or string representation of a numeric)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;check if the value is between Min and Max (included)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;b&gt;Size(Min=, Max=)&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property (array, collection, map)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;check if the element size is between Min and Max (included)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;b&gt;AssertFalse&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;check that the method evaluates to false (useful for constraints expressed in code rather than attributes)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;b&gt;AssertTrue&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;check that the method evaluates to true (useful for constraints expressed in code rather than attributes)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;b&gt;Valid&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property (object)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;perform validation recursively on the associated object. If the object is a Collection or an array, the elements are validated recursively. If the object is a Map, the value elements are validated recursively.&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;b&gt;Email&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property (String)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;check whether the string conforms to the email address specification&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;b&gt;Digits&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property (numeric or string representation of a numeric)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;checks whether the property is a number having up to&amp;nbsp;&lt;span style="font-family:&amp;#39;Trebuchet MS&amp;#39;;"&gt;&lt;i&gt;integerDigits&lt;/i&gt; integer digits and &lt;i&gt;fractionalDigits&lt;/i&gt;&amp;nbsp;fractonal digits&lt;/span&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;define column precision and scale&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&amp;lt;/tbody&amp;gt;
&amp;lt;/tbody&amp;gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 class="title" style="clear:both;"&gt;Error messages&lt;/h2&gt;
&lt;p&gt;NHibernate Validator comes with a default set of error messages translated in several languages (if yours is not present, please send us a patch). You can add your own additional set of messages while writing your validator Attributes or at XML. If NHibernate Validator cannot resolve a key from your ResourceManager nor from ValidatorMessage, it falls back to the default built-in values.&lt;/p&gt;
&lt;p&gt;Alternatively you can provide a ResourceManager while checking programmatically the validation rules on a bean or if you want a completly different interpolation mechanism, you can provide an implementation of NHibernate.Validator.Engine.IMessageInterpolator.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 class="title" style="clear:both;"&gt;Writing your own constraints&lt;/h2&gt;
&lt;p&gt;Extending the set of built-in constraints is extremely easy. Any constraint consists of two pieces: the constraint &lt;span class="emphasis"&gt;&lt;i&gt;descriptor&lt;/i&gt;&lt;/span&gt; (the attribute) and the constraint &lt;span class="emphasis"&gt;&lt;i&gt;validator&lt;/i&gt;&lt;/span&gt; (the implementation class). Here is a simple user-defined descriptor:&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]&lt;br /&gt;[ValidatorClass(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt;(PhoneValidator))]&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; PhoneAttribute : Attribute, IRuleArgs&lt;br /&gt;{&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; message = &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt;.Empty;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Message&lt;br /&gt;    {&lt;br /&gt;        get { &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; message; }&lt;br /&gt;        set { message = &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;; }&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;To link a descriptor to its validator implementation, we use the &lt;i&gt;[ValidatorClass]&lt;/i&gt;&amp;nbsp;meta-attribute. Here you can see an implementation of a constraint validator:&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; PhoneValidator : IValidator&lt;br /&gt;{&lt;br /&gt;     &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; IsValid(&lt;span style="color:#0000ff;"&gt;object&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;)&lt;br /&gt;     {&lt;br /&gt;         Regex regex = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Regex(&lt;span style="color:#006080;"&gt;@&amp;quot;^[2-9]\d{2}-\d{3}-\d{4}$&amp;quot;&lt;/span&gt;);&lt;br /&gt;         &lt;span style="color:#0000ff;"&gt;if&lt;/span&gt; (&lt;span style="color:#0000ff;"&gt;value&lt;/span&gt; == &lt;span style="color:#0000ff;"&gt;null&lt;/span&gt;) &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;;&lt;br /&gt;         &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; regex.IsMatch(&lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;.ToString());&lt;br /&gt;     }&lt;br /&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;This PhoneValidator class is simple. The method &lt;i&gt;IsValid()&lt;/i&gt;&amp;nbsp;receives a string that should be validated with a Regex expression. The &lt;i&gt;IsValid()&lt;/i&gt;&amp;nbsp;method should return false if the constraint has been violated.&lt;/p&gt;
&lt;p&gt;If you need to initialize the class validator you can implement the interface &lt;i&gt;IInitializableValidator&lt;/i&gt;&lt;i&gt;&amp;lt;A&amp;gt;&amp;nbsp;&lt;/i&gt;and the class should look like this:&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; PhoneValidator : IInitializableValidator&amp;lt;PhoneAttribute&amp;gt;&lt;br /&gt;{&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; Regex regex;&lt;br /&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; IsValid(&lt;span style="color:#0000ff;"&gt;object&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;)&lt;br /&gt;    {&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;if&lt;/span&gt; (&lt;span style="color:#0000ff;"&gt;value&lt;/span&gt; == &lt;span style="color:#0000ff;"&gt;null&lt;/span&gt;) &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;;&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; regex.IsMatch(&lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;.ToString());&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Initialize(PhoneAttribute parameters)&lt;br /&gt;    {&lt;br /&gt;        regex = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Regex(&lt;span style="color:#006080;"&gt;@&amp;quot;^[2-9]\d{2}-\d{3}-\d{4}$&amp;quot;&lt;/span&gt;);&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;As you can see, implementing the &lt;i&gt;IInitializableValidator&amp;lt;A&amp;gt;&lt;/i&gt;&amp;nbsp;interface we have to implement the method &lt;i&gt;Initialize(...)&lt;/i&gt; that initializes the validator, and it&amp;#39;s executed first of all.&lt;/p&gt;
&lt;p&gt;The &lt;i&gt;IsValid()&lt;/i&gt;&amp;nbsp;method should return &lt;i&gt;false&lt;/i&gt;&amp;nbsp;if the constraint has been violated. For more examples, refer to the built-in validator implementations.&lt;/p&gt;
&lt;p&gt;We now have to implement the validator (ie. the rule checking implementation). A validation implementation can check the value of the a property (by implementing PropertyConstraint ) and/or can modify the NHibernate mapping metadata to express the constraint at the database level (by implementing PersistentClassConstraint)&lt;/p&gt;
&lt;p&gt;We have only seen property level validation, but you can write a class level validation attribute. Instead of receiving the return instance of a property, the bean itself will be passed to the validator. To activate the validation checking, just put the validation attribute to the class itself instead of in a property. A small sample can be found in the unit test suite.&lt;/p&gt;
&lt;p&gt;If your constraint can be applied multiple times (with different parameters) on the same property or type, you can use set the &lt;i&gt;AllowMultiple&lt;/i&gt;&amp;nbsp;parameter to &lt;i&gt;true&lt;/i&gt;&amp;nbsp;on the &lt;i&gt;AttributeUsage&lt;/i&gt;&amp;nbsp;attribute. An example of this it&amp;#39;s the built-in validator Pattern, have a look:&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;[Serializable]&lt;br /&gt;[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = &lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;)]&lt;br /&gt;[ValidatorClass(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt; (PatternValidator))]&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; PatternAttribute : Attribute, IRuleArgs&lt;br /&gt;{&lt;br /&gt;    &amp;hellip;&lt;br /&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;pre class="programlisting"&gt;&amp;nbsp;&lt;/pre&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 class="title" style="clear:both;"&gt;Configuring your domain model with .Net Attributes&lt;/h2&gt;
&lt;p&gt;Since you are already familiar with attributes now, the syntax should be very familiar&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; Address&lt;br /&gt;{ &lt;br /&gt;    &lt;span style="color:#008000;"&gt;// a static non null field  &lt;/span&gt;&lt;br /&gt;    [NotNull]&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;static&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; blacklistedZipCode;&lt;br /&gt;    &lt;br /&gt;    &lt;span style="color:#008000;"&gt;// field a not null string of 20 characters maximum&lt;/span&gt;&lt;br /&gt;    [Length(Max = 20), NotNull]    &lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; country;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;long&lt;/span&gt; id;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; internalValid = &lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; line1;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; line2;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; state;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; zip;&lt;br /&gt;    &lt;br /&gt;    &lt;span style="color:#008000;"&gt;// a numeric between 1 and 2000&lt;/span&gt;&lt;br /&gt;    [Min(1), Range(Max = 2000)]&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;long&lt;/span&gt; Id&lt;br /&gt;    {&lt;br /&gt;        get { &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; id; }&lt;br /&gt;        set { id = &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;; }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;span style="color:#008000;"&gt;// respond to the constraint in the field: country&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Country&lt;br /&gt;    {&lt;br /&gt;        get { &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; country; }&lt;br /&gt;        set { country = &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;; }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;span style="color:#008000;"&gt;// a non null string&lt;/span&gt;&lt;br /&gt;    [NotNull]&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Line1&lt;br /&gt;    {&lt;br /&gt;        get { &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; line1; }&lt;br /&gt;        set { line1 = &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;; }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;span style="color:#008000;"&gt;// a not null string of 3 characters maximum&lt;/span&gt;&lt;br /&gt;    [Length(Max = 3), NotNull]&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; State&lt;br /&gt;    {&lt;br /&gt;        get { &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; state; }&lt;br /&gt;        set { state = &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;; }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;span style="color:#008000;"&gt;// a not null numeric string of 5 characters maximum&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#008000;"&gt;// if the string is longer, the message will&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#008000;"&gt;// be searched in the resource bundle at key &amp;#39;long&amp;#39;&lt;/span&gt;&lt;br /&gt;    [Length(Max = 5, Message = &lt;span style="color:#006080;"&gt;&amp;quot;{long}&amp;quot;&lt;/span&gt;)]&lt;br /&gt;    [Pattern(Regex = &lt;span style="color:#006080;"&gt;&amp;quot;[0-9]+&amp;quot;&lt;/span&gt;)]&lt;br /&gt;    [NotNull]&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Zip&lt;br /&gt;    {&lt;br /&gt;&lt;br /&gt;        get { &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; zip; }&lt;br /&gt;        set { zip = &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;; }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;span style="color:#008000;"&gt;// no constraint&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Line2&lt;br /&gt;    {&lt;br /&gt;        get { &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; line2; }&lt;br /&gt;        set { line2 = &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;; }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;span style="color:#008000;"&gt;// should always be true&lt;/span&gt;&lt;br /&gt;    [AssertTrue]&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; InternalValid&lt;br /&gt;    {&lt;br /&gt;        get { &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; internalValid; }&lt;br /&gt;        set { internalValid = &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;; }&lt;br /&gt;    }&lt;br /&gt;} &lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;While the example only shows public property validation, you can also annotate fields of any kind of visibility. In the example &lt;b&gt;MyBeanConstraint&lt;/b&gt; is a custom Class Validator, we will talk about this later.&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;[MyBeanConstraint(Max=45)]&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; Dog &lt;br /&gt;{&lt;br /&gt;    [AssertTrue] &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; IsMale;&lt;br /&gt;    [NotNull] &lt;span style="color:#0000ff;"&gt;protected&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Name { get {...} set{...} }&lt;br /&gt;    &amp;hellip;&lt;br /&gt;}     &lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;You can also annotate interfaces. NHibernate Validator will check all superclasses and interfaces extended or implemented by a given type to read the appropriate validator attributes.&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;interface&lt;/span&gt; Named &lt;br /&gt;{&lt;br /&gt;    [NotNull] &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Name {get; set;}&lt;br /&gt;    ...&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; Dog : Named &lt;br /&gt;{&lt;br /&gt;    [AssertTrue] &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; isMale;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Name { get {...} set{...} }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;The name property will be checked for nullity when the Dog type is validated.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 class="title" style="clear:both;"&gt;Configuring your domain model with XML&lt;/h2&gt;
&lt;p&gt;You also can configurate your domain model with constraints using xml files: the nhv.xml files. We prefer to embed these Xml files into the assemblies, and NHibernate Validator is prepared to inspect them and retrieve the resources with constrains, like NHibernate does with hbm.xml files.&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#800000;"&gt;xml&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;version&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;1.0&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;encoding&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;utf-8&amp;quot;&lt;/span&gt; ?&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;nhv-mapping&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;urn:nhibernate-validator-1.0&amp;quot;&lt;/span&gt;&lt;br /&gt;                        &lt;span style="color:#ff0000;"&gt;namespace&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;NHibernate.Validator.Tests.Base&amp;quot;&lt;/span&gt;&lt;br /&gt;                        &lt;span style="color:#ff0000;"&gt;assembly&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;NHibernate.Validator.Tests&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;class&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Address&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;blacklistedZipCode&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;                  &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;not-null&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;country&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;           &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;length&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;max&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;20&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;           &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;not-null&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;    &lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;floor&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;    &lt;br /&gt;          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;range&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;min&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;-2&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;max&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;50&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;message&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;{floor.out.of.range} (escaping #{el})&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Id&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;    &lt;br /&gt;          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;min&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;value&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;1&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;range&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;max&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;2000&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;max&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;value&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;2500&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Line1&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;    &lt;br /&gt;          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;not-null&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;State&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;    &lt;br /&gt;          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;not-null&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;length&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;max&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;3&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Zip&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;length&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;max&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;5&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;message&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;{long}&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;pattern&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;regex&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;[0-9]+&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;not-null&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;InternalValid&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;    &lt;br /&gt;          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;asserttrue&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;nhv-mapping&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;      &lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;span class="bold"&gt;&lt;b&gt;We strongly recommend&lt;/b&gt;&lt;/span&gt; to validate every nhv.xml file of your application with the respective XSD file. Every &lt;b&gt;nhv.xml&lt;/b&gt; file should be validated with the &lt;b&gt;nhv-mapping.xsd&lt;/b&gt;&amp;nbsp;file included within the NHibernate Validator distribution whether in source or binaries. Indeed, if you are using an IDE like Visual Studio it offers IntelliSense support in the &lt;b&gt;nhv.xml&lt;/b&gt; file if you add the schema to Visual Studio&amp;#39;s xml schemas folder.&lt;/p&gt;
&lt;p&gt;&lt;span class="bold"&gt;&lt;b&gt;Xml beauties:&lt;/b&gt;&lt;/span&gt; The nicest part of Xml for validation is that you can put xml validators in a different dll so you don&amp;#39;t need to recompile your domain when validation rules change. Only the dll with the Xml files..&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class="chapter"&gt;
&lt;h1&gt;Configuration&lt;/h1&gt;
&lt;p&gt;NHibernate Validator has global parameters that you can configure at the startup. This configuration can be done in differents ways: programmaticaly or using xml files. If you are involved with NHibernate this could be very familiar to you.&lt;/p&gt;
&lt;p&gt;Configuration properties:&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Table Configuration properties&lt;/b&gt;&lt;/p&gt;
&lt;div class="table"&gt;
&amp;lt;tbody&amp;gt;
&amp;lt;/tbody&amp;gt;
&lt;table bgcolor="#ffffc8" cellpadding="2" cellspacing="1"&gt;
&amp;lt;tbody&amp;gt;
&amp;lt;tbody&amp;gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;th align="middle"&gt;Property name&lt;/th&gt;
&lt;th&gt;Purpose&lt;/th&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="middle"&gt;&lt;b&gt;apply_to_ddl&lt;/b&gt;&lt;/td&gt;
&lt;td&gt;Configure NHibernate to apply the constraints to the DDL.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="middle"&gt;&lt;b&gt;autoregister_listeners&lt;/b&gt;&lt;/td&gt;
&lt;td&gt;Register the Event-Listeners automatically on Inserts and Updates.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="middle"&gt;&lt;b&gt;default_validator_mode&lt;/b&gt;&lt;/td&gt;
&lt;td&gt;Define what are the constraints sources are and how to proceed. There are some built-in configurations:
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;div class="itemizedlist"&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;b&gt;UseAttribute&lt;/b&gt; (default): Use Attributes to configurate the validators on types and/or members. Xml is ignored.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;b&gt;UseXml&lt;/b&gt;: Use Xml to configurate the validators on types and/or members. Attributes are ignored.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;b&gt;OverrideAttributeWithXml&lt;/b&gt;: If the same type/member is annotated with an Attribute and has a Xml rule the Attribute annotation will be used.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;b&gt;OverrideXmlWithAttribute&lt;/b&gt;: If the same type/member is annotated with an Attribute and has a Xml rule the Attribute annotation will be used.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="middle"&gt;&lt;b&gt;assembly&lt;/b&gt;&lt;/td&gt;
&lt;td&gt;Which are the assemblies with contains the nhv.xml files embedded. You can have more than one assembly.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="middle"&gt;&lt;b&gt;shared_engine_provider&lt;/b&gt;&lt;/td&gt;
&lt;td&gt;To inject a custom Shared Engine Provider.&lt;/td&gt;
&lt;/tr&gt;
&amp;lt;/tbody&amp;gt;
&amp;lt;/tbody&amp;gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 class="title" style="clear:both;"&gt;Programmatic configuration&lt;/h2&gt;
&lt;p&gt;You can use the following code to configure NHibernate.Validator programmatically:&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NHibernate.Validator.Cfg;&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NHibernate.Validator.Engine;&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; Environment=NHibernate.Validator.Cfg.Environment;&lt;br /&gt;&amp;hellip;&lt;br /&gt;ValidatorEngine ve = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ValidatorEngine();&lt;br /&gt;&lt;br /&gt;NHVConfiguration nhvc = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; NHVConfiguration();&lt;br /&gt;nhvc.Properties[Environment.ApplyToDDL] = &lt;span style="color:#006080;"&gt;&amp;quot;false&amp;quot;&lt;/span&gt;;&lt;br /&gt;nhvc.Properties[Environment.AutoregisterListeners] = &lt;span style="color:#006080;"&gt;&amp;quot;false&amp;quot;&lt;/span&gt;;&lt;br /&gt;nhvc.Properties[Environment.ValidatorMode] = GetMode();&lt;br /&gt;nhvc.Mappings.Add(&lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; MappingConfiguration(&lt;span style="color:#006080;"&gt;&amp;quot;NHibernate.Validator.Demo.Model&amp;quot;&lt;/span&gt;,&lt;span style="color:#0000ff;"&gt;null&lt;/span&gt;));&lt;br /&gt;ve.Configure(nhvc);  &lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;The &lt;i&gt;ValidatorEngine&lt;/i&gt;&amp;nbsp;will be set with the configuration. The NHibernate Validator configuration is represented by the &lt;i&gt;NHibernate.Validator.Cfg.NHVConfiguration&lt;/i&gt;&amp;nbsp;type. Other class used to configure all the stuff is the &lt;i&gt;NHibernate.Validator.Cfg.Environment&lt;/i&gt;&amp;nbsp;type, this class contain the string with the names of the properties of the configuration of NHibernate Validator.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 class="title" style="clear:both;"&gt;Declarative configuration: using Xml files&lt;/h2&gt;
&lt;p&gt;The declarative configuration keeps configuration out of the code. This means configuring the application using a different file, in this case a XML file. But the options don&amp;#39;t end here: you can choose to configure NHibernate Validator using a simple Xml file or you can put the configuration in the well known App.Config/Web.Config. &lt;/p&gt;
&lt;div class="section"&gt;
&lt;h3 class="title"&gt;Simple xml file&lt;/h3&gt;
&lt;p&gt;This is a sample of a simple configuration in a xml file:&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#800000;"&gt;xml&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;version&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;1.0&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;encoding&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;utf-8&amp;quot;&lt;/span&gt; ?&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;nhv-configuration&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;#39;urn:nhv-configuration-1.0&amp;#39;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;#39;apply_to_ddl&amp;#39;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;true&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;#39;autoregister_listeners&amp;#39;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;true&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;#39;default_validator_mode&amp;#39;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;UseXml&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;mapping&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;assembly&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;#39;NHibernate.Validator.Demo.Model&amp;#39;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;nhv-configuration&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;We encourage to name the configuration file &lt;b&gt;nhvalidator.cfg.xml&lt;/b&gt;, because NHibernate Validator will pick up this file from the application directory in order to get the configuration parameters, however you can name this file as you wish.&lt;/p&gt;
&lt;p&gt;In order to configurate NHibernate Validator with the &lt;b&gt;nhvalidator.cfg.xml&lt;/b&gt;&amp;nbsp;you can use the following codeו:&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;ValidatorEngine engine = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ValidatorEngine();&lt;br /&gt;engine.Configure(); &lt;span style="color:#008000;"&gt;// by convention reads the nhvalidator.cfg.xml file.&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;You can choose another name for your configuration file and proceed of this manner: &lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;ValidatorEngine engine = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ValidatorEngine();&lt;br /&gt;engine.Configure(&lt;span style="color:#006080;"&gt;&amp;quot;path/to/MyNHVConfigurationFile.xml&amp;quot;&lt;/span&gt;); &lt;span style="color:#008000;"&gt;// you can use whatever path to point to the configuration file&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h3 class="title"&gt;Configuration Application file: app.config/web.config&lt;/h3&gt;
&lt;p&gt;The configuration can be declared into our configuration application .Net files: app.config or web.config. In the below example you will see how to do it.&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#800000;"&gt;xml&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;version&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;1.0&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;encoding&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;utf-8&amp;quot;&lt;/span&gt; ?&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;configuration&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;configSections&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;section&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;nhv-configuration&amp;quot;&lt;/span&gt; &lt;br /&gt;                            &lt;span style="color:#ff0000;"&gt;type&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;NHibernate.Validator.Cfg.ConfigurationSectionHandler, NHibernate.Validator&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;        ...&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;configSections&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    ...&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;nhv-configuration&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;#39;urn:nhv-configuration-1.0&amp;#39;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;shared_engine_provider&lt;/span&gt; &lt;br /&gt;         &lt;span style="color:#ff0000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;#39;NHibernate.Validator.Event.NHibernateSharedEngineProvider, NHibernate.Validator&amp;#39;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;nhv-configuration&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    ...&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;configuration&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;      &lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;</description></item><item><title>NHibernate Validator 1.0.0 Documentation </title><link>http://nhforge.org/wikis/validator/nhibernate-validator-1-0-0-documentation/revision/9.aspx</link><pubDate>Mon, 09 Feb 2009 19:53:25 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:218</guid><dc:creator>Tobin Harris</dc:creator><description>Revision 9 posted to Validator by Tobin Harris on 09/02/2009 04:53:25 p.m.&lt;br /&gt;
&lt;h2&gt;NHibernate Validator 1.0.0 &lt;span style="background: SpringGreen;"&gt;Documentation&lt;/span&gt; &lt;/h2&gt;
&lt;h1&gt;Preface&lt;/h1&gt;
&lt;div class="preface"&gt;
&lt;p&gt;Attributes are a very convenient and elegant way to specify invariant constraints for a domain model. You can, for example, express that a property should never be null, that the account balance should be strictly positive, etc. These domain model constraints are declared in the class itself by annotating its properties. A validator can then read them and check for constraint violations. The validation mechanism can be executed in different layers in your application without having to duplicate any of these rules (presentation layer, data access layer). Following the DRY principle, NHibernate Validator has been designed for that purpose. NHibernate Validator also lets you declare the same rules in XML files. Both approaches (attributes and XML) give us interesting possibilities for configuring our domain rules. &lt;/p&gt;
&lt;p&gt;NHibernate Validator works on two levels. First, it is able to check in-memory instances of a class for constraint violations. Second, it can apply the constraints to the NHibernate metamodel and incorporate them into the generated database schema.&lt;/p&gt;
&lt;p&gt;Each constraint attribute is associated to a validator implementation responsible for checking the constraint on the entity instance. A validator can also (optionally) apply the constraint to the NHibernate metamodel, allowing NHibernate to generate DDL that expresses the constraint. With the appropriate event listener, you can execute the checking operation on inserts and updates done by NHibernate. NHibernate Validator is not limited to only be used with NHibernate. You can easily use it anywhere in your application as well, indeed, you can use it at UI validation with Winforms or Asp.Net technologies.&lt;/p&gt;
&lt;p&gt;When checking instances at runtime, NHibernate Validator returns information about constraint violations in an array of &lt;i&gt;InvalidValue&lt;/i&gt;s. Among other information, the &lt;i&gt;InvalidValue&lt;/i&gt; contains an error description message that can embed the parameter values bundle with the annotation (eg. length limit), and message strings that may be externalized to a ResourceManager.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="chapter"&gt;
&lt;h1&gt;About the project&lt;/h1&gt;
&lt;p&gt;NHibernate Validator is a powerful and extensible framework to validate objects using the .Net Platform. Born as a port of &lt;span class="italic"&gt;Hibernate Validator 3.0.0&lt;/span&gt; project at december of 2007. Later on, many improvements and changes were made to the Core and API, and now we can say that NHibernate Validator is more than just a port. NHibernate Validator is entirely written in C#. The first release of NHibernate Validator was entirely developed by &lt;span class="italic"&gt;Dario Quintana&lt;/span&gt;, &lt;span class="italic"&gt;Gustavo Ringel&lt;/span&gt; and &lt;span class="italic"&gt;Fabio Maulo&lt;/span&gt;. We appreciate the help of &lt;span class="italic"&gt;Davy Brion&lt;/span&gt; in the revision of this document.&lt;/p&gt;
&lt;p&gt;NHibernate Validator is part of the &lt;a href="http://sourceforge.net/projects/nhcontrib/" target="_top"&gt;NHibernate Contrib&lt;/a&gt; project and you can download it&amp;nbsp;&lt;a href="http://sourceforge.net/project/showfiles.php?group_id=216446&amp;amp;package_id=275108&amp;amp;release_id=621966"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;You can ask questions about the development and utilization of NHibernate Validator on the following mailing lists:&lt;/p&gt;
&lt;p&gt;The development mailing list can be found &lt;a href="http://groups.google.com/group/nhcdevs" target="_top"&gt;here&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;The user forum can be found &lt;a href="http://groups.google.com/group/nhusers" target="_top"&gt;here&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;Bug and issues report should be sent to the development mailing list.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="chapter"&gt;
&lt;h1&gt;Using the Validator framework&lt;/h1&gt;
&lt;p&gt;NHibernate Validator is intended to be used to implement multi-layered data validation, where constraints are expressed in a single place and checked in various different layers of the application.&lt;/p&gt;
&lt;p&gt;This chapter will cover NHibernate Validator usage for different layers&lt;/p&gt;
&lt;div class="section"&gt;
&lt;h2 class="title" style="clear:both;"&gt;Database schema-level validation&lt;/h2&gt;
&lt;p&gt;Out of the box, NHibernate Validator will translate the constraints you have defined for your entities into mapping metadata. For example, if a property of your entity has the attribute [NotNull], its columns will be declared as &lt;i&gt;not null&lt;/i&gt; in the DDL schema generated by NHibernate.&lt;/p&gt;
&lt;p&gt;Using hbm2ddl, domain model constraints will be expressed into the database schema.&lt;/p&gt;
&lt;p&gt;If, for some reason, the feature needs to be disabled, set &lt;b&gt;&lt;i&gt;apply_to_ddl&lt;/i&gt;&lt;/b&gt; to false.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 class="title" style="clear:both;"&gt;ORM integration&lt;/h2&gt;
&lt;p&gt;NHibernate Validator integrates with NHibernate in a native way, and knows how to treat every persistent entity.&lt;/p&gt;
&lt;div class="section"&gt;
&lt;h3 class="title"&gt;NHibernate event-based validation&lt;/h3&gt;
&lt;p&gt;NHibernate Validator has two built-in NHibernate event listeners. Whenever a PreInsertEvent or PreUpdateEvent occurs, the listeners will verify all constraints of the entity instance and throw an exception if any of them are violated. Basically, objects will be checked before any insert and before any update triggered by NHibernate. This includes cascading changes! This is the most convenient and easiest way to activate the validation process. If a constraint is violated, the event will raise a runtime InvalidStateException which contains an array of &lt;i&gt;InvalidValue&lt;/i&gt;s describing each failure.&lt;/p&gt;
&lt;p&gt;If, for some reason, you want to disable this integration, set &lt;b&gt;autoregister_listeners&lt;/b&gt;&amp;nbsp;to false&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;span style="text-decoration:underline;"&gt;Note&lt;/span&gt;: If the entities are not annotated with validation attributes, there is no runtime performance cost.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;In case you wish to set manually the event listeners for NHibernate Core, use the following configuration in &lt;b&gt;hibernate.cfg.xml&lt;/b&gt;:&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    ...&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;event&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;type&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;pre-update&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;listener&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;NHibernate.Validator.Event.ValidatePreUpdateEventListener, NHibernate.Validator&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;event&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;event&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;type&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;pre-insert&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;listener&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;NHibernate.Validator.Event.ValidatePreInsertEventListener, NHibernate.Validator&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;event&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 class="title" style="clear:both;"&gt;Application-level validation&lt;/h2&gt;
&lt;p&gt;In order to interact with NHibernate Validator in your application you must use the main class: &lt;i&gt;ValidatorEngine&lt;/i&gt;. &lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;ValidatorEngine validator = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ValidatorEngine();&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; isValid = validator.IsValid(customer);&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;These two lines are enough to validate an object with its validation rules, which can be configured using Attributes or XML. The first line is the instantiation of the validator engine of NHibernate Validator. The second line is the validation of an object, and the result of the &lt;i&gt;IsValid(...)&lt;/i&gt;method indicates if the object is valid or not. In order to get the invalid values of an object, you can do:&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;ValidatorEngine validator = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ValidatorEngine();&lt;br /&gt;InvalidValue[] validationMessages = validator.GetInvalidValues(customer);&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Now instead of only knowing if the object is valid or not, we get an array of InvalidValues. If this array length is greater than zero then there are validation errors and you can traverse the InvalidValue array to get verbose information about the problems that cause the object to be invalid. &lt;/p&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 class="title" style="clear:both;"&gt;Presentation layer validation&lt;/h2&gt;
&lt;p&gt;[&lt;b&gt;&lt;span style="color:#008000;"&gt;TODO&lt;/span&gt;&lt;/b&gt;]&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 class="title" style="clear:both;"&gt;Validation informations&lt;/h2&gt;
&lt;p&gt;NHibernate Validator provides an array of InvalidValue. Each &lt;i&gt;InvalidValue&lt;/i&gt;&amp;nbsp;has several methods describing the reasons that caused the entity to be invalid.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;BeanClass&lt;/b&gt; retrieves the failing bean type&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Bean&amp;nbsp;&lt;/b&gt;retrieves the failing instance (if any ie not when using &lt;i&gt;GetPotentialInvalidValues()&lt;/i&gt;)&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Value&lt;/b&gt;&amp;nbsp;retrieves the failing value&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Message&lt;/b&gt;&amp;nbsp;retrieves the proper internationalized error message&lt;/p&gt;
&lt;p&gt;&lt;b&gt;RootBean&lt;/b&gt;&amp;nbsp;retrieves the root bean instance generating the issue (useful in conjunction with &lt;i&gt;[Valid]&lt;/i&gt;), is null if &lt;i&gt;GetPotentialInvalidValues(...)&lt;/i&gt; is used.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;PropertyPath&lt;/b&gt;&amp;nbsp;retrieves the dotted path of the failing property starting from the root bean&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class="chapter"&gt;
&lt;h1&gt;Defining constraints&lt;/h1&gt;
&lt;div class="section"&gt;
&lt;h2 class="title" style="clear:both;"&gt;What is a constraint?&lt;/h2&gt;
&lt;p&gt;A constraint is a rule that a given element (field, property or type) has to comply to. The rule semantic is expressed by an Attribute or configured in Xml files. A constraint may have some properties to configure the associated validator, for example a Min constraint will have a value property setting the minimum acceptable value for the element to be valid. &lt;/p&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 class="title" style="clear:both;"&gt;Built in constraints&lt;/h2&gt;
&lt;p&gt;Hibernate Validator comes with some built-in constraints, which cover most of the basic data checks. As we&amp;#39;ll see later, you&amp;#39;re not limited to them, you can literally in a minute write your own constraints.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Table Built-in constraints&lt;/b&gt;&lt;/p&gt;
&amp;lt;tbody&amp;gt;
&amp;lt;/tbody&amp;gt;
&lt;table bgcolor="#ffffc8" cellpadding="2" cellspacing="1"&gt;
&amp;lt;tbody&amp;gt;
&amp;lt;tbody&amp;gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;b&gt;Constraint&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;b&gt;Apply on&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;b&gt;Runtime checking&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;b&gt;NHibernate Metadata impact&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;b&gt;Length(Min=, Max=)&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property (String)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;checks if the string length is between the min-max range&lt;/span&gt;&lt;/td&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;Column length will be set to max&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;b&gt;Max(Value=)&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property (numeric or string representation of a numeric)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;checks if the value is less than or equals to max&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;Add a check constraint on the column&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;b&gt;Min(Value=)&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property (numeric or string representation of a numeric)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;checks if the value is more than or equals to min&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;Add a check constraint on the column&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;b&gt;NotNull&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;checks if the value is not null&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;Column(s) are not null&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;b&gt;NotEmpty&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;checks if the string is not empty.&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;b&gt;NotEmptyOrNull&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;Checks if the string is not null or not empty.&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;b&gt;Past&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property (DateTime)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;check if the date is in the past&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;b&gt;Future&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property (DateTime)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;check if the date is in the future&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;b&gt;Pattern(Regex=&amp;quot;regexp&amp;quot;, Flag=)&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property (string)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;check if the property matches the regular expression given a match flag. The &lt;span style="font-family:&amp;#39;Trebuchet MS&amp;#39;;"&gt;&amp;lt;tt&amp;gt;Flag&amp;lt;/tt&amp;gt; parameter is a &lt;i&gt;System.Text.RegularExpressions.RegexOptions&lt;/i&gt;&amp;nbsp;enum. You can also declare multiple Pattern attributes on the same member.&lt;/span&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;b&gt;Range(Min=, Max=)&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property (numeric or string representation of a numeric)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;check if the value is between Min and Max (included)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;b&gt;Size(Min=, Max=)&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property (array, collection, map)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;check if the element size is between Min and Max (included)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;b&gt;AssertFalse&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;check that the method evaluates to false (useful for constraints expressed in code rather than attributes)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;b&gt;AssertTrue&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;check that the method evaluates to true (useful for constraints expressed in code rather than attributes)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;b&gt;Valid&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property (object)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;perform validation recursively on the associated object. If the object is a Collection or an array, the elements are validated recursively. If the object is a Map, the value elements are validated recursively.&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;b&gt;Email&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property (String)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;check whether the string conforms to the email address specification&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;b&gt;Digits&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property (numeric or string representation of a numeric)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;checks whether the property is a number having up to&amp;nbsp;&lt;span style="font-family:&amp;#39;Trebuchet MS&amp;#39;;"&gt;&lt;i&gt;integerDigits&lt;/i&gt; integer digits and &lt;i&gt;fractionalDigits&lt;/i&gt;&amp;nbsp;fractonal digits&lt;/span&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;define column precision and scale&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&amp;lt;/tbody&amp;gt;
&amp;lt;/tbody&amp;gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 class="title" style="clear:both;"&gt;Error messages&lt;/h2&gt;
&lt;p&gt;NHibernate Validator comes with a default set of error messages translated in several languages (if yours is not present, please send us a patch). You can add your own additional set of messages while writing your validator Attributes or at XML. If NHibernate Validator cannot resolve a key from your ResourceManager nor from ValidatorMessage, it falls back to the default built-in values.&lt;/p&gt;
&lt;p&gt;Alternatively you can provide a ResourceManager while checking programmatically the validation rules on a bean or if you want a completly different interpolation mechanism, you can provide an implementation of NHibernate.Validator.Engine.IMessageInterpolator.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 class="title" style="clear:both;"&gt;Writing your own constraints&lt;/h2&gt;
&lt;p&gt;Extending the set of built-in constraints is extremely easy. Any constraint consists of two pieces: the constraint &lt;span class="emphasis"&gt;&lt;i&gt;descriptor&lt;/i&gt;&lt;/span&gt; (the attribute) and the constraint &lt;span class="emphasis"&gt;&lt;i&gt;validator&lt;/i&gt;&lt;/span&gt; (the implementation class). Here is a simple user-defined descriptor:&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]&lt;br /&gt;[ValidatorClass(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt;(PhoneValidator))]&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; PhoneAttribute : Attribute, IRuleArgs&lt;br /&gt;{&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; message = &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt;.Empty;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Message&lt;br /&gt;    {&lt;br /&gt;        get { &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; message; }&lt;br /&gt;        set { message = &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;; }&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;To link a descriptor to its validator implementation, we use the &lt;i&gt;[ValidatorClass]&lt;/i&gt;&amp;nbsp;meta-attribute. Here you can see an implementation of a constraint validator:&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; PhoneValidator : IValidator&lt;br /&gt;{&lt;br /&gt;     &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; IsValid(&lt;span style="color:#0000ff;"&gt;object&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;)&lt;br /&gt;     {&lt;br /&gt;         Regex regex = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Regex(&lt;span style="color:#006080;"&gt;@&amp;quot;^[2-9]\d{2}-\d{3}-\d{4}$&amp;quot;&lt;/span&gt;);&lt;br /&gt;         &lt;span style="color:#0000ff;"&gt;if&lt;/span&gt; (&lt;span style="color:#0000ff;"&gt;value&lt;/span&gt; == &lt;span style="color:#0000ff;"&gt;null&lt;/span&gt;) &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;;&lt;br /&gt;         &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; regex.IsMatch(&lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;.ToString());&lt;br /&gt;     }&lt;br /&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;This PhoneValidator class is simple. The method &lt;i&gt;IsValid()&lt;/i&gt;&amp;nbsp;receives a string that should be validated with a Regex expression. The &lt;i&gt;IsValid()&lt;/i&gt;&amp;nbsp;method should return false if the constraint has been violated.&lt;/p&gt;
&lt;p&gt;If you need to initialize the class validator you can implement the interface &lt;i&gt;IInitializableValidator&lt;/i&gt;&lt;i&gt;&amp;lt;A&amp;gt;&amp;nbsp;&lt;/i&gt;and the class should look like this:&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; PhoneValidator : IInitializableValidator&amp;lt;PhoneAttribute&amp;gt;&lt;br /&gt;{&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; Regex regex;&lt;br /&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; IsValid(&lt;span style="color:#0000ff;"&gt;object&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;)&lt;br /&gt;    {&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;if&lt;/span&gt; (&lt;span style="color:#0000ff;"&gt;value&lt;/span&gt; == &lt;span style="color:#0000ff;"&gt;null&lt;/span&gt;) &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;;&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; regex.IsMatch(&lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;.ToString());&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Initialize(PhoneAttribute parameters)&lt;br /&gt;    {&lt;br /&gt;        regex = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Regex(&lt;span style="color:#006080;"&gt;@&amp;quot;^[2-9]\d{2}-\d{3}-\d{4}$&amp;quot;&lt;/span&gt;);&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;As you can see, implementing the &lt;i&gt;IInitializableValidator&amp;lt;A&amp;gt;&lt;/i&gt;&amp;nbsp;interface we have to implement the method &lt;i&gt;Initialize(...)&lt;/i&gt; that initializes the validator, and it&amp;#39;s executed first of all.&lt;/p&gt;
&lt;p&gt;The &lt;i&gt;IsValid()&lt;/i&gt;&amp;nbsp;method should return &lt;i&gt;false&lt;/i&gt;&amp;nbsp;if the constraint has been violated. For more examples, refer to the built-in validator implementations.&lt;/p&gt;
&lt;p&gt;We now have to implement the validator (ie. the rule checking implementation). A validation implementation can check the value of the a property (by implementing PropertyConstraint ) and/or can modify the NHibernate mapping metadata to express the constraint at the database level (by implementing PersistentClassConstraint)&lt;/p&gt;
&lt;p&gt;We have only seen property level validation, but you can write a class level validation attribute. Instead of receiving the return instance of a property, the bean itself will be passed to the validator. To activate the validation checking, just put the validation attribute to the class itself instead of in a property. A small sample can be found in the unit test suite.&lt;/p&gt;
&lt;p&gt;If your constraint can be applied multiple times (with different parameters) on the same property or type, you can use set the &lt;i&gt;AllowMultiple&lt;/i&gt;&amp;nbsp;parameter to &lt;i&gt;true&lt;/i&gt;&amp;nbsp;on the &lt;i&gt;AttributeUsage&lt;/i&gt;&amp;nbsp;attribute. An example of this it&amp;#39;s the built-in validator Pattern, have a look:&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;[Serializable]&lt;br /&gt;[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = &lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;)]&lt;br /&gt;[ValidatorClass(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt; (PatternValidator))]&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; PatternAttribute : Attribute, IRuleArgs&lt;br /&gt;{&lt;br /&gt;    &amp;hellip;&lt;br /&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;pre class="programlisting"&gt;&amp;nbsp;&lt;/pre&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 class="title" style="clear:both;"&gt;Configuring your domain model with .Net Attributes&lt;/h2&gt;
&lt;p&gt;Since you are already familiar with attributes now, the syntax should be very familiar&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; Address&lt;br /&gt;{ &lt;br /&gt;    &lt;span style="color:#008000;"&gt;// a static non null field  &lt;/span&gt;&lt;br /&gt;    [NotNull]&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;static&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; blacklistedZipCode;&lt;br /&gt;    &lt;br /&gt;    &lt;span style="color:#008000;"&gt;// field a not null string of 20 characters maximum&lt;/span&gt;&lt;br /&gt;    [Length(Max = 20), NotNull]    &lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; country;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;long&lt;/span&gt; id;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; internalValid = &lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; line1;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; line2;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; state;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; zip;&lt;br /&gt;    &lt;br /&gt;    &lt;span style="color:#008000;"&gt;// a numeric between 1 and 2000&lt;/span&gt;&lt;br /&gt;    [Min(1), Range(Max = 2000)]&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;long&lt;/span&gt; Id&lt;br /&gt;    {&lt;br /&gt;        get { &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; id; }&lt;br /&gt;        set { id = &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;; }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;span style="color:#008000;"&gt;// respond to the constraint in the field: country&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Country&lt;br /&gt;    {&lt;br /&gt;        get { &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; country; }&lt;br /&gt;        set { country = &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;; }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;span style="color:#008000;"&gt;// a non null string&lt;/span&gt;&lt;br /&gt;    [NotNull]&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Line1&lt;br /&gt;    {&lt;br /&gt;        get { &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; line1; }&lt;br /&gt;        set { line1 = &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;; }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;span style="color:#008000;"&gt;// a not null string of 3 characters maximum&lt;/span&gt;&lt;br /&gt;    [Length(Max = 3), NotNull]&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; State&lt;br /&gt;    {&lt;br /&gt;        get { &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; state; }&lt;br /&gt;        set { state = &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;; }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;span style="color:#008000;"&gt;// a not null numeric string of 5 characters maximum&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#008000;"&gt;// if the string is longer, the message will&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#008000;"&gt;// be searched in the resource bundle at key &amp;#39;long&amp;#39;&lt;/span&gt;&lt;br /&gt;    [Length(Max = 5, Message = &lt;span style="color:#006080;"&gt;&amp;quot;{long}&amp;quot;&lt;/span&gt;)]&lt;br /&gt;    [Pattern(Regex = &lt;span style="color:#006080;"&gt;&amp;quot;[0-9]+&amp;quot;&lt;/span&gt;)]&lt;br /&gt;    [NotNull]&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Zip&lt;br /&gt;    {&lt;br /&gt;&lt;br /&gt;        get { &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; zip; }&lt;br /&gt;        set { zip = &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;; }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;span style="color:#008000;"&gt;// no constraint&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Line2&lt;br /&gt;    {&lt;br /&gt;        get { &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; line2; }&lt;br /&gt;        set { line2 = &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;; }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;span style="color:#008000;"&gt;// should always be true&lt;/span&gt;&lt;br /&gt;    [AssertTrue]&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; InternalValid&lt;br /&gt;    {&lt;br /&gt;        get { &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; internalValid; }&lt;br /&gt;        set { internalValid = &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;; }&lt;br /&gt;    }&lt;br /&gt;} &lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;While the example only shows public property validation, you can also annotate fields of any kind of visibility. In the example &lt;b&gt;MyBeanConstraint&lt;/b&gt; is a custom Class Validator, we will talk about this later.&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;[MyBeanConstraint(Max=45)]&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; Dog &lt;br /&gt;{&lt;br /&gt;    [AssertTrue] &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; IsMale;&lt;br /&gt;    [NotNull] &lt;span style="color:#0000ff;"&gt;protected&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Name { get {...} set{...} }&lt;br /&gt;    &amp;hellip;&lt;br /&gt;}     &lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;You can also annotate interfaces. NHibernate Validator will check all superclasses and interfaces extended or implemented by a given type to read the appropriate validator attributes.&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;interface&lt;/span&gt; Named &lt;br /&gt;{&lt;br /&gt;    [NotNull] &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Name {get; set;}&lt;br /&gt;    ...&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; Dog : Named &lt;br /&gt;{&lt;br /&gt;    [AssertTrue] &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; isMale;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Name { get {...} set{...} }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;The name property will be checked for nullity when the Dog type is validated.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 class="title" style="clear:both;"&gt;Configuring your domain model with XML&lt;/h2&gt;
&lt;p&gt;You also can configurate your domain model with constraints using xml files: the nhv.xml files. We prefer to embed these Xml files into the assemblies, and NHibernate Validator is prepared to inspect them and retrieve the resources with constrains, like NHibernate does with hbm.xml files.&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#800000;"&gt;xml&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;version&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;1.0&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;encoding&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;utf-8&amp;quot;&lt;/span&gt; ?&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;nhv-mapping&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;urn:nhibernate-validator-1.0&amp;quot;&lt;/span&gt;&lt;br /&gt;                        &lt;span style="color:#ff0000;"&gt;namespace&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;NHibernate.Validator.Tests.Base&amp;quot;&lt;/span&gt;&lt;br /&gt;                        &lt;span style="color:#ff0000;"&gt;assembly&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;NHibernate.Validator.Tests&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;class&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Address&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;blacklistedZipCode&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;                  &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;not-null&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;country&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;           &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;length&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;max&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;20&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;           &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;not-null&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;    &lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;floor&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;    &lt;br /&gt;          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;range&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;min&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;-2&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;max&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;50&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;message&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;{floor.out.of.range} (escaping #{el})&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Id&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;    &lt;br /&gt;          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;min&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;value&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;1&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;range&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;max&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;2000&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;max&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;value&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;2500&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Line1&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;    &lt;br /&gt;          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;not-null&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;State&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;    &lt;br /&gt;          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;not-null&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;length&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;max&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;3&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Zip&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;length&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;max&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;5&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;message&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;{long}&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;pattern&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;regex&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;[0-9]+&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;not-null&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;InternalValid&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;    &lt;br /&gt;          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;asserttrue&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;nhv-mapping&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;      &lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;span class="bold"&gt;&lt;b&gt;We strongly recommend&lt;/b&gt;&lt;/span&gt; to validate every nhv.xml file of your application with the respective XSD file. Every &lt;b&gt;nhv.xml&lt;/b&gt; file should be validated with the &lt;b&gt;nhv-mapping.xsd&lt;/b&gt;&amp;nbsp;file included within the NHibernate Validator distribution whether in source or binaries. Indeed, if you are using an IDE like Visual Studio it offers IntelliSense support in the &lt;b&gt;nhv.xml&lt;/b&gt; file if you add the schema to Visual Studio&amp;#39;s xml schemas folder.&lt;/p&gt;
&lt;p&gt;&lt;span class="bold"&gt;&lt;b&gt;Xml beauties:&lt;/b&gt;&lt;/span&gt; The nicest part of Xml for validation is that you can put xml validators in a different dll so you don&amp;#39;t need to recompile your domain when validation rules change. Only the dll with the Xml files..&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class="chapter"&gt;
&lt;h1&gt;Configuration&lt;/h1&gt;
&lt;p&gt;NHibernate Validator has global parameters that you can configure at the startup. This configuration can be done in differents ways: programmaticaly or using xml files. If you are involved with NHibernate this could be very familiar to you.&lt;/p&gt;
&lt;p&gt;Configuration properties:&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Table Configuration properties&lt;/b&gt;&lt;/p&gt;
&lt;div class="table"&gt;
&amp;lt;tbody&amp;gt;
&amp;lt;/tbody&amp;gt;
&lt;table bgcolor="#ffffc8" cellpadding="2" cellspacing="1"&gt;
&amp;lt;tbody&amp;gt;
&amp;lt;tbody&amp;gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;th align="middle"&gt;Property name&lt;/th&gt;
&lt;th&gt;Purpose&lt;/th&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="middle"&gt;&lt;b&gt;apply_to_ddl&lt;/b&gt;&lt;/td&gt;
&lt;td&gt;Configure NHibernate to apply the constraints to the DDL.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="middle"&gt;&lt;b&gt;autoregister_listeners&lt;/b&gt;&lt;/td&gt;
&lt;td&gt;Register the Event-Listeners automatically on Inserts and Updates.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="middle"&gt;&lt;b&gt;default_validator_mode&lt;/b&gt;&lt;/td&gt;
&lt;td&gt;Define what are the constraints sources are and how to proceed. There are some built-in configurations:
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;div class="itemizedlist"&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;b&gt;UseAttribute&lt;/b&gt; (default): Use Attributes to configurate the validators on types and/or members. Xml is ignored.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;b&gt;UseXml&lt;/b&gt;: Use Xml to configurate the validators on types and/or members. Attributes are ignored.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;b&gt;OverrideAttributeWithXml&lt;/b&gt;: If the same type/member is annotated with an Attribute and has a Xml rule the Attribute annotation will be used.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;b&gt;OverrideXmlWithAttribute&lt;/b&gt;: If the same type/member is annotated with an Attribute and has a Xml rule the Attribute annotation will be used.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="middle"&gt;&lt;b&gt;assembly&lt;/b&gt;&lt;/td&gt;
&lt;td&gt;Which are the assemblies with contains the nhv.xml files embedded. You can have more than one assembly.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="middle"&gt;&lt;b&gt;shared_engine_provider&lt;/b&gt;&lt;/td&gt;
&lt;td&gt;To inject a custom Shared Engine Provider.&lt;/td&gt;
&lt;/tr&gt;
&amp;lt;/tbody&amp;gt;
&amp;lt;/tbody&amp;gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 class="title" style="clear:both;"&gt;Programmatic configuration&lt;/h2&gt;
&lt;p&gt;You can use the following code to configure NHibernate.Validator programmatically:&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NHibernate.Validator.Cfg;&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NHibernate.Validator.Engine;&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; Environment=NHibernate.Validator.Cfg.Environment;&lt;br /&gt;&amp;hellip;&lt;br /&gt;ValidatorEngine ve = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ValidatorEngine();&lt;br /&gt;&lt;br /&gt;NHVConfiguration nhvc = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; NHVConfiguration();&lt;br /&gt;nhvc.Properties[Environment.ApplyToDDL] = &lt;span style="color:#006080;"&gt;&amp;quot;false&amp;quot;&lt;/span&gt;;&lt;br /&gt;nhvc.Properties[Environment.AutoregisterListeners] = &lt;span style="color:#006080;"&gt;&amp;quot;false&amp;quot;&lt;/span&gt;;&lt;br /&gt;nhvc.Properties[Environment.ValidatorMode] = GetMode();&lt;br /&gt;nhvc.Mappings.Add(&lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; MappingConfiguration(&lt;span style="color:#006080;"&gt;&amp;quot;NHibernate.Validator.Demo.Model&amp;quot;&lt;/span&gt;,&lt;span style="color:#0000ff;"&gt;null&lt;/span&gt;));&lt;br /&gt;ve.Configure(nhvc);  &lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;The &lt;i&gt;ValidatorEngine&lt;/i&gt;&amp;nbsp;will be set with the configuration. The NHibernate Validator configuration is represented by the &lt;i&gt;NHibernate.Validator.Cfg.NHVConfiguration&lt;/i&gt;&amp;nbsp;type. Other class used to configure all the stuff is the &lt;i&gt;NHibernate.Validator.Cfg.Environment&lt;/i&gt;&amp;nbsp;type, this class contain the string with the names of the properties of the configuration of NHibernate Validator.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 class="title" style="clear:both;"&gt;Declarative configuration: using Xml files&lt;/h2&gt;
&lt;p&gt;The declarative configuration keeps configuration out of the code. This means configuring the application using a different file, in this case a XML file. But the options don&amp;#39;t end here: you can choose to configure NHibernate Validator using a simple Xml file or you can put the configuration in the well known App.Config/Web.Config. &lt;/p&gt;
&lt;div class="section"&gt;
&lt;h3 class="title"&gt;Simple xml file&lt;/h3&gt;
&lt;p&gt;This is a sample of a simple configuration in a xml file:&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#800000;"&gt;xml&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;version&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;1.0&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;encoding&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;utf-8&amp;quot;&lt;/span&gt; ?&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;nhv-configuration&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;#39;urn:nhv-configuration-1.0&amp;#39;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;#39;apply_to_ddl&amp;#39;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;true&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;#39;autoregister_listeners&amp;#39;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;true&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;#39;default_validator_mode&amp;#39;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;UseXml&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;mapping&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;assembly&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;#39;NHibernate.Validator.Demo.Model&amp;#39;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;nhv-configuration&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;We encourage to name the configuration file &lt;b&gt;nhvalidator.cfg.xml&lt;/b&gt;, because NHibernate Validator will pick up this file from the application directory in order to get the configuration parameters, however you can name this file as you wish.&lt;/p&gt;
&lt;p&gt;In order to configurate NHibernate Validator with the &lt;b&gt;nhvalidator.cfg.xml&lt;/b&gt;&amp;nbsp;you can use the following codeו:&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;ValidatorEngine engine = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ValidatorEngine();&lt;br /&gt;engine.Configure(); &lt;span style="color:#008000;"&gt;// by convention reads the nhvalidator.cfg.xml file.&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;You can choose another name for your configuration file and proceed of this manner: &lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;ValidatorEngine engine = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ValidatorEngine();&lt;br /&gt;engine.Configure(&lt;span style="color:#006080;"&gt;&amp;quot;path/to/MyNHVConfigurationFile.xml&amp;quot;&lt;/span&gt;); &lt;span style="color:#008000;"&gt;// you can use whatever path to point to the configuration file&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h3 class="title"&gt;Configuration Application file: app.config/web.config&lt;/h3&gt;
&lt;p&gt;The configuration can be declared into our configuration application .Net files: app.config or web.config. In the below example you will see how to do it.&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#800000;"&gt;xml&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;version&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;1.0&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;encoding&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;utf-8&amp;quot;&lt;/span&gt; ?&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;configuration&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;configSections&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;section&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;nhv-configuration&amp;quot;&lt;/span&gt; &lt;br /&gt;                            &lt;span style="color:#ff0000;"&gt;type&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;NHibernate.Validator.Cfg.ConfigurationSectionHandler, NHibernate.Validator&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;        ...&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;configSections&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    ...&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;nhv-configuration&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;#39;urn:nhv-configuration-1.0&amp;#39;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;shared_engine_provider&lt;/span&gt; &lt;br /&gt;         &lt;span style="color:#ff0000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;#39;NHibernate.Validator.Event.NHibernateSharedEngineProvider, NHibernate.Validator&amp;#39;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;nhv-configuration&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    ...&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;configuration&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;      &lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;</description></item><item><title>NHibernate Validator 1.0.0</title><link>http://nhforge.org/wikis/validator/nhibernate-validator-1-0-0-documentation/revision/8.aspx</link><pubDate>Wed, 17 Sep 2008 16:23:24 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:217</guid><dc:creator>Román</dc:creator><description>Revision 8 posted to Validator by Román on 17/09/2008 01:23:24 p.m.&lt;br /&gt;
&lt;h2&gt;NHibernate Validator 1.0.0&lt;/h2&gt;
&lt;h1&gt;Preface&lt;/h1&gt;
&lt;div class="preface"&gt;
&lt;p&gt;Attributes are a very convenient and elegant way to specify invariant constraints for a domain model. You can, for example, express that a property should never be null, that the account balance should be strictly positive, etc. These domain model constraints are declared in the class itself by annotating its properties. A validator can then read them and check for constraint violations. The validation mechanism can be executed in different layers in your application without having to duplicate any of these rules (presentation layer, data access layer). Following the DRY principle, NHibernate Validator has been designed for that purpose. NHibernate Validator also lets you declare the same rules in XML files. Both approaches (attributes and XML) give us interesting possibilities for configuring our domain rules. &lt;/p&gt;
&lt;p&gt;NHibernate Validator works on two levels. First, it is able to check in-memory instances of a class for constraint violations. Second, it can apply the constraints to the NHibernate metamodel and incorporate them into the generated database schema.&lt;/p&gt;
&lt;p&gt;Each constraint attribute is associated to a validator implementation responsible for checking the constraint on the entity instance. A validator can also (optionally) apply the constraint to the NHibernate metamodel, allowing NHibernate to generate DDL that expresses the constraint. With the appropriate event listener, you can execute the checking operation on inserts and updates done by NHibernate. NHibernate Validator is not limited to only be used with NHibernate. You can easily use it anywhere in your application as well, indeed, you can use it at UI validation with Winforms or Asp.Net technologies.&lt;/p&gt;
&lt;p&gt;When checking instances at runtime, NHibernate Validator returns information about constraint violations in an array of &lt;i&gt;InvalidValue&lt;/i&gt;s. Among other information, the &lt;i&gt;InvalidValue&lt;/i&gt; contains an error description message that can embed the parameter values bundle with the annotation (eg. length limit), and message strings that may be externalized to a ResourceManager.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="chapter"&gt;
&lt;h1&gt;About the project&lt;/h1&gt;
&lt;p&gt;NHibernate Validator is a powerful and extensible framework to validate objects using the .Net Platform. Born as a port of &lt;span class="italic"&gt;Hibernate Validator 3.0.0&lt;/span&gt; project at december of 2007. Later on, many improvements and changes were made to the Core and API, and now we can say that NHibernate Validator is more than just a port. NHibernate Validator is entirely written in C#. The first release of NHibernate Validator was entirely developed by &lt;span class="italic"&gt;Dario Quintana&lt;/span&gt;, &lt;span class="italic"&gt;Gustavo Ringel&lt;/span&gt; and &lt;span class="italic"&gt;Fabio Maulo&lt;/span&gt;. We appreciate the help of &lt;span class="italic"&gt;Davy Brion&lt;/span&gt; in the revision of this document.&lt;/p&gt;
&lt;p&gt;NHibernate Validator is part of the &lt;a href="http://sourceforge.net/projects/nhcontrib/" target="_top"&gt;NHibernate Contrib&lt;/a&gt; project and you can download it&amp;nbsp;&lt;a href="http://sourceforge.net/project/showfiles.php?group_id=216446&amp;amp;package_id=275108&amp;amp;release_id=621966"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;You can ask questions about the development and utilization of NHibernate Validator on the following mailing lists:&lt;/p&gt;
&lt;p&gt;The development mailing list can be found &lt;a href="http://groups.google.com/group/nhcdevs" target="_top"&gt;here&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;The user forum can be found &lt;a href="http://groups.google.com/group/nhusers" target="_top"&gt;here&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;Bug and issues report should be sent to the development mailing list.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="chapter"&gt;
&lt;h1&gt;Using the Validator framework&lt;/h1&gt;
&lt;p&gt;NHibernate Validator is intended to be used to implement multi-layered data validation, where constraints are expressed in a single place and checked in various different layers of the application.&lt;/p&gt;
&lt;p&gt;This chapter will cover NHibernate Validator usage for different layers&lt;/p&gt;
&lt;div class="section"&gt;
&lt;h2 class="title" style="clear:both;"&gt;Database schema-level validation&lt;/h2&gt;
&lt;p&gt;Out of the box, NHibernate Validator will translate the constraints you have defined for your entities into mapping metadata. For example, if a property of your entity has the attribute [NotNull], its columns will be declared as &lt;i&gt;not null&lt;/i&gt; in the DDL schema generated by NHibernate.&lt;/p&gt;
&lt;p&gt;Using hbm2ddl, domain model constraints will be expressed into the database schema.&lt;/p&gt;
&lt;p&gt;If, for some reason, the feature needs to be disabled, set &lt;b&gt;&lt;i&gt;apply_to_ddl&lt;/i&gt;&lt;/b&gt; to false.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 class="title" style="clear:both;"&gt;ORM integration&lt;/h2&gt;
&lt;p&gt;NHibernate Validator integrates with NHibernate in a native way, and knows how to treat every persistent entity.&lt;/p&gt;
&lt;div class="section"&gt;
&lt;h3 class="title"&gt;NHibernate event-based validation&lt;/h3&gt;
&lt;p&gt;NHibernate Validator has two built-in NHibernate event listeners. Whenever a PreInsertEvent or PreUpdateEvent occurs, the listeners will verify all constraints of the entity instance and throw an exception if any of them are violated. Basically, objects will be checked before any insert and before any update triggered by NHibernate. This includes cascading changes! This is the most convenient and easiest way to activate the validation process. If a constraint is violated, the event will raise a runtime InvalidStateException which contains an array of &lt;i&gt;InvalidValue&lt;/i&gt;s describing each failure.&lt;/p&gt;
&lt;p&gt;If, for some reason, you want to disable this integration, set &lt;b&gt;autoregister_listeners&lt;/b&gt;&amp;nbsp;to false&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;span style="text-decoration:underline;"&gt;Note&lt;/span&gt;: If the entities are not annotated with validation attributes, there is no runtime performance cost.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;In case you wish to set manually the event listeners for NHibernate Core, use the following configuration in &lt;b&gt;hibernate.cfg.xml&lt;/b&gt;:&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    ...&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;event&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;type&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;pre-update&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;listener&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;NHibernate.Validator.Event.ValidatePreUpdateEventListener&amp;quot;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;NHibernate.Validator.Event.ValidatePreUpdateEventListener,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;NHibernate.Validator&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;event&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;event&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;type&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;pre-insert&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;listener&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;NHibernate.Validator.Event.ValidatePreInsertEventListener&amp;quot;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;NHibernate.Validator.Event.ValidatePreInsertEventListener,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;NHibernate.Validator&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;event&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 class="title" style="clear:both;"&gt;Application-level validation&lt;/h2&gt;
&lt;p&gt;In order to interact with NHibernate Validator in your application you must use the main class: &lt;i&gt;ValidatorEngine&lt;/i&gt;. &lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;ValidatorEngine validator = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ValidatorEngine();&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; isValid = validator.IsValid(customer);&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;These two lines are enough to validate an object with its validation rules, which can be configured using Attributes or XML. The first line is the instantiation of the validator engine of NHibernate Validator. The second line is the validation of an object, and the result of the &lt;i&gt;IsValid(...)&lt;/i&gt;method indicates if the object is valid or not. In order to get the invalid values of an object, you can do:&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;ValidatorEngine validator = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ValidatorEngine();&lt;br /&gt;InvalidValue[] validationMessages = validator.GetInvalidValues(customer);&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Now instead of only knowing if the object is valid or not, we get an array of InvalidValues. If this array length is greater than zero then there are validation errors and you can traverse the InvalidValue array to get verbose information about the problems that cause the object to be invalid. &lt;/p&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 class="title" style="clear:both;"&gt;Presentation layer validation&lt;/h2&gt;
&lt;p&gt;[&lt;b&gt;&lt;span style="color:#008000;"&gt;TODO&lt;/span&gt;&lt;/b&gt;]&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 class="title" style="clear:both;"&gt;Validation informations&lt;/h2&gt;
&lt;p&gt;NHibernate Validator provides an array of InvalidValue. Each &lt;i&gt;InvalidValue&lt;/i&gt;&amp;nbsp;has several methods describing the reasons that caused the entity to be invalid.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;BeanClass&lt;/b&gt; retrieves the failing bean type&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Bean&amp;nbsp;&lt;/b&gt;retrieves the failing instance (if any ie not when using &lt;i&gt;GetPotentialInvalidValues()&lt;/i&gt;)&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Value&lt;/b&gt;&amp;nbsp;retrieves the failing value&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Message&lt;/b&gt;&amp;nbsp;retrieves the proper internationalized error message&lt;/p&gt;
&lt;p&gt;&lt;b&gt;RootBean&lt;/b&gt;&amp;nbsp;retrieves the root bean instance generating the issue (useful in conjunction with &lt;i&gt;[Valid]&lt;/i&gt;), is null if &lt;i&gt;GetPotentialInvalidValues(...)&lt;/i&gt; is used.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;PropertyPath&lt;/b&gt;&amp;nbsp;retrieves the dotted path of the failing property starting from the root bean&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class="chapter"&gt;
&lt;h1&gt;Defining constraints&lt;/h1&gt;
&lt;div class="section"&gt;
&lt;h2 class="title" style="clear:both;"&gt;What is a constraint?&lt;/h2&gt;
&lt;p&gt;A constraint is a rule that a given element (field, property or type) has to comply to. The rule semantic is expressed by an Attribute or configured in Xml files. A constraint may have some properties to configure the associated validator, for example a Min constraint will have a value property setting the minimum acceptable value for the element to be valid. &lt;/p&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 class="title" style="clear:both;"&gt;Built in constraints&lt;/h2&gt;
&lt;p&gt;Hibernate Validator comes with some built-in constraints, which cover most of the basic data checks. As we&amp;#39;ll see later, you&amp;#39;re not limited to them, you can literally in a minute write your own constraints.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Table Built-in constraints&lt;/b&gt;&lt;/p&gt;
&amp;lt;tbody&amp;gt;
&amp;lt;/tbody&amp;gt;
&lt;table bgcolor="#ffffc8" cellpadding="2" cellspacing="1"&gt;
&lt;span style="background: SpringGreen;"&gt;&amp;lt;tbody&amp;gt;&lt;/span&gt;
&amp;lt;tbody&amp;gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;b&gt;Constraint&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;b&gt;Apply on&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;b&gt;Runtime checking&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;b&gt;NHibernate Metadata impact&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;b&gt;Length(Min=, Max=)&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property (String)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;checks if the string length is between the min-max range&lt;/span&gt;&lt;/td&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;Column length will be set to max&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;b&gt;Max(Value=)&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property (numeric or string representation of a numeric)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;checks if the value is less than or equals to max&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;Add a check constraint on the column&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;b&gt;Min(Value=)&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property (numeric or string representation of a numeric)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;checks if the value is more than or equals to min&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;Add a check constraint on the column&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;b&gt;NotNull&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;checks if the value is not null&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;Column(s) are not null&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;b&gt;NotEmpty&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;checks if the string is not empty.&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;b&gt;NotEmptyOrNull&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;Checks if the string is not null or not empty.&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;b&gt;Past&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property (DateTime)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;check if the date is in the past&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;b&gt;Future&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property (DateTime)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;check if the date is in the future&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;b&gt;Pattern(Regex=&amp;quot;regexp&amp;quot;, Flag=)&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property (string)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;check if the property matches the regular expression given a match flag. The &lt;span style="font-family:&amp;#39;Trebuchet MS&amp;#39;;"&gt;&amp;lt;tt&amp;gt;Flag&amp;lt;/tt&amp;gt; parameter is a &lt;i&gt;System.Text.RegularExpressions.RegexOptions&lt;/i&gt;&amp;nbsp;enum. You can also declare multiple Pattern attributes on the same member.&lt;/span&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;b&gt;Range(Min=, Max=)&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property (numeric or string representation of a numeric)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;check if the value is between Min and Max (included)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;b&gt;Size(Min=, Max=)&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property (array, collection, map)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;check if the element size is between Min and Max (included)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;b&gt;AssertFalse&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;check that the method evaluates to false (useful for constraints expressed in code rather than attributes)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;b&gt;AssertTrue&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;check that the method evaluates to true (useful for constraints expressed in code rather than attributes)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;b&gt;Valid&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property (object)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;perform validation recursively on the associated object. If the object is a Collection or an array, the elements are validated recursively. If the object is a Map, the value elements are validated recursively.&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;b&gt;Email&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property (String)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;check whether the string conforms to the email address specification&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;b&gt;Digits&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property (numeric or string representation of a numeric)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;checks whether the property is a number having up to&amp;nbsp;&lt;span style="font-family:&amp;#39;Trebuchet MS&amp;#39;;"&gt;&lt;i&gt;integerDigits&lt;/i&gt; integer digits and &lt;i&gt;fractionalDigits&lt;/i&gt;&amp;nbsp;fractonal digits&lt;/span&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;define column precision and scale&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&amp;lt;/tbody&amp;gt;
&lt;span style="background: SpringGreen;"&gt;&amp;lt;/tbody&amp;gt;&lt;/span&gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 class="title" style="clear:both;"&gt;Error messages&lt;/h2&gt;
&lt;p&gt;NHibernate Validator comes with a default set of error messages translated in several languages (if yours is not present, please send us a patch). You can add your own additional set of messages while writing your validator Attributes or at XML. If NHibernate Validator cannot resolve a key from your ResourceManager nor from ValidatorMessage, it falls back to the default built-in values.&lt;/p&gt;
&lt;p&gt;Alternatively you can provide a ResourceManager while checking programmatically the validation rules on a bean or if you want a completly different interpolation mechanism, you can provide an implementation of NHibernate.Validator.Engine.IMessageInterpolator.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 class="title" style="clear:both;"&gt;Writing your own constraints&lt;/h2&gt;
&lt;p&gt;Extending the set of built-in constraints is extremely easy. Any constraint consists of two pieces: the constraint &lt;span class="emphasis"&gt;&lt;i&gt;descriptor&lt;/i&gt;&lt;/span&gt; (the attribute) and the constraint &lt;span class="emphasis"&gt;&lt;i&gt;validator&lt;/i&gt;&lt;/span&gt; (the implementation class). Here is a simple user-defined descriptor:&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]&lt;br /&gt;[ValidatorClass(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt;(PhoneValidator))]&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; PhoneAttribute : Attribute, IRuleArgs&lt;br /&gt;{&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; message = &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt;.Empty;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Message&lt;br /&gt;    {&lt;br /&gt;        get { &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; message; }&lt;br /&gt;        set { message = &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;; }&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;To link a descriptor to its validator implementation, we use the &lt;i&gt;[ValidatorClass]&lt;/i&gt;&amp;nbsp;meta-attribute. Here you can see an implementation of a constraint validator:&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; PhoneValidator : IValidator&lt;br /&gt;{&lt;br /&gt;     &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; IsValid(&lt;span style="color:#0000ff;"&gt;object&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;)&lt;br /&gt;     {&lt;br /&gt;         Regex regex = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Regex(&lt;span style="color:#006080;"&gt;@&amp;quot;^[2-9]\d{2}-\d{3}-\d{4}$&amp;quot;&lt;/span&gt;);&lt;br /&gt;         &lt;span style="color:#0000ff;"&gt;if&lt;/span&gt; (&lt;span style="color:#0000ff;"&gt;value&lt;/span&gt; == &lt;span style="color:#0000ff;"&gt;null&lt;/span&gt;) &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;;&lt;br /&gt;         &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; regex.IsMatch(&lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;.ToString());&lt;br /&gt;     }&lt;br /&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;This PhoneValidator class is simple. The method &lt;i&gt;IsValid()&lt;/i&gt;&amp;nbsp;receives a string that should be validated with a Regex expression. The &lt;i&gt;IsValid()&lt;/i&gt;&amp;nbsp;method should return false if the constraint has been violated.&lt;/p&gt;
&lt;p&gt;If you need to initialize the class validator you can implement the interface &lt;i&gt;IInitializableValidator&lt;/i&gt;&lt;i&gt;&amp;lt;A&amp;gt;&amp;nbsp;&lt;/i&gt;and the class should look like this:&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; PhoneValidator : IInitializableValidator&amp;lt;PhoneAttribute&amp;gt;&lt;br /&gt;{&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; Regex regex;&lt;br /&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; IsValid(&lt;span style="color:#0000ff;"&gt;object&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;)&lt;br /&gt;    {&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;if&lt;/span&gt; (&lt;span style="color:#0000ff;"&gt;value&lt;/span&gt; == &lt;span style="color:#0000ff;"&gt;null&lt;/span&gt;) &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;;&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; regex.IsMatch(&lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;.ToString());&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Initialize(PhoneAttribute parameters)&lt;br /&gt;    {&lt;br /&gt;        regex = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Regex(&lt;span style="color:#006080;"&gt;@&amp;quot;^[2-9]\d{2}-\d{3}-\d{4}$&amp;quot;&lt;/span&gt;);&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;As you can see, implementing the &lt;i&gt;IInitializableValidator&amp;lt;A&amp;gt;&lt;/i&gt;&amp;nbsp;interface we have to implement the method &lt;i&gt;Initialize(...)&lt;/i&gt; that initializes the validator, and it&amp;#39;s executed first of all.&lt;/p&gt;
&lt;p&gt;The &lt;i&gt;IsValid()&lt;/i&gt;&amp;nbsp;method should return &lt;i&gt;false&lt;/i&gt;&amp;nbsp;if the constraint has been violated. For more examples, refer to the built-in validator implementations.&lt;/p&gt;
&lt;p&gt;We now have to implement the validator (ie. the rule checking implementation). A validation implementation can check the value of the a property (by implementing PropertyConstraint ) and/or can modify the NHibernate mapping metadata to express the constraint at the database level (by implementing PersistentClassConstraint)&lt;/p&gt;
&lt;p&gt;We have only seen property level validation, but you can write a class level validation attribute. Instead of receiving the return instance of a property, the bean itself will be passed to the validator. To activate the validation checking, just put the validation attribute to the class itself instead of in a property. A small sample can be found in the unit test suite.&lt;/p&gt;
&lt;p&gt;If your constraint can be applied multiple times (with different parameters) on the same property or type, you can use set the &lt;i&gt;AllowMultiple&lt;/i&gt;&amp;nbsp;parameter to &lt;i&gt;true&lt;/i&gt;&amp;nbsp;on the &lt;i&gt;AttributeUsage&lt;/i&gt;&amp;nbsp;attribute. An example of this it&amp;#39;s the built-in validator Pattern, have a look:&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;[Serializable]&lt;br /&gt;[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = &lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;)]&lt;br /&gt;[ValidatorClass(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt; (PatternValidator))]&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; PatternAttribute : Attribute, IRuleArgs&lt;br /&gt;{&lt;br /&gt;    &amp;hellip;&lt;br /&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;pre class="programlisting"&gt;&amp;nbsp;&lt;/pre&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 class="title" style="clear:both;"&gt;Configuring your domain model with .Net Attributes&lt;/h2&gt;
&lt;p&gt;Since you are already familiar with attributes now, the syntax should be very familiar&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; Address&lt;br /&gt;{ &lt;br /&gt;    &lt;span style="color:#008000;"&gt;// a static non null field  &lt;/span&gt;&lt;br /&gt;    [NotNull]&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;static&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; blacklistedZipCode;&lt;br /&gt;    &lt;br /&gt;    &lt;span style="color:#008000;"&gt;// field a not null string of 20 characters maximum&lt;/span&gt;&lt;br /&gt;    [Length(Max = 20), NotNull]    &lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; country;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;long&lt;/span&gt; id;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; internalValid = &lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; line1;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; line2;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; state;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; zip;&lt;br /&gt;    &lt;br /&gt;    &lt;span style="color:#008000;"&gt;// a numeric between 1 and 2000&lt;/span&gt;&lt;br /&gt;    [Min(1), Range(Max = 2000)]&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;long&lt;/span&gt; Id&lt;br /&gt;    {&lt;br /&gt;        get { &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; id; }&lt;br /&gt;        set { id = &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;; }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;span style="color:#008000;"&gt;// respond to the constraint in the field: country&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Country&lt;br /&gt;    {&lt;br /&gt;        get { &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; country; }&lt;br /&gt;        set { country = &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;; }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;span style="color:#008000;"&gt;// a non null string&lt;/span&gt;&lt;br /&gt;    [NotNull]&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Line1&lt;br /&gt;    {&lt;br /&gt;        get { &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; line1; }&lt;br /&gt;        set { line1 = &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;; }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;span style="color:#008000;"&gt;// a not null string of 3 characters maximum&lt;/span&gt;&lt;br /&gt;    [Length(Max = 3), NotNull]&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; State&lt;br /&gt;    {&lt;br /&gt;        get { &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; state; }&lt;br /&gt;        set { state = &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;; }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;span style="color:#008000;"&gt;// a not null numeric string of 5 characters maximum&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#008000;"&gt;// if the string is longer, the message will&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#008000;"&gt;// be searched in the resource bundle at key &amp;#39;long&amp;#39;&lt;/span&gt;&lt;br /&gt;    [Length(Max = 5, Message = &lt;span style="color:#006080;"&gt;&amp;quot;{long}&amp;quot;&lt;/span&gt;)]&lt;br /&gt;    [Pattern(Regex = &lt;span style="color:#006080;"&gt;&amp;quot;[0-9]+&amp;quot;&lt;/span&gt;)]&lt;br /&gt;    [NotNull]&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Zip&lt;br /&gt;    {&lt;br /&gt;&lt;br /&gt;        get { &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; zip; }&lt;br /&gt;        set { zip = &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;; }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;span style="color:#008000;"&gt;// no constraint&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Line2&lt;br /&gt;    {&lt;br /&gt;        get { &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; line2; }&lt;br /&gt;        set { line2 = &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;; }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;span style="color:#008000;"&gt;// should always be true&lt;/span&gt;&lt;br /&gt;    [AssertTrue]&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; InternalValid&lt;br /&gt;    {&lt;br /&gt;        get { &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; internalValid; }&lt;br /&gt;        set { internalValid = &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;; }&lt;br /&gt;    }&lt;br /&gt;} &lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;While the example only shows public property validation, you can also annotate fields of any kind of visibility. In the example &lt;b&gt;MyBeanConstraint&lt;/b&gt; is a custom Class Validator, we will talk about this later.&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;[MyBeanConstraint(Max=45)]&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; Dog &lt;br /&gt;{&lt;br /&gt;    [AssertTrue] &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; IsMale;&lt;br /&gt;    [NotNull] &lt;span style="color:#0000ff;"&gt;protected&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Name { get {...} set{...} }&lt;br /&gt;    &amp;hellip;&lt;br /&gt;}     &lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;You can also annotate interfaces. NHibernate Validator will check all superclasses and interfaces extended or implemented by a given type to read the appropriate validator attributes.&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;interface&lt;/span&gt; Named &lt;br /&gt;{&lt;br /&gt;    [NotNull] &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Name {get; set;}&lt;br /&gt;    ...&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; Dog : Named &lt;br /&gt;{&lt;br /&gt;    [AssertTrue] &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; isMale;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Name { get {...} set{...} }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;The name property will be checked for nullity when the Dog type is validated.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 class="title" style="clear:both;"&gt;Configuring your domain model with XML&lt;/h2&gt;
&lt;p&gt;You also can configurate your domain model with constraints using xml files: the nhv.xml files. We prefer to embed these Xml files into the assemblies, and NHibernate Validator is prepared to inspect them and retrieve the resources with constrains, like NHibernate does with hbm.xml files.&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#800000;"&gt;xml&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;version&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;1.0&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;encoding&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;utf-8&amp;quot;&lt;/span&gt; ?&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;nhv-mapping&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;urn:nhibernate-validator-1.0&amp;quot;&lt;/span&gt;&lt;br /&gt;                        &lt;span style="color:#ff0000;"&gt;namespace&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;NHibernate.Validator.Tests.Base&amp;quot;&lt;/span&gt;&lt;br /&gt;                        &lt;span style="color:#ff0000;"&gt;assembly&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;NHibernate.Validator.Tests&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;class&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Address&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;blacklistedZipCode&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;                  &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;not-null&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;country&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;           &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;length&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;max&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;20&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;           &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;not-null&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;    &lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;floor&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;    &lt;br /&gt;          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;range&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;min&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;-2&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;max&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;50&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;message&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;{floor.out.of.range} (escaping #{el})&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Id&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;    &lt;br /&gt;          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;min&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;value&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;1&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;range&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;max&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;2000&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;max&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;value&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;2500&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Line1&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;    &lt;br /&gt;          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;not-null&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;State&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;    &lt;br /&gt;          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;not-null&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;length&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;max&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;3&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Zip&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;length&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;max&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;5&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;message&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;{long}&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;pattern&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;regex&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;[0-9]+&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;not-null&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;InternalValid&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;    &lt;br /&gt;          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;asserttrue&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;nhv-mapping&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;      &lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;span class="bold"&gt;&lt;b&gt;We strongly recommend&lt;/b&gt;&lt;/span&gt; to validate every nhv.xml file of your application with the respective XSD file. Every &lt;b&gt;nhv.xml&lt;/b&gt; file should be validated with the &lt;b&gt;nhv-mapping.xsd&lt;/b&gt;&amp;nbsp;file included within the NHibernate Validator distribution whether in source or binaries. Indeed, if you are using an IDE like Visual Studio it offers IntelliSense support in the &lt;b&gt;nhv.xml&lt;/b&gt; file if you add the schema to Visual Studio&amp;#39;s xml schemas folder.&lt;/p&gt;
&lt;p&gt;&lt;span class="bold"&gt;&lt;b&gt;Xml beauties:&lt;/b&gt;&lt;/span&gt; The nicest part of Xml for validation is that you can put xml validators in a different dll so you don&amp;#39;t need to recompile your domain when validation rules change. Only the dll with the Xml files..&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class="chapter"&gt;
&lt;h1&gt;Configuration&lt;/h1&gt;
&lt;p&gt;NHibernate Validator has global parameters that you can configure at the startup. This configuration can be done in differents ways: programmaticaly or using xml files. If you are involved with NHibernate this could be very familiar to you.&lt;/p&gt;
&lt;p&gt;Configuration properties:&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Table Configuration properties&lt;/b&gt;&lt;/p&gt;
&lt;div class="table"&gt;
&amp;lt;tbody&amp;gt;
&amp;lt;/tbody&amp;gt;
&lt;table bgcolor="#ffffc8" cellpadding="2" cellspacing="1"&gt;
&lt;span style="background: SpringGreen;"&gt;&amp;lt;tbody&amp;gt;&lt;/span&gt;
&amp;lt;tbody&amp;gt;
&lt;tr&gt;
&lt;th align="middle"&gt;Property name&lt;/th&gt;
&lt;th&gt;Purpose&lt;/th&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="middle"&gt;&lt;b&gt;apply_to_ddl&lt;/b&gt;&lt;/td&gt;
&lt;td&gt;Configure NHibernate to apply the constraints to the DDL.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="middle"&gt;&lt;b&gt;autoregister_listeners&lt;/b&gt;&lt;/td&gt;
&lt;td&gt;Register the Event-Listeners automatically on Inserts and Updates.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="middle"&gt;&lt;b&gt;default_validator_mode&lt;/b&gt;&lt;/td&gt;
&lt;td&gt;Define what are the constraints sources are and how to proceed. There are some built-in configurations:
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;div class="itemizedlist"&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;b&gt;UseAttribute&lt;/b&gt; (default): Use Attributes to configurate the validators on types and/or members. Xml is ignored.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;b&gt;UseXml&lt;/b&gt;: Use Xml to configurate the validators on types and/or members. Attributes are ignored.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;b&gt;OverrideAttributeWithXml&lt;/b&gt;: If the same type/member is annotated with an Attribute and has a Xml rule the Attribute annotation will be used.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;b&gt;OverrideXmlWithAttribute&lt;/b&gt;: If the same type/member is annotated with an Attribute and has a Xml rule the Attribute annotation will be used.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="middle"&gt;&lt;b&gt;assembly&lt;/b&gt;&lt;/td&gt;
&lt;td&gt;Which are the assemblies with contains the nhv.xml files embedded. You can have more than one assembly.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="middle"&gt;&lt;b&gt;shared_engine_provider&lt;/b&gt;&lt;/td&gt;
&lt;td&gt;To inject a custom Shared Engine Provider.&lt;/td&gt;
&lt;/tr&gt;
&lt;span style="background: SpringGreen;"&gt;&amp;lt;/tbody&amp;gt;&lt;/span&gt;
&amp;lt;/tbody&amp;gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 class="title" style="clear:both;"&gt;Programmatic configuration&lt;/h2&gt;
&lt;p&gt;You can use the following code to configure NHibernate.Validator programmatically:&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NHibernate.Validator.Cfg;&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NHibernate.Validator.Engine;&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; Environment=NHibernate.Validator.Cfg.Environment;&lt;br /&gt;&amp;hellip;&lt;br /&gt;ValidatorEngine ve = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ValidatorEngine();&lt;br /&gt;&lt;br /&gt;NHVConfiguration nhvc = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; NHVConfiguration();&lt;br /&gt;nhvc.Properties[Environment.ApplyToDDL] = &lt;span style="color:#006080;"&gt;&amp;quot;false&amp;quot;&lt;/span&gt;;&lt;br /&gt;nhvc.Properties[Environment.AutoregisterListeners] = &lt;span style="color:#006080;"&gt;&amp;quot;false&amp;quot;&lt;/span&gt;;&lt;br /&gt;nhvc.Properties[Environment.ValidatorMode] = GetMode();&lt;br /&gt;nhvc.Mappings.Add(&lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; MappingConfiguration(&lt;span style="color:#006080;"&gt;&amp;quot;NHibernate.Validator.Demo.Model&amp;quot;&lt;/span&gt;,&lt;span style="color:#0000ff;"&gt;null&lt;/span&gt;));&lt;br /&gt;ve.Configure(nhvc);  &lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;The &lt;i&gt;ValidatorEngine&lt;/i&gt;&amp;nbsp;will be set with the configuration. The NHibernate Validator configuration is represented by the &lt;i&gt;NHibernate.Validator.Cfg.NHVConfiguration&lt;/i&gt;&amp;nbsp;type. Other class used to configure all the stuff is the &lt;i&gt;NHibernate.Validator.Cfg.Environment&lt;/i&gt;&amp;nbsp;type, this class contain the string with the names of the properties of the configuration of NHibernate Validator.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 class="title" style="clear:both;"&gt;Declarative configuration: using Xml files&lt;/h2&gt;
&lt;p&gt;The declarative configuration keeps configuration out of the code. This means configuring the application using a different file, in this case a XML file. But the options don&amp;#39;t end here: you can choose to configure NHibernate Validator using a simple Xml file or you can put the configuration in the well known App.Config/Web.Config. &lt;/p&gt;
&lt;div class="section"&gt;
&lt;h3 class="title"&gt;Simple xml file&lt;/h3&gt;
&lt;p&gt;This is a sample of a simple configuration in a xml file:&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#800000;"&gt;xml&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;version&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;1.0&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;encoding&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;utf-8&amp;quot;&lt;/span&gt; ?&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;nhv-configuration&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;#39;urn:nhv-configuration-1.0&amp;#39;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;#39;apply_to_ddl&amp;#39;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;true&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;#39;autoregister_listeners&amp;#39;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;true&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;#39;default_validator_mode&amp;#39;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;UseXml&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;mapping&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;assembly&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;#39;NHibernate.Validator.Demo.Model&amp;#39;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;nhv-configuration&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;We encourage to name the configuration file &lt;b&gt;nhvalidator.cfg.xml&lt;/b&gt;, because NHibernate Validator will pick up this file from the application directory in order to get the configuration parameters, however you can name this file as you wish.&lt;/p&gt;
&lt;p&gt;In order to configurate NHibernate Validator with the &lt;b&gt;nhvalidator.cfg.xml&lt;/b&gt;&amp;nbsp;you can use the following codeו:&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;ValidatorEngine engine = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ValidatorEngine();&lt;br /&gt;engine.Configure(); &lt;span style="color:#008000;"&gt;// by convention reads the nhvalidator.cfg.xml file.&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;You can choose another name for your configuration file and proceed of this manner: &lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;ValidatorEngine engine = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ValidatorEngine();&lt;br /&gt;engine.Configure(&lt;span style="color:#006080;"&gt;&amp;quot;path/to/MyNHVConfigurationFile.xml&amp;quot;&lt;/span&gt;); &lt;span style="color:#008000;"&gt;// you can use whatever path to point to the configuration file&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h3 class="title"&gt;Configuration Application file: app.config/web.config&lt;/h3&gt;
&lt;p&gt;The configuration can be declared into our configuration application .Net files: app.config or web.config. In the below example you will see how to do it.&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#800000;"&gt;xml&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;version&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;1.0&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;encoding&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;utf-8&amp;quot;&lt;/span&gt; ?&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;configuration&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;configSections&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;section&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;nhv-configuration&amp;quot;&lt;/span&gt; &lt;br /&gt;                            &lt;span style="color:#ff0000;"&gt;type&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;NHibernate.Validator.Cfg.ConfigurationSectionHandler, NHibernate.Validator&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;        ...&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;configSections&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    ...&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;nhv-configuration&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;#39;urn:nhv-configuration-1.0&amp;#39;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;shared_engine_provider&lt;/span&gt; &lt;br /&gt;         &lt;span style="color:#ff0000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;#39;NHibernate.Validator.Event.NHibernateSharedEngineProvider, NHibernate.Validator&amp;#39;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;nhv-configuration&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    ...&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;configuration&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;      &lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;</description></item><item><title>NHibernate Validator 1.0.0</title><link>http://nhforge.org/wikis/validator/nhibernate-validator-1-0-0-documentation/revision/7.aspx</link><pubDate>Sat, 13 Sep 2008 23:23:03 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:126</guid><dc:creator>Dario Quintana</dc:creator><description>Revision 7 posted to Validator by Dario Quintana on 13/09/2008 08:23:03 p.m.&lt;br /&gt;
&lt;h2&gt;NHibernate Validator 1.0.0&lt;/h2&gt;
&lt;h1&gt;Preface&lt;/h1&gt;
&lt;div class="preface"&gt;
&lt;p&gt;Attributes are a very convenient and elegant way to specify invariant constraints for a domain model. You can, for example, express that a property should never be null, that the account balance should be strictly positive, etc. These domain model constraints are declared in the class itself by annotating its properties. A validator can then read them and check for constraint violations. The validation mechanism can be executed in different layers in your application without having to duplicate any of these rules (presentation layer, data access layer). Following the DRY principle, NHibernate Validator has been designed for that purpose. NHibernate Validator also lets you declare the same rules in XML files. Both approaches (attributes and XML) give us interesting possibilities for configuring our domain rules. &lt;/p&gt;
&lt;p&gt;NHibernate Validator works on two levels. First, it is able to check in-memory instances of a class for constraint violations. Second, it can apply the constraints to the NHibernate metamodel and incorporate them into the generated database schema.&lt;/p&gt;
&lt;p&gt;Each constraint attribute is associated to a validator implementation responsible for checking the constraint on the entity instance. A validator can also (optionally) apply the constraint to the NHibernate metamodel, allowing NHibernate to generate DDL that expresses the constraint. With the appropriate event listener, you can execute the checking operation on inserts and updates done by NHibernate. NHibernate Validator is not limited to only be used with NHibernate. You can easily use it anywhere in your application as well, indeed, you can use it at UI validation with Winforms or Asp.Net technologies.&lt;/p&gt;
&lt;p&gt;When checking instances at runtime, NHibernate Validator returns information about constraint violations in an array of &lt;em&gt;InvalidValue&lt;/em&gt;s. Among other information, the &lt;em&gt;InvalidValue&lt;/em&gt; contains an error description message that can embed the parameter values bundle with the annotation (eg. length limit), and message strings that may be externalized to a ResourceManager.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="chapter"&gt;
&lt;h1&gt;About the project&lt;/h1&gt;
&lt;p&gt;NHibernate Validator is a powerful and extensible framework to validate objects using the .Net Platform. Born as a port of &lt;span class="italic"&gt;Hibernate Validator 3.0.0&lt;/span&gt; project at december of 2007. Later on, many improvements and changes were made to the Core and API, and now we can say that NHibernate Validator is more than just a port. NHibernate Validator is entirely written in C#. The first release of NHibernate Validator was entirely developed by &lt;span class="italic"&gt;Dario Quintana&lt;/span&gt;, &lt;span class="italic"&gt;Gustavo Ringel&lt;/span&gt; and &lt;span class="italic"&gt;Fabio Maulo&lt;/span&gt;. We appreciate the help of &lt;span class="italic"&gt;Davy Brion&lt;/span&gt; in the revision of this document.&lt;/p&gt;
&lt;p&gt;NHibernate Validator is part of the &lt;a href="http://sourceforge.net/projects/nhcontrib/" target="_top"&gt;NHibernate Contrib&lt;/a&gt; project and you can download it&amp;nbsp;&lt;a href="http://sourceforge.net/project/showfiles.php?group_id=216446&amp;amp;package_id=275108&amp;amp;release_id=621966"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;You can ask questions about the development and utilization of NHibernate Validator on the following mailing lists:&lt;/p&gt;
&lt;p&gt;The development mailing list can be found &lt;a href="http://groups.google.com/group/nhcdevs" target="_top"&gt;here&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;The user forum can be found &lt;a href="http://groups.google.com/group/nhusers" target="_top"&gt;here&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;Bug and issues report should be sent to the development mailing list.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="chapter"&gt;
&lt;h1&gt;Using the Validator framework&lt;/h1&gt;
&lt;p&gt;NHibernate Validator is intended to be used to implement multi-layered data validation, where constraints are expressed in a single place and checked in various different layers of the application.&lt;/p&gt;
&lt;p&gt;This chapter will cover NHibernate Validator usage for different layers&lt;/p&gt;
&lt;div class="section"&gt;
&lt;h2 class="title" style="clear:both;"&gt;Database schema-level validation&lt;/h2&gt;
&lt;p&gt;Out of the box, NHibernate Validator will translate the constraints you have defined for your entities into mapping metadata. For example, if a property of your entity has the attribute [NotNull], its columns will be declared as &lt;em&gt;not null&lt;/em&gt; in the DDL schema generated by NHibernate.&lt;/p&gt;
&lt;p&gt;Using hbm2ddl, domain model constraints will be expressed into the database schema.&lt;/p&gt;
&lt;p&gt;If, for some reason, the feature needs to be disabled, set &lt;strong&gt;&lt;em&gt;apply_to_ddl&lt;/em&gt;&lt;/strong&gt; to false.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 class="title" style="clear:both;"&gt;ORM integration&lt;/h2&gt;
&lt;p&gt;NHibernate Validator integrates with NHibernate in a native way, and knows how to treat every persistent entity.&lt;/p&gt;
&lt;div class="section"&gt;
&lt;h3 class="title"&gt;NHibernate event-based validation&lt;/h3&gt;
&lt;p&gt;NHibernate Validator has two built-in NHibernate event listeners. Whenever a PreInsertEvent or PreUpdateEvent occurs, the listeners will verify all constraints of the entity instance and throw an exception if any of them are violated. Basically, objects will be checked before any insert and before any update triggered by NHibernate. This includes cascading changes! This is the most convenient and easiest way to activate the validation process. If a constraint is violated, the event will raise a runtime InvalidStateException which contains an array of &lt;em&gt;InvalidValue&lt;/em&gt;s describing each failure.&lt;/p&gt;
&lt;p&gt;If, for some reason, you want to disable this integration, set &lt;strong&gt;autoregister_listeners&lt;/strong&gt;&amp;nbsp;to false&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;span style="text-decoration:underline;"&gt;Note&lt;/span&gt;: If the entities are not annotated with validation attributes, there is no runtime performance cost.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;In case you wish to set manually the event listeners for NHibernate Core, use the following configuration in &lt;strong&gt;hibernate.cfg.xml&lt;/strong&gt;:&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    ...
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;event&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;type&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;pre-update&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
        &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;listener&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;NHibernate.Validator.Event.ValidatePreUpdateEventListener&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;event&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;event&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;type&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;pre-insert&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
        &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;listener&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;NHibernate.Validator.Event.ValidatePreInsertEventListener&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;event&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 class="title" style="clear:both;"&gt;Application-level validation&lt;/h2&gt;
&lt;p&gt;In order to interact with NHibernate Validator in your application you must use the main class: &lt;em&gt;ValidatorEngine&lt;/em&gt;. &lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;ValidatorEngine validator = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ValidatorEngine();
&lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; isValid = validator.IsValid(customer);&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;These two lines are enough to validate an object with its validation rules, which can be configured using Attributes or XML. The first line is the instantiation of the validator engine of NHibernate Validator. The second line is the validation of an object, and the result of the &lt;em&gt;IsValid(...)&lt;/em&gt;method indicates if the object is valid or not. In order to get the invalid values of an object, you can do:&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;ValidatorEngine validator = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ValidatorEngine();
InvalidValue[] validationMessages = validator.GetInvalidValues(customer);&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Now instead of only knowing if the object is valid or not, we get an array of InvalidValues. If this array length is greater than zero then there are validation errors and you can traverse the InvalidValue array to get verbose information about the problems that cause the object to be invalid. &lt;/p&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 class="title" style="clear:both;"&gt;Presentation layer validation&lt;/h2&gt;
&lt;p&gt;[&lt;strong&gt;&lt;span style="color:#008000;"&gt;TODO&lt;/span&gt;&lt;/strong&gt;]&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 class="title" style="clear:both;"&gt;Validation informations&lt;/h2&gt;
&lt;p&gt;NHibernate Validator provides an array of InvalidValue. Each &lt;em&gt;InvalidValue&lt;/em&gt;&amp;nbsp;has several methods describing the reasons that caused the entity to be invalid.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;BeanClass&lt;/strong&gt; retrieves the failing bean type&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Bean&amp;nbsp;&lt;/strong&gt;retrieves the failing instance (if any ie not when using &lt;em&gt;GetPotentialInvalidValues()&lt;/em&gt;)&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Value&lt;/strong&gt;&amp;nbsp;retrieves the failing value&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Message&lt;/strong&gt;&amp;nbsp;retrieves the proper internationalized error message&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;RootBean&lt;/strong&gt;&amp;nbsp;retrieves the root bean instance generating the issue (useful in conjunction with &lt;em&gt;[Valid]&lt;/em&gt;), is null if &lt;em&gt;GetPotentialInvalidValues(...)&lt;/em&gt; is used.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;PropertyPath&lt;/strong&gt;&amp;nbsp;retrieves the dotted path of the failing property starting from the root bean&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class="chapter"&gt;
&lt;h1&gt;Defining constraints&lt;/h1&gt;
&lt;div class="section"&gt;
&lt;h2 class="title" style="clear:both;"&gt;What is a constraint?&lt;/h2&gt;
&lt;p&gt;A constraint is a rule that a given element (field, property or type) has to comply to. The rule semantic is expressed by an Attribute or configured in Xml files. A constraint may have some properties to configure the associated validator, for example a Min constraint will have a value property setting the minimum acceptable value for the element to be valid. &lt;/p&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 class="title" style="clear:both;"&gt;Built in constraints&lt;/h2&gt;
&lt;p&gt;Hibernate Validator comes with some built-in constraints, which cover most of the basic data checks. As we&amp;#39;ll see later, you&amp;#39;re not limited to them, you can literally in a minute write your own constraints.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Table Built-in constraints&lt;/strong&gt;&lt;/p&gt;
&amp;lt;tbody&amp;gt;
&lt;span style="background: SpringGreen;"&gt;&amp;lt;/tbody&amp;gt;&lt;/span&gt;
&lt;table bgcolor="#ffffc8" cellspacing="1" cellpadding="2"&gt;
&lt;span style="background: SpringGreen;"&gt;&amp;lt;tbody&amp;gt;&lt;/span&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;strong&gt;Constraint&lt;/strong&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;strong&gt;Apply on&lt;/strong&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;strong&gt;Runtime checking&lt;/strong&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;strong&gt;NHibernate Metadata impact&lt;/strong&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;strong&gt;Length(Min=, Max=)&lt;/strong&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property (String)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;checks if the string length is between the min-max range&lt;/span&gt;&lt;/td&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;Column length will be set to max&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;strong&gt;Max(Value=)&lt;/strong&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property (numeric or string representation of a numeric)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;checks if the value is less than or equals to max&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;Add a check constraint on the column&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;strong&gt;Min(Value=)&lt;/strong&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property (numeric or string representation of a numeric)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;checks if the value is more than or equals to min&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;Add a check constraint on the column&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;strong&gt;NotNull&lt;/strong&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;checks if the value is not null&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;Column(s) are not null&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;strong&gt;NotEmpty&lt;/strong&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;checks if the string is not empty.&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;strong&gt;NotEmptyOrNull&lt;/strong&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;Checks if the string is not null or not empty.&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;strong&gt;Past&lt;/strong&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property (DateTime)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;check if the date is in the past&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;strong&gt;Future&lt;/strong&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property (DateTime)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;check if the date is in the future&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;strong&gt;Pattern(Regex=&amp;quot;regexp&amp;quot;, Flag=)&lt;/strong&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property (string)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;check if the property matches the regular expression given a match flag. The &lt;span style="font-family:&amp;#39;Trebuchet MS&amp;#39;;"&gt;&amp;lt;tt&amp;gt;Flag&amp;lt;/tt&amp;gt; parameter is a &lt;em&gt;System.Text.RegularExpressions.RegexOptions&lt;/em&gt;&amp;nbsp;enum. You can also declare multiple Pattern attributes on the same member.&lt;/span&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;strong&gt;Range(Min=, Max=)&lt;/strong&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property (numeric or string representation of a numeric)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;check if the value is between Min and Max (included)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;strong&gt;Size(Min=, Max=)&lt;/strong&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property (array, collection, map)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;check if the element size is between Min and Max (included)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;strong&gt;AssertFalse&lt;/strong&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;check that the method evaluates to false (useful for constraints expressed in code rather than attributes)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;strong&gt;AssertTrue&lt;/strong&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;check that the method evaluates to true (useful for constraints expressed in code rather than attributes)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;strong&gt;Valid&lt;/strong&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property (object)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;perform validation recursively on the associated object. If the object is a Collection or an array, the elements are validated recursively. If the object is a Map, the value elements are validated recursively.&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;strong&gt;Email&lt;/strong&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property (String)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;check whether the string conforms to the email address specification&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;strong&gt;Digits&lt;/strong&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property (numeric or string representation of a numeric)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;checks whether the property is a number having up to&amp;nbsp;&lt;span style="font-family:&amp;#39;Trebuchet MS&amp;#39;;"&gt;&lt;em&gt;integerDigits&lt;/em&gt; integer digits and &lt;em&gt;fractionalDigits&lt;/em&gt;&amp;nbsp;fractonal digits&lt;/span&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;define column precision and scale&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&amp;lt;/tbody&amp;gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 class="title" style="clear:both;"&gt;Error messages&lt;/h2&gt;
&lt;p&gt;NHibernate Validator comes with a default set of error messages translated in several languages (if yours is not present, please send us a patch). You can add your own additional set of messages while writing your validator Attributes or at XML. If NHibernate Validator cannot resolve a key from your ResourceManager nor from ValidatorMessage, it falls back to the default built-in values.&lt;/p&gt;
&lt;p&gt;Alternatively you can provide a ResourceManager while checking programmatically the validation rules on a bean or if you want a completly different interpolation mechanism, you can provide an implementation of NHibernate.Validator.Engine.IMessageInterpolator.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 class="title" style="clear:both;"&gt;Writing your own constraints&lt;/h2&gt;
&lt;p&gt;Extending the set of built-in constraints is extremely easy. Any constraint consists of two pieces: the constraint &lt;span class="emphasis"&gt;&lt;em&gt;descriptor&lt;/em&gt;&lt;/span&gt; (the attribute) and the constraint &lt;span class="emphasis"&gt;&lt;em&gt;validator&lt;/em&gt;&lt;/span&gt; (the implementation class). Here is a simple user-defined descriptor:&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
[ValidatorClass(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt;(PhoneValidator))]
&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; PhoneAttribute : Attribute, IRuleArgs
{
    &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; message = &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt;.Empty;
    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Message
    {
        get { &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; message; }
        set { message = &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;; }
    }
}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;To link a descriptor to its validator implementation, we use the &lt;em&gt;[ValidatorClass]&lt;/em&gt;&amp;nbsp;meta-attribute. Here you can see an implementation of a constraint validator:&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; PhoneValidator : IValidator
{
     &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; IsValid(&lt;span style="color:#0000ff;"&gt;object&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;)
     {
         Regex regex = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Regex(&lt;span style="color:#006080;"&gt;@&amp;quot;^[2-9]\d{2}-\d{3}-\d{4}$&amp;quot;&lt;/span&gt;);
         &lt;span style="color:#0000ff;"&gt;if&lt;/span&gt; (&lt;span style="color:#0000ff;"&gt;value&lt;/span&gt; == &lt;span style="color:#0000ff;"&gt;null&lt;/span&gt;) &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;;
         &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; regex.IsMatch(&lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;.ToString());
     }
}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;This PhoneValidator class is simple. The method &lt;em&gt;IsValid()&lt;/em&gt;&amp;nbsp;receives a string that should be validated with a Regex expression. The &lt;em&gt;IsValid()&lt;/em&gt;&amp;nbsp;method should return false if the constraint has been violated.&lt;/p&gt;
&lt;p&gt;If you need to initialize the class validator you can implement the interface &lt;em&gt;IInitializableValidator&lt;/em&gt;&lt;em&gt;&amp;lt;A&amp;gt;&amp;nbsp;&lt;/em&gt;and the class should look like this:&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; PhoneValidator : IInitializableValidator&amp;lt;PhoneAttribute&amp;gt;
{
    &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; Regex regex;

    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; IsValid(&lt;span style="color:#0000ff;"&gt;object&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;)
    {
        &lt;span style="color:#0000ff;"&gt;if&lt;/span&gt; (&lt;span style="color:#0000ff;"&gt;value&lt;/span&gt; == &lt;span style="color:#0000ff;"&gt;null&lt;/span&gt;) &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;;
        &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; regex.IsMatch(&lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;.ToString());
    }

    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Initialize(PhoneAttribute parameters)
    {
        regex = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Regex(&lt;span style="color:#006080;"&gt;@&amp;quot;^[2-9]\d{2}-\d{3}-\d{4}$&amp;quot;&lt;/span&gt;);
    }
}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;As you can see, implementing the &lt;em&gt;IInitializableValidator&amp;lt;A&amp;gt;&lt;/em&gt;&amp;nbsp;interface we have to implement the method &lt;em&gt;Initialize(...)&lt;/em&gt; that initializes the validator, and it&amp;#39;s executed first of all.&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;IsValid()&lt;/em&gt;&amp;nbsp;method should return &lt;em&gt;false&lt;/em&gt;&amp;nbsp;if the constraint has been violated. For more examples, refer to the built-in validator implementations.&lt;/p&gt;
&lt;p&gt;We now have to implement the validator (ie. the rule checking implementation). A validation implementation can check the value of the a property (by implementing PropertyConstraint ) and/or can modify the NHibernate mapping metadata to express the constraint at the database level (by implementing PersistentClassConstraint)&lt;/p&gt;
&lt;p&gt;We have only seen property level validation, but you can write a class level validation attribute. Instead of receiving the return instance of a property, the bean itself will be passed to the validator. To activate the validation checking, just put the validation attribute to the class itself instead of in a property. A small sample can be found in the unit test suite.&lt;/p&gt;
&lt;p&gt;If your constraint can be applied multiple times (with different parameters) on the same property or type, you can use set the &lt;em&gt;AllowMultiple&lt;/em&gt;&amp;nbsp;parameter to &lt;em&gt;true&lt;/em&gt;&amp;nbsp;on the &lt;em&gt;AttributeUsage&lt;/em&gt;&amp;nbsp;attribute. An example of this it&amp;#39;s the built-in validator Pattern, have a look:&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;[Serializable]
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = &lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;)]
[ValidatorClass(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt; (PatternValidator))]
&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; PatternAttribute : Attribute, IRuleArgs
{
    &amp;hellip;
}&lt;/pre&gt;
&lt;/div&gt;
&lt;pre class="programlisting"&gt;&amp;nbsp;&lt;/pre&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 class="title" style="clear:both;"&gt;Configuring your domain model with .Net Attributes&lt;/h2&gt;
&lt;p&gt;Since you are already familiar with attributes now, the syntax should be very familiar&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; Address
{ 
    &lt;span style="color:#008000;"&gt;// a static non null field  &lt;/span&gt;
    [NotNull]
    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;static&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; blacklistedZipCode;
    
    &lt;span style="color:#008000;"&gt;// field a not null string of 20 characters maximum&lt;/span&gt;
    [Length(Max = 20), NotNull]    
    &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; country;
    &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;long&lt;/span&gt; id;
    &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; internalValid = &lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;;
    &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; line1;
    &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; line2;
    &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; state;
    &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; zip;
    
    &lt;span style="color:#008000;"&gt;// a numeric between 1 and 2000&lt;/span&gt;
    [Min(1), Range(Max = 2000)]
    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;long&lt;/span&gt; Id
    {
        get { &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; id; }
        set { id = &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;; }
    }

    &lt;span style="color:#008000;"&gt;// respond to the constraint in the field: country&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Country
    {
        get { &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; country; }
        set { country = &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;; }
    }

    &lt;span style="color:#008000;"&gt;// a non null string&lt;/span&gt;
    [NotNull]
    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Line1
    {
        get { &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; line1; }
        set { line1 = &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;; }
    }

    &lt;span style="color:#008000;"&gt;// a not null string of 3 characters maximum&lt;/span&gt;
    [Length(Max = 3), NotNull]
    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; State
    {
        get { &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; state; }
        set { state = &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;; }
    }

    &lt;span style="color:#008000;"&gt;// a not null numeric string of 5 characters maximum&lt;/span&gt;
    &lt;span style="color:#008000;"&gt;// if the string is longer, the message will&lt;/span&gt;
    &lt;span style="color:#008000;"&gt;// be searched in the resource bundle at key &amp;#39;long&amp;#39;&lt;/span&gt;
    [Length(Max = 5, Message = &lt;span style="color:#006080;"&gt;&amp;quot;{long}&amp;quot;&lt;/span&gt;)]
    [Pattern(Regex = &lt;span style="color:#006080;"&gt;&amp;quot;[0-9]+&amp;quot;&lt;/span&gt;)]
    [NotNull]
    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Zip
    {
        get { &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; zip; }
        set { zip = &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;; }
    }

    &lt;span style="color:#008000;"&gt;// no constraint&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Line2
    {
        get { &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; line2; }
        set { line2 = &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;; }
    }

    &lt;span style="color:#008000;"&gt;// should always be true&lt;/span&gt;
    [AssertTrue]
    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; InternalValid
    {
        get { &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; internalValid; }
        set { internalValid = &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;; }
    }
} &lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;While the example only shows public property validation, you can also annotate fields of any kind of visibility. In the example &lt;strong&gt;MyBeanConstraint&lt;/strong&gt; is a custom Class Validator, we will talk about this later.&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;[MyBeanConstraint(Max=45)]
&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; Dog 
{
    [AssertTrue] &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; IsMale;
    [NotNull] &lt;span style="color:#0000ff;"&gt;protected&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Name { get {...} set{...} }
    &amp;hellip;
}     &lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;You can also annotate interfaces. NHibernate Validator will check all superclasses and interfaces extended or implemented by a given type to read the appropriate validator attributes.&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;interface&lt;/span&gt; Named 
{
    [NotNull] &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Name {get; set;}
    ...
}

&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; Dog : Named 
{
    [AssertTrue] &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; isMale;
    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Name { get {...} set{...} }
}
&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;The name property will be checked for nullity when the Dog type is validated.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 class="title" style="clear:both;"&gt;Configuring your domain model with XML&lt;/h2&gt;
&lt;p&gt;You also can configurate your domain model with constraints using xml files: the nhv.xml files. We prefer to embed these Xml files into the assemblies, and NHibernate Validator is prepared to inspect them and retrieve the resources with constrains, like NHibernate does with hbm.xml files.&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#800000;"&gt;xml&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;version&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;1.0&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;encoding&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;utf-8&amp;quot;&lt;/span&gt; ?&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;nhv-mapping&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;urn:nhibernate-validator-1.0&amp;quot;&lt;/span&gt;
                        &lt;span style="color:#ff0000;"&gt;namespace&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;NHibernate.Validator.Tests.Base&amp;quot;&lt;/span&gt;
                        &lt;span style="color:#ff0000;"&gt;assembly&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;NHibernate.Validator.Tests&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;class&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Address&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;blacklistedZipCode&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
                  &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;not-null&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;country&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
           &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;length&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;max&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;20&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
           &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;not-null&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;    
    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;floor&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;    
          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;range&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;min&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;-2&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;max&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;50&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;message&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;{floor.out.of.range} (escaping #{el})&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Id&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;    
          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;min&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;value&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;1&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;range&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;max&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;2000&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;max&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;value&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;2500&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Line1&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;    
          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;not-null&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;State&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;    
          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;not-null&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;length&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;max&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;3&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;

    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Zip&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;length&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;max&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;5&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;message&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;{long}&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;pattern&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;regex&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;[0-9]+&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;not-null&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;InternalValid&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;    
          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;asserttrue&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;nhv-mapping&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;      &lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;span class="bold"&gt;&lt;strong&gt;We strongly recommend&lt;/strong&gt;&lt;/span&gt; to validate every nhv.xml file of your application with the respective XSD file. Every &lt;strong&gt;nhv.xml&lt;/strong&gt; file should be validated with the &lt;strong&gt;nhv-mapping.xsd&lt;/strong&gt;&amp;nbsp;file included within the NHibernate Validator distribution whether in source or binaries. Indeed, if you are using an IDE like Visual Studio it offers IntelliSense support in the &lt;strong&gt;nhv.xml&lt;/strong&gt; file if you add the schema to Visual Studio&amp;#39;s xml schemas folder.&lt;/p&gt;
&lt;p&gt;&lt;span class="bold"&gt;&lt;strong&gt;Xml beauties:&lt;/strong&gt;&lt;/span&gt; The nicest part of Xml for validation is that you can put xml validators in a different dll so you don&amp;#39;t need to recompile your domain when validation rules change. Only the dll with the Xml files..&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class="chapter"&gt;
&lt;h1&gt;Configuration&lt;/h1&gt;
&lt;p&gt;NHibernate Validator has global parameters that you can configure at the startup. This configuration can be done in differents ways: programmaticaly or using xml files. If you are involved with NHibernate this could be very familiar to you.&lt;/p&gt;
&lt;p&gt;Configuration properties:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Table Configuration properties&lt;/strong&gt;&lt;/p&gt;
&lt;div class="table"&gt;
&lt;span style="background: SpringGreen;"&gt;&amp;lt;tbody&amp;gt;&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;&amp;lt;/tbody&amp;gt;&lt;/span&gt;
&lt;table bgcolor="#ffffc8" cellspacing="1" cellpadding="2"&gt;
&amp;lt;tbody&amp;gt;
&lt;tr&gt;
&lt;th align="middle"&gt;Property name&lt;/th&gt;
&lt;th&gt;Purpose&lt;/th&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="middle"&gt;&lt;strong&gt;apply_to_ddl&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Configure NHibernate to apply the constraints to the DDL.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="middle"&gt;&lt;strong&gt;autoregister_listeners&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Register the Event-Listeners automatically on Inserts and Updates.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="middle"&gt;&lt;strong&gt;default_validator_mode&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Define what are the constraints sources are and how to proceed. There are some built-in configurations:
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;div class="itemizedlist"&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;UseAttribute&lt;/strong&gt; (default): Use Attributes to configurate the validators on types and/or members. Xml is ignored.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;UseXml&lt;/strong&gt;: Use Xml to configurate the validators on types and/or members. Attributes are ignored.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;OverrideAttributeWithXml&lt;/strong&gt;: If the same type/member is annotated with an Attribute and has a Xml rule the Attribute annotation will be used.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;OverrideXmlWithAttribute&lt;/strong&gt;: If the same type/member is annotated with an Attribute and has a Xml rule the Attribute annotation will be used.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="middle"&gt;&lt;strong&gt;assembly&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Which are the assemblies with contains the nhv.xml files embedded. You can have more than one assembly.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="middle"&gt;&lt;strong&gt;shared_engine_provider&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;To inject a custom Shared Engine Provider.&lt;/td&gt;
&lt;/tr&gt;
&amp;lt;/tbody&amp;gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 class="title" style="clear:both;"&gt;Programmatic configuration&lt;/h2&gt;
&lt;p&gt;You can use the following code to configure NHibernate.Validator programmatically:&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NHibernate.Validator.Cfg;
&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NHibernate.Validator.Engine;
&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; Environment=NHibernate.Validator.Cfg.Environment;
&amp;hellip;
ValidatorEngine ve = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ValidatorEngine();

NHVConfiguration nhvc = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; NHVConfiguration();
nhvc.Properties[Environment.ApplyToDDL] = &lt;span style="color:#006080;"&gt;&amp;quot;false&amp;quot;&lt;/span&gt;;
nhvc.Properties[Environment.AutoregisterListeners] = &lt;span style="color:#006080;"&gt;&amp;quot;false&amp;quot;&lt;/span&gt;;
nhvc.Properties[Environment.ValidatorMode] = GetMode();
nhvc.Mappings.Add(&lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; MappingConfiguration(&lt;span style="color:#006080;"&gt;&amp;quot;NHibernate.Validator.Demo.Model&amp;quot;&lt;/span&gt;,&lt;span style="color:#0000ff;"&gt;null&lt;/span&gt;));
ve.Configure(nhvc);  &lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;The &lt;em&gt;ValidatorEngine&lt;/em&gt;&amp;nbsp;will be set with the configuration. The NHibernate Validator configuration is represented by the &lt;em&gt;NHibernate.Validator.Cfg.NHVConfiguration&lt;/em&gt;&amp;nbsp;type. Other class used to configure all the stuff is the &lt;em&gt;NHibernate.Validator.Cfg.Environment&lt;/em&gt;&amp;nbsp;type, this class contain the string with the names of the properties of the configuration of NHibernate Validator.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 class="title" style="clear:both;"&gt;Declarative configuration: using Xml files&lt;/h2&gt;
&lt;p&gt;The declarative configuration keeps configuration out of the code. This means configuring the application using a different file, in this case a XML file. But the options don&amp;#39;t end here: you can choose to configure NHibernate Validator using a simple Xml file or you can put the configuration in the well known App.Config/Web.Config. &lt;/p&gt;
&lt;div class="section"&gt;
&lt;h3 class="title"&gt;Simple xml file&lt;/h3&gt;
&lt;p&gt;This is a sample of a simple configuration in a xml file:&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#800000;"&gt;xml&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;version&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;1.0&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;encoding&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;utf-8&amp;quot;&lt;/span&gt; ?&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;nhv-configuration&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;#39;urn:nhv-configuration-1.0&amp;#39;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
  &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;#39;apply_to_ddl&amp;#39;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;true&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
  &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;#39;autoregister_listeners&amp;#39;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;true&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
  &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;#39;default_validator_mode&amp;#39;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;UseXml&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
  &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;mapping&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;assembly&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;#39;NHibernate.Validator.Demo.Model&amp;#39;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;nhv-configuration&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;We encourage to name the configuration file &lt;strong&gt;nhvalidator.cfg.xml&lt;/strong&gt;, because NHibernate Validator will pick up this file from the application directory in order to get the configuration parameters, however you can name this file as you wish.&lt;/p&gt;
&lt;p&gt;In order to configurate NHibernate Validator with the &lt;strong&gt;nhvalidator.cfg.xml&lt;/strong&gt;&amp;nbsp;you can use the following codeו:&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;ValidatorEngine engine = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ValidatorEngine();
engine.Configure(); &lt;span style="color:#008000;"&gt;// by convention reads the nhvalidator.cfg.xml file.&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;You can choose another name for your configuration file and proceed of this manner: &lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;ValidatorEngine engine = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ValidatorEngine();
engine.Configure(&lt;span style="color:#006080;"&gt;&amp;quot;path/to/MyNHVConfigurationFile.xml&amp;quot;&lt;/span&gt;); &lt;span style="color:#008000;"&gt;// you can use whatever path to point to the configuration file&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h3 class="title"&gt;Configuration Application file: app.config/web.config&lt;/h3&gt;
&lt;p&gt;The configuration can be declared into our configuration application .Net files: app.config or web.config. In the below example you will see how to do it.&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#800000;"&gt;xml&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;version&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;1.0&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;encoding&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;utf-8&amp;quot;&lt;/span&gt; ?&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;configuration&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;configSections&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
        &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;section&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;nhv-configuration&amp;quot;&lt;/span&gt; 
                            &lt;span style="color:#ff0000;"&gt;type&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;NHibernate.Validator.Cfg.ConfigurationSectionHandler, NHibernate.Validator&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
        ...
    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;configSections&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    ...
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;nhv-configuration&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;#39;urn:nhv-configuration-1.0&amp;#39;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
        &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;shared_engine_provider&lt;/span&gt; 
         &lt;span style="color:#ff0000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;#39;NHibernate.Validator.Event.NHibernateSharedEngineProvider, NHibernate.Validator&amp;#39;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;nhv-configuration&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    ...
&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;configuration&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;      &lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;</description></item><item><title>NHibernate Validator 1.0.0</title><link>http://nhforge.org/wikis/validator/nhibernate-validator-1-0-0-documentation/revision/6.aspx</link><pubDate>Sat, 13 Sep 2008 06:04:41 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:111</guid><dc:creator>Fabio Maulo</dc:creator><description>Revision 6 posted to Validator by Fabio Maulo on 13/09/2008 03:04:41 a.m.&lt;br /&gt;
&lt;h2&gt;NHibernate Validator 1.0.0&lt;/h2&gt;
&lt;h1&gt;Preface&lt;/h1&gt;
&lt;div class="preface"&gt;
&lt;p&gt;Attributes are a very convenient and elegant way to specify invariant constraints for a domain model. You can, for example, express that a property should never be null, that the account balance should be strictly positive, etc. These domain model constraints are declared in the class itself by annotating its properties. A validator can then read them and check for constraint violations. The validation mechanism can be executed in different layers in your application without having to duplicate any of these rules (presentation layer, data access layer). Following the DRY principle, NHibernate Validator has been designed for that purpose. NHibernate Validator also lets you declare the same rules in XML files. Both approaches (attributes and XML) give us interesting possibilities for configuring our domain rules. &lt;/p&gt;
&lt;p&gt;NHibernate Validator works on two levels. First, it is able to check in-memory instances of a class for constraint violations. Second, it can apply the constraints to the NHibernate metamodel and incorporate them into the generated database schema.&lt;/p&gt;
&lt;p&gt;Each constraint attribute is associated to a validator implementation responsible for checking the constraint on the entity instance. A validator can also (optionally) apply the constraint to the NHibernate metamodel, allowing NHibernate to generate DDL that expresses the constraint. With the appropriate event listener, you can execute the checking operation on inserts and updates done by NHibernate. NHibernate Validator is not limited to only be used with NHibernate. You can easily use it anywhere in your application as well, indeed, you can use it at UI validation with Winforms or Asp.Net technologies.&lt;/p&gt;
&lt;p&gt;When checking instances at runtime, NHibernate Validator returns information about constraint violations in an array of &lt;em&gt;InvalidValue&lt;/em&gt;s. Among other information, the &lt;em&gt;InvalidValue&lt;/em&gt; contains an error description message that can embed the parameter values bundle with the annotation (eg. length limit), and message strings that may be externalized to a ResourceManager.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="chapter"&gt;
&lt;h1&gt;About the project&lt;/h1&gt;
&lt;p&gt;NHibernate Validator is a powerful and extensible framework to validate objects using the .Net Platform. Born as a port of &lt;span class="italic"&gt;Hibernate Validator 3.0.0&lt;/span&gt; project at december of 2007. Later on, many improvements and changes were made to the Core and API, and now we can say that NHibernate Validator is more than just a port. NHibernate Validator is entirely written in C#. The first release of NHibernate Validator was entirely developed by &lt;span class="italic"&gt;Dario Quintana&lt;/span&gt;, &lt;span class="italic"&gt;Gustavo Ringel&lt;/span&gt; and &lt;span class="italic"&gt;Fabio Maulo&lt;/span&gt;. We appreciate the help of &lt;span class="italic"&gt;Davy Brion&lt;/span&gt; in the revision of this document.&lt;/p&gt;
&lt;p&gt;NHibernate Validator is part of the &lt;a target="_top" href="http://sourceforge.net/projects/nhcontrib/"&gt;NHibernate Contrib&lt;/a&gt; project and you can download it &lt;a target="_top" href="http://sourceforge.net/project/showfiles.php?group_id=216446&amp;amp;package_id=275108&amp;amp;release_id=597674"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;You can ask questions about the development and utilization of NHibernate Validator on the following mailing lists:&lt;/p&gt;
&lt;p&gt;The development mailing list can be found &lt;a target="_top" href="http://groups.google.com/group/nhcdevs"&gt;here&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;The user forum can be found &lt;a target="_top" href="http://groups.google.com/group/nhusers"&gt;here&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;Bug and issues report should be sent to the development mailing list.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="chapter"&gt;
&lt;h1&gt;Using the Validator framework&lt;/h1&gt;
&lt;p&gt;NHibernate Validator is intended to be used to implement multi-layered data validation, where constraints are expressed in a single place and checked in various different layers of the application.&lt;/p&gt;
&lt;p&gt;This chapter will cover NHibernate Validator usage for different layers&lt;/p&gt;
&lt;div class="section"&gt;
&lt;h2 style="clear:both;" class="title"&gt;Database schema-level validation&lt;/h2&gt;
&lt;p&gt;Out of the box, NHibernate Validator will translate the constraints you have defined for your entities into mapping metadata. For example, if a property of your entity has the attribute [NotNull], its columns will be declared as &lt;em&gt;not null&lt;/em&gt; in the DDL schema generated by NHibernate.&lt;/p&gt;
&lt;p&gt;Using hbm2ddl, domain model constraints will be expressed into the database schema.&lt;/p&gt;
&lt;p&gt;If, for some reason, the feature needs to be disabled, set &lt;strong&gt;&lt;em&gt;apply_to_ddl&lt;/em&gt;&lt;/strong&gt; to false.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 style="clear:both;" class="title"&gt;ORM integration&lt;/h2&gt;
&lt;p&gt;NHibernate Validator integrates with NHibernate in a native way, and knows how to treat every persistent entity.&lt;/p&gt;
&lt;div class="section"&gt;
&lt;h3 class="title"&gt;NHibernate event-based validation&lt;/h3&gt;
&lt;p&gt;NHibernate Validator has two built-in NHibernate event listeners. Whenever a PreInsertEvent or PreUpdateEvent occurs, the listeners will verify all constraints of the entity instance and throw an exception if any of them are violated. Basically, objects will be checked before any insert and before any update triggered by NHibernate. This includes cascading changes! This is the most convenient and easiest way to activate the validation process. If a constraint is violated, the event will raise a runtime InvalidStateException which contains an array of &lt;em&gt;InvalidValue&lt;/em&gt;s describing each failure.&lt;/p&gt;
&lt;p&gt;If, for some reason, you want to disable this integration, set &lt;strong&gt;autoregister_listeners&lt;/strong&gt;&amp;nbsp;to false&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;span style="text-decoration:underline;"&gt;Note&lt;/span&gt;: If the entities are not annotated with validation attributes, there is no runtime performance cost.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;In case you wish to set manually the event listeners for NHibernate Core, use the following configuration in &lt;strong&gt;hibernate.cfg.xml&lt;/strong&gt;:&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    ...
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;event&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;type&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;pre-update&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
        &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;listener&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;NHibernate.Validator.Event.ValidatePreUpdateEventListener&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;event&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;event&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;type&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;pre-insert&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
        &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;listener&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;NHibernate.Validator.Event.ValidatePreInsertEventListener&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;event&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 style="clear:both;" class="title"&gt;Application-level validation&lt;/h2&gt;
&lt;p&gt;In order to interact with NHibernate Validator in your application you must use the main class: &lt;em&gt;ValidatorEngine&lt;/em&gt;. &lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;ValidatorEngine validator = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ValidatorEngine();
&lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; isValid = validator.IsValid(customer);&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;These two lines are enough to validate an object with its validation rules, which can be configured using Attributes or XML. The first line is the instantiation of the validator engine of NHibernate Validator. The second line is the validation of an object, and the result of the &lt;em&gt;IsValid(...)&lt;/em&gt;method indicates if the object is valid or not. In order to get the invalid values of an object, you can do:&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;ValidatorEngine validator = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ValidatorEngine();
InvalidValue[] validationMessages = validator.GetInvalidValues(customer);&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Now instead of only knowing if the object is valid or not, we get an array of InvalidValues. If this array length is greater than zero then there are validation errors and you can traverse the InvalidValue array to get verbose information about the problems that cause the object to be invalid. &lt;/p&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 style="clear:both;" class="title"&gt;Presentation layer validation&lt;/h2&gt;
&lt;p&gt;[&lt;strong&gt;&lt;span style="color:#008000;"&gt;TODO&lt;/span&gt;&lt;/strong&gt;]&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 style="clear:both;" class="title"&gt;Validation informations&lt;/h2&gt;
&lt;p&gt;NHibernate Validator provides an array of InvalidValue. Each &lt;em&gt;InvalidValue&lt;/em&gt;&amp;nbsp;has several methods describing the reasons that caused the entity to be invalid.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;BeanClass&lt;/strong&gt; retrieves the failing bean type&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Bean&amp;nbsp;&lt;/strong&gt;retrieves the failing instance (if any ie not when using &lt;em&gt;GetPotentialInvalidValues()&lt;/em&gt;)&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Value&lt;/strong&gt;&amp;nbsp;retrieves the failing value&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Message&lt;/strong&gt;&amp;nbsp;retrieves the proper internationalized error message&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;RootBean&lt;/strong&gt;&amp;nbsp;retrieves the root bean instance generating the issue (useful in conjunction with &lt;em&gt;[Valid]&lt;/em&gt;), is null if &lt;em&gt;GetPotentialInvalidValues(...)&lt;/em&gt; is used.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;PropertyPath&lt;/strong&gt;&amp;nbsp;retrieves the dotted path of the failing property starting from the root bean&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class="chapter"&gt;
&lt;h1&gt;Defining constraints&lt;/h1&gt;
&lt;div class="section"&gt;
&lt;h2 style="clear:both;" class="title"&gt;What is a constraint?&lt;/h2&gt;
&lt;p&gt;A constraint is a rule that a given element (field, property or type) has to comply to. The rule semantic is expressed by an Attribute or configured in Xml files. A constraint may have some properties to configure the associated validator, for example a Min constraint will have a value property setting the minimum acceptable value for the element to be valid. &lt;/p&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 style="clear:both;" class="title"&gt;Built in constraints&lt;/h2&gt;
&lt;p&gt;Hibernate Validator comes with some built-in constraints, which cover most of the basic data checks. As we&amp;#39;ll see later, you&amp;#39;re not limited to them, you can literally in a minute write your own constraints.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Table Built-in constraints&lt;/strong&gt;&lt;/p&gt;
&lt;table cellpadding="2" cellspacing="1" bgcolor="#ffffc8"&gt;
&amp;lt;tbody&amp;gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;strong&gt;Constraint&lt;/strong&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;strong&gt;Apply on&lt;/strong&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;strong&gt;Runtime checking&lt;/strong&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;strong&gt;NHibernate Metadata impact&lt;/strong&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;strong&gt;Length(Min=, Max=)&lt;/strong&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property (String)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;checks if the string length is between the min-max range&lt;/span&gt;&lt;/td&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;Column length will be set to max&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;strong&gt;Max(Value=)&lt;/strong&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property (numeric or string representation of a numeric)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;checks if the value is less than or equals to max&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;Add a check constraint on the column&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;strong&gt;Min(Value=)&lt;/strong&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property (numeric or string representation of a numeric)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;checks if the value is more than or equals to min&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;Add a check constraint on the column&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;strong&gt;NotNull&lt;/strong&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;checks if the value is not null&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;Column(s) are not null&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;strong&gt;NotEmpty&lt;/strong&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;checks if the string is not empty.&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;strong&gt;NotEmptyOrNull&lt;/strong&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;Checks if the string is not null or not empty.&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;strong&gt;Past&lt;/strong&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property (DateTime)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;check if the date is in the past&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;strong&gt;Future&lt;/strong&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property (DateTime)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;check if the date is in the future&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;strong&gt;Pattern(Regex=&amp;quot;regexp&amp;quot;, Flag=)&lt;/strong&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property (string)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;check if the property matches the regular expression given a match flag. The &lt;span style="font-family:&amp;#39;Trebuchet MS&amp;#39;;"&gt;&amp;lt;tt&amp;gt;Flag&amp;lt;/tt&amp;gt; parameter is a &lt;em&gt;System.Text.RegularExpressions.RegexOptions&lt;/em&gt;&amp;nbsp;enum. You can also declare multiple Pattern attributes on the same member.&lt;/span&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;strong&gt;Range(Min=, Max=)&lt;/strong&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property (numeric or string representation of a numeric)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;check if the value is between Min and Max (included)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;strong&gt;Size(Min=, Max=)&lt;/strong&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property (array, collection, map)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;check if the element size is between Min and Max (included)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;strong&gt;AssertFalse&lt;/strong&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;check that the method evaluates to false (useful for constraints expressed in code rather than attributes)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;strong&gt;AssertTrue&lt;/strong&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;check that the method evaluates to true (useful for constraints expressed in code rather than attributes)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;strong&gt;Valid&lt;/strong&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property (object)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;perform validation recursively on the associated object. If the object is a Collection or an array, the elements are validated recursively. If the object is a Map, the value elements are validated recursively.&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;strong&gt;Email&lt;/strong&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property (String)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;check whether the string conforms to the email address specification&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;strong&gt;Digits&lt;/strong&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property (numeric or string representation of a numeric)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;checks whether the property is a number having up to&amp;nbsp;&lt;span style="font-family:&amp;#39;Trebuchet MS&amp;#39;;"&gt;&lt;em&gt;integerDigits&lt;/em&gt; integer digits and &lt;em&gt;fractionalDigits&lt;/em&gt;&amp;nbsp;fractonal digits&lt;/span&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;define column precision and scale&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&amp;lt;/tbody&amp;gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 style="clear:both;" class="title"&gt;Error messages&lt;/h2&gt;
&lt;p&gt;NHibernate Validator comes with a default set of error messages translated in several languages (if yours is not present, please send us a patch). You can add your own additional set of messages while writing your validator Attributes or at XML. If NHibernate Validator cannot resolve a key from your ResourceManager nor from ValidatorMessage, it falls back to the default built-in values.&lt;/p&gt;
&lt;p&gt;Alternatively you can provide a ResourceManager while checking programmatically the validation rules on a bean or if you want a completly different interpolation mechanism, you can provide an implementation of NHibernate.Validator.Engine.IMessageInterpolator.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 style="clear:both;" class="title"&gt;Writing your own constraints&lt;/h2&gt;
&lt;p&gt;Extending the set of built-in constraints is extremely easy. Any constraint consists of two pieces: the constraint &lt;span class="emphasis"&gt;&lt;em&gt;descriptor&lt;/em&gt;&lt;/span&gt; (the attribute) and the constraint &lt;span class="emphasis"&gt;&lt;em&gt;validator&lt;/em&gt;&lt;/span&gt; (the implementation class). Here is a simple user-defined descriptor:&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
[ValidatorClass(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt;(PhoneValidator))]
&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; PhoneAttribute : Attribute, IRuleArgs
{
    &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; message = &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt;.Empty;
    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Message
    {
        get { &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; message; }
        set { message = &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;; }
    }
}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;To link a descriptor to its validator implementation, we use the &lt;em&gt;[ValidatorClass]&lt;/em&gt;&amp;nbsp;meta-attribute. Here you can see an implementation of a constraint validator:&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; PhoneValidator : IValidator
{
     &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; IsValid(&lt;span style="color:#0000ff;"&gt;object&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;)
     {
         Regex regex = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Regex(&lt;span style="color:#006080;"&gt;@&amp;quot;^[2-9]\d{2}-\d{3}-\d{4}$&amp;quot;&lt;/span&gt;);
         &lt;span style="color:#0000ff;"&gt;if&lt;/span&gt; (&lt;span style="color:#0000ff;"&gt;value&lt;/span&gt; == &lt;span style="color:#0000ff;"&gt;null&lt;/span&gt;) &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;;
         &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; regex.IsMatch(&lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;.ToString());
     }
}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;This PhoneValidator class is simple. The method &lt;em&gt;IsValid()&lt;/em&gt;&amp;nbsp;receives a string that should be validated with a Regex expression. The &lt;em&gt;IsValid()&lt;/em&gt;&amp;nbsp;method should return false if the constraint has been violated.&lt;/p&gt;
&lt;p&gt;If you need to initialize the class validator you can implement the interface &lt;em&gt;IInitializableValidator&lt;/em&gt;&lt;em&gt;&amp;lt;A&amp;gt;&amp;nbsp;&lt;/em&gt;and the class should look like this:&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; PhoneValidator : IInitializableValidator&amp;lt;PhoneAttribute&amp;gt;
{
    &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; Regex regex;

    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; IsValid(&lt;span style="color:#0000ff;"&gt;object&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;)
    {
        &lt;span style="color:#0000ff;"&gt;if&lt;/span&gt; (&lt;span style="color:#0000ff;"&gt;value&lt;/span&gt; == &lt;span style="color:#0000ff;"&gt;null&lt;/span&gt;) &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;;
        &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; regex.IsMatch(&lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;.ToString());
    }

    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Initialize(PhoneAttribute parameters)
    {
        regex = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Regex(&lt;span style="color:#006080;"&gt;@&amp;quot;^[2-9]\d{2}-\d{3}-\d{4}$&amp;quot;&lt;/span&gt;);
    }
}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;As you can see, implementing the &lt;em&gt;IInitializableValidator&amp;lt;A&amp;gt;&lt;/em&gt;&amp;nbsp;interface we have to implement the method &lt;em&gt;Initialize(...)&lt;/em&gt; that initializes the validator, and it&amp;#39;s executed first of all.&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;IsValid()&lt;/em&gt;&amp;nbsp;method should return &lt;em&gt;false&lt;/em&gt;&amp;nbsp;if the constraint has been violated. For more examples, refer to the built-in validator implementations.&lt;/p&gt;
&lt;p&gt;We now have to implement the validator (ie. the rule checking implementation). A validation implementation can check the value of the a property (by implementing PropertyConstraint ) and/or can modify the NHibernate mapping metadata to express the constraint at the database level (by implementing PersistentClassConstraint)&lt;/p&gt;
&lt;p&gt;We have only seen property level validation, but you can write a class level validation attribute. Instead of receiving the return instance of a property, the bean itself will be passed to the validator. To activate the validation checking, just put the validation attribute to the class itself instead of in a property. A small sample can be found in the unit test suite.&lt;/p&gt;
&lt;p&gt;If your constraint can be applied multiple times (with different parameters) on the same property or type, you can use set the &lt;em&gt;AllowMultiple&lt;/em&gt;&amp;nbsp;parameter to &lt;em&gt;true&lt;/em&gt;&amp;nbsp;on the &lt;em&gt;AttributeUsage&lt;/em&gt;&amp;nbsp;attribute. An example of this it&amp;#39;s the built-in validator Pattern, have a look:&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;[Serializable]
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = &lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;)]
[ValidatorClass(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt; (PatternValidator))]
&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; PatternAttribute : Attribute, IRuleArgs
{
    &amp;hellip;
}&lt;/pre&gt;
&lt;/div&gt;
&lt;pre class="programlisting"&gt;&amp;nbsp;&lt;/pre&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 style="clear:both;" class="title"&gt;Configuring your domain model with .Net Attributes&lt;/h2&gt;
&lt;p&gt;Since you are already familiar with attributes now, the syntax should be very familiar&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; Address
{ 
    &lt;span style="color:#008000;"&gt;// a static non null field  &lt;/span&gt;
    [NotNull]
    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;static&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; blacklistedZipCode;
    
    &lt;span style="color:#008000;"&gt;// field a not null string of 20 characters maximum&lt;/span&gt;
    [Length(Max = 20), NotNull]    
    &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; country;
    &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;long&lt;/span&gt; id;
    &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; internalValid = &lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;;
    &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; line1;
    &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; line2;
    &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; state;
    &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; zip;
    
    &lt;span style="color:#008000;"&gt;// a numeric between 1 and 2000&lt;/span&gt;
    [Min(1), Range(Max = 2000)]
    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;long&lt;/span&gt; Id
    {
        get { &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; id; }
        set { id = &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;; }
    }

    &lt;span style="color:#008000;"&gt;// respond to the constraint in the field: country&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Country
    {
        get { &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; country; }
        set { country = &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;; }
    }

    &lt;span style="color:#008000;"&gt;// a non null string&lt;/span&gt;
    [NotNull]
    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Line1
    {
        get { &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; line1; }
        set { line1 = &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;; }
    }

    &lt;span style="color:#008000;"&gt;// a not null string of 3 characters maximum&lt;/span&gt;
    [Length(Max = 3), NotNull]
    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; State
    {
        get { &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; state; }
        set { state = &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;; }
    }

    &lt;span style="color:#008000;"&gt;// a not null numeric string of 5 characters maximum&lt;/span&gt;
    &lt;span style="color:#008000;"&gt;// if the string is longer, the message will&lt;/span&gt;
    &lt;span style="color:#008000;"&gt;// be searched in the resource bundle at key &amp;#39;long&amp;#39;&lt;/span&gt;
    [Length(Max = 5, Message = &lt;span style="color:#006080;"&gt;&amp;quot;{long}&amp;quot;&lt;/span&gt;)]
    [Pattern(Regex = &lt;span style="color:#006080;"&gt;&amp;quot;[0-9]+&amp;quot;&lt;/span&gt;)]
    [NotNull]
    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Zip
    {
        get { &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; zip; }
        set { zip = &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;; }
    }

    &lt;span style="color:#008000;"&gt;// no constraint&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Line2
    {
        get { &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; line2; }
        set { line2 = &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;; }
    }

    &lt;span style="color:#008000;"&gt;// should always be true&lt;/span&gt;
    [AssertTrue]
    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; InternalValid
    {
        get { &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; internalValid; }
        set { internalValid = &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;; }
    }
} &lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;While the example only shows public property validation, you can also annotate fields of any kind of visibility. In the example &lt;strong&gt;MyBeanConstraint&lt;/strong&gt; is a custom Class Validator, we will talk about this later.&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;[MyBeanConstraint(Max=45)]
&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; Dog 
{
    [AssertTrue] &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; IsMale;
    [NotNull] &lt;span style="color:#0000ff;"&gt;protected&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Name { get {...} set{...} }
    &amp;hellip;
}     &lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;You can also annotate interfaces. NHibernate Validator will check all superclasses and interfaces extended or implemented by a given type to read the appropriate validator attributes.&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;interface&lt;/span&gt; Named 
{
    [NotNull] &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Name {get; set;}
    ...
}

&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; Dog : Named 
{
    [AssertTrue] &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; isMale;
    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Name { get {...} set{...} }
}
&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;The name property will be checked for nullity when the Dog type is validated.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 style="clear:both;" class="title"&gt;Configuring your domain model with XML&lt;/h2&gt;
&lt;p&gt;You also can configurate your domain model with constraints using xml files: the nhv.xml files. We prefer to embed these Xml files into the assemblies, and NHibernate Validator is prepared to inspect them and retrieve the resources with constrains, like NHibernate does with hbm.xml files.&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#800000;"&gt;xml&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;version&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;1.0&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;encoding&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;utf-8&amp;quot;&lt;/span&gt; ?&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;nhv-mapping&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;urn:nhibernate-validator-1.0&amp;quot;&lt;/span&gt;
                        &lt;span style="color:#ff0000;"&gt;namespace&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;NHibernate.Validator.Tests.Base&amp;quot;&lt;/span&gt;
                        &lt;span style="color:#ff0000;"&gt;assembly&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;NHibernate.Validator.Tests&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;class&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Address&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;blacklistedZipCode&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
                  &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;not-null&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;country&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
           &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;length&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;max&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;20&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
           &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;not-null&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;    
    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;floor&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;    
          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;range&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;min&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;-2&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;max&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;50&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;message&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;{floor.out.of.range} (escaping #{el})&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Id&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;    
          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;min&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;value&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;1&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;range&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;max&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;2000&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;max&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;value&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;2500&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Line1&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;    
          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;not-null&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;State&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;    
          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;not-null&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;length&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;max&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;3&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;

    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Zip&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;length&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;max&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;5&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;message&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;{long}&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;pattern&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;regex&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;[0-9]+&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;not-null&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;InternalValid&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;    
          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;asserttrue&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;nhv-mapping&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;      &lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;span class="bold"&gt;&lt;strong&gt;We strongly recommend&lt;/strong&gt;&lt;/span&gt; to validate every nhv.xml file of your application with the respective XSD file. Every &lt;strong&gt;nhv.xml&lt;/strong&gt; file should be validated with the &lt;strong&gt;nhv-mapping.xsd&lt;/strong&gt;&amp;nbsp;file included within the NHibernate Validator distribution whether in source or binaries. Indeed, if you are using an IDE like Visual Studio it offers IntelliSense support in the &lt;strong&gt;nhv.xml&lt;/strong&gt; file if you add the schema to Visual Studio&amp;#39;s xml schemas folder.&lt;/p&gt;
&lt;p&gt;&lt;span class="bold"&gt;&lt;strong&gt;Xml beauties:&lt;/strong&gt;&lt;/span&gt; The nicest part of Xml for validation is that you can put xml validators in a different dll so you don&amp;#39;t need to recompile your domain when validation rules change. Only the dll with the Xml files..&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class="chapter"&gt;
&lt;h1&gt;Configuration&lt;/h1&gt;
&lt;p&gt;NHibernate Validator has global parameters that you can configure at the startup. This configuration can be done in differents ways: programmaticaly or using xml files. If you are involved with NHibernate this could be very familiar to you.&lt;/p&gt;
&lt;p&gt;Configuration properties:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Table Configuration properties&lt;/strong&gt;&lt;/p&gt;
&lt;div class="table"&gt;
&lt;table cellpadding="2" cellspacing="1" bgcolor="#ffffc8"&gt;
&amp;lt;tbody&amp;gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;/tbody&amp;gt;&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;tbody&amp;gt;&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;/tbody&amp;gt;&lt;/span&gt;
&lt;table&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;tbody&amp;gt;&lt;/span&gt;
&lt;tr&gt;
&lt;th align="middle"&gt;Property name&lt;/th&gt;
&lt;th&gt;Purpose&lt;/th&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="middle"&gt;&lt;strong&gt;apply_to_ddl&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Configure NHibernate to apply the constraints to the DDL.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="middle"&gt;&lt;strong&gt;autoregister_listeners&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Register the Event-Listeners automatically on Inserts and Updates.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="middle"&gt;&lt;strong&gt;default_validator_mode&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Define what are the constraints sources are and how to proceed. There are some built-in configurations:
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;div class="itemizedlist"&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;UseAttribute&lt;/strong&gt; (default): Use Attributes to configurate the validators on types and/or members. Xml is ignored.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;UseXml&lt;/strong&gt;: Use Xml to configurate the validators on types and/or members. Attributes are ignored.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;OverrideAttributeWithXml&lt;/strong&gt;: If the same type/member is annotated with an Attribute and has a Xml rule the Attribute annotation will be used.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;OverrideXmlWithAttribute&lt;/strong&gt;: If the same type/member is annotated with an Attribute and has a Xml rule the Attribute annotation will be used.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="middle"&gt;&lt;strong&gt;assembly&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Which are the assemblies with contains the nhv.xml files embedded. You can have more than one assembly.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="middle"&gt;&lt;strong&gt;shared_engine_provider&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;To inject a custom Shared Engine Provider.&lt;/td&gt;
&lt;/tr&gt;
&amp;lt;/tbody&amp;gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 style="clear:both;" class="title"&gt;Programmatic configuration&lt;/h2&gt;
&lt;p&gt;You can use the following code to configure NHibernate.Validator programmatically:&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NHibernate.Validator.Cfg;
&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NHibernate.Validator.Engine;
&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; Environment=NHibernate.Validator.Cfg.Environment;
&amp;hellip;
ValidatorEngine ve = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ValidatorEngine();

NHVConfiguration nhvc = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; NHVConfiguration();
nhvc.Properties[Environment.ApplyToDDL] = &lt;span style="color:#006080;"&gt;&amp;quot;false&amp;quot;&lt;/span&gt;;
nhvc.Properties[Environment.AutoregisterListeners] = &lt;span style="color:#006080;"&gt;&amp;quot;false&amp;quot;&lt;/span&gt;;
nhvc.Properties[Environment.ValidatorMode] = GetMode();
nhvc.Mappings.Add(&lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; MappingConfiguration(&lt;span style="color:#006080;"&gt;&amp;quot;NHibernate.Validator.Demo.Model&amp;quot;&lt;/span&gt;,&lt;span style="color:#0000ff;"&gt;null&lt;/span&gt;));
ve.Configure(nhvc);  &lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;The &lt;em&gt;ValidatorEngine&lt;/em&gt;&amp;nbsp;will be set with the configuration. The NHibernate Validator configuration is represented by the &lt;em&gt;NHibernate.Validator.Cfg.NHVConfiguration&lt;/em&gt;&amp;nbsp;type. Other class used to configure all the stuff is the &lt;em&gt;NHibernate.Validator.Cfg.Environment&lt;/em&gt;&amp;nbsp;type, this class contain the string with the names of the properties of the configuration of NHibernate Validator.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 style="clear:both;" class="title"&gt;Declarative configuration: using Xml files&lt;/h2&gt;
&lt;p&gt;The declarative configuration keeps configuration out of the code. This means configuring the application using a different file, in this case a XML file. But the options don&amp;#39;t end here: you can choose to configure NHibernate Validator using a simple Xml file or you can put the configuration in the well known App.Config/Web.Config. &lt;/p&gt;
&lt;div class="section"&gt;
&lt;h3 class="title"&gt;Simple xml file&lt;/h3&gt;
&lt;p&gt;This is a sample of a simple configuration in a xml file:&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#800000;"&gt;xml&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;version&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;1.0&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;encoding&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;utf-8&amp;quot;&lt;/span&gt; ?&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;nhv-configuration&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;#39;urn:nhv-configuration-1.0&amp;#39;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
  &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;#39;apply_to_ddl&amp;#39;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;true&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
  &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;#39;autoregister_listeners&amp;#39;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;true&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
  &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;#39;default_validator_mode&amp;#39;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;UseXml&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
  &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;mapping&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;assembly&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;#39;NHibernate.Validator.Demo.Model&amp;#39;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;nhv-configuration&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;We encourage to name the configuration file &lt;strong&gt;nhvalidator.cfg.xml&lt;/strong&gt;, because NHibernate Validator will pick up this file from the application directory in order to get the configuration parameters, however you can name this file as you wish.&lt;/p&gt;
&lt;p&gt;In order to configurate NHibernate Validator with the &lt;strong&gt;nhvalidator.cfg.xml&lt;/strong&gt;&amp;nbsp;you can use the following codeו:&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;ValidatorEngine engine = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ValidatorEngine();
engine.Configure(); &lt;span style="color:#008000;"&gt;// by convention reads the nhvalidator.cfg.xml file.&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;You can choose another name for your configuration file and proceed of this manner: &lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;ValidatorEngine engine = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ValidatorEngine();
engine.Configure(&lt;span style="color:#006080;"&gt;&amp;quot;path/to/MyNHVConfigurationFile.xml&amp;quot;&lt;/span&gt;); &lt;span style="color:#008000;"&gt;// you can use whatever path to point to the configuration file&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h3 class="title"&gt;Configuration Application file: app.config/web.config&lt;/h3&gt;
&lt;p&gt;The configuration can be declared into our configuration application .Net files: app.config or web.config. In the below example you will see how to do it.&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#800000;"&gt;xml&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;version&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;1.0&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;encoding&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;utf-8&amp;quot;&lt;/span&gt; ?&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;configuration&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;configSections&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
        &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;section&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;nhv-configuration&amp;quot;&lt;/span&gt; 
                            &lt;span style="color:#ff0000;"&gt;type&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;NHibernate.Validator.Cfg.ConfigurationSectionHandler, NHibernate.Validator&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
        ...
    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;configSections&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    ...
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;nhv-configuration&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;#39;urn:nhv-configuration-1.0&amp;#39;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
        &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;shared_engine_provider&lt;/span&gt; 
         &lt;span style="color:#ff0000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;#39;NHibernate.Validator.Event.NHibernateSharedEngineProvider, NHibernate.Validator&amp;#39;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;nhv-configuration&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    ...
&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;configuration&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;      &lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;</description></item><item><title>NHibernate Validator 1.0.0</title><link>http://nhforge.org/wikis/validator/nhibernate-validator-1-0-0-documentation/revision/5.aspx</link><pubDate>Sat, 13 Sep 2008 06:02:47 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:106</guid><dc:creator>Fabio Maulo</dc:creator><description>Revision 5 posted to Validator by Fabio Maulo on 13/09/2008 03:02:47 a.m.&lt;br /&gt;
&lt;h2&gt;NHibernate Validator 1.0.0&lt;/h2&gt;
&lt;h1&gt;Preface&lt;/h1&gt;
&lt;div class="preface"&gt;
&lt;p&gt;Attributes are a very convenient and elegant way to specify invariant constraints for a domain model. You can, for example, express that a property should never be null, that the account balance should be strictly positive, etc. These domain model constraints are declared in the class itself by annotating its properties. A validator can then read them and check for constraint violations. The validation mechanism can be executed in different layers in your application without having to duplicate any of these rules (presentation layer, data access layer). Following the DRY principle, NHibernate Validator has been designed for that purpose. NHibernate Validator also lets you declare the same rules in XML files. Both approaches (attributes and XML) give us interesting possibilities for configuring our domain rules. &lt;/p&gt;
&lt;p&gt;NHibernate Validator works on two levels. First, it is able to check in-memory instances of a class for constraint violations. Second, it can apply the constraints to the NHibernate metamodel and incorporate them into the generated database schema.&lt;/p&gt;
&lt;p&gt;Each constraint attribute is associated to a validator implementation responsible for checking the constraint on the entity instance. A validator can also (optionally) apply the constraint to the NHibernate metamodel, allowing NHibernate to generate DDL that expresses the constraint. With the appropriate event listener, you can execute the checking operation on inserts and updates done by NHibernate. NHibernate Validator is not limited to only be used with NHibernate. You can easily use it anywhere in your application as well, indeed, you can use it at UI validation with Winforms or Asp.Net technologies.&lt;/p&gt;
&lt;p&gt;When checking instances at runtime, NHibernate Validator returns information about constraint violations in an array of &lt;em&gt;InvalidValue&lt;/em&gt;s. Among other information, the &lt;em&gt;InvalidValue&lt;/em&gt; contains an error description message that can embed the parameter values bundle with the annotation (eg. length limit), and message strings that may be externalized to a ResourceManager.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="chapter"&gt;
&lt;h1&gt;About the project&lt;/h1&gt;
&lt;p&gt;NHibernate Validator is a powerful and extensible framework to validate objects using the .Net Platform. Born as a port of &lt;span class="italic"&gt;Hibernate Validator 3.0.0&lt;/span&gt; project at december of 2007. Later on, many improvements and changes were made to the Core and API, and now we can say that NHibernate Validator is more than just a port. NHibernate Validator is entirely written in C#. The first release of NHibernate Validator was entirely developed by &lt;span class="italic"&gt;Dario Quintana&lt;/span&gt;, &lt;span class="italic"&gt;Gustavo Ringel&lt;/span&gt; and &lt;span class="italic"&gt;Fabio Maulo&lt;/span&gt;. We appreciate the help of &lt;span class="italic"&gt;Davy Brion&lt;/span&gt; in the revision of this document.&lt;/p&gt;
&lt;p&gt;NHibernate Validator is part of the &lt;a href="http://sourceforge.net/projects/nhcontrib/" target="_top"&gt;NHibernate Contrib&lt;/a&gt; project and you can download it &lt;a href="http://sourceforge.net/project/showfiles.php?group_id=216446&amp;amp;package_id=275108&amp;amp;release_id=597674" target="_top"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;You can ask questions about the development and utilization of NHibernate Validator on the following mailing lists:&lt;/p&gt;
&lt;p&gt;The development mailing list can be found &lt;a href="http://groups.google.com/group/nhcdevs" target="_top"&gt;here&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;The user forum can be found &lt;a href="http://groups.google.com/group/nhusers" target="_top"&gt;here&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;Bug and issues report should be sent to the development mailing list.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="chapter"&gt;
&lt;h1&gt;Using the Validator framework&lt;/h1&gt;
&lt;p&gt;NHibernate Validator is intended to be used to implement multi-layered data validation, where constraints are expressed in a single place and checked in various different layers of the application.&lt;/p&gt;
&lt;p&gt;This chapter will cover NHibernate Validator usage for different layers&lt;/p&gt;
&lt;div class="section"&gt;
&lt;h2 class="title" style="clear:both;"&gt;Database schema-level validation&lt;/h2&gt;
&lt;p&gt;Out of the box, NHibernate Validator will translate the constraints you have defined for your entities into mapping metadata. For example, if a property of your entity has the attribute [NotNull], its columns will be declared as &lt;em&gt;not null&lt;/em&gt; in the DDL schema generated by NHibernate.&lt;/p&gt;
&lt;p&gt;Using hbm2ddl, domain model constraints will be expressed into the database schema.&lt;/p&gt;
&lt;p&gt;If, for some reason, the feature needs to be disabled, set &lt;strong&gt;&lt;em&gt;apply_to_ddl&lt;/em&gt;&lt;/strong&gt; to false.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 class="title" style="clear:both;"&gt;ORM integration&lt;/h2&gt;
&lt;p&gt;NHibernate Validator integrates with NHibernate in a native way, and knows how to treat every persistent entity.&lt;/p&gt;
&lt;div class="section"&gt;
&lt;h3 class="title"&gt;NHibernate event-based validation&lt;/h3&gt;
&lt;p&gt;NHibernate Validator has two built-in NHibernate event listeners. Whenever a PreInsertEvent or PreUpdateEvent occurs, the listeners will verify all constraints of the entity instance and throw an exception if any of them are violated. Basically, objects will be checked before any insert and before any update triggered by NHibernate. This includes cascading changes! This is the most convenient and easiest way to activate the validation process. If a constraint is violated, the event will raise a runtime InvalidStateException which contains an array of &lt;em&gt;InvalidValue&lt;/em&gt;s describing each failure.&lt;/p&gt;
&lt;p&gt;If, for some reason, you want to disable this integration, set &lt;strong&gt;autoregister_listeners&lt;/strong&gt;&amp;nbsp;to false&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;span style="text-decoration:underline;"&gt;Note&lt;/span&gt;: If the entities are not annotated with validation attributes, there is no runtime performance cost.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;In case you wish to set manually the event listeners for NHibernate Core, use the following configuration in &lt;strong&gt;hibernate.cfg.xml&lt;/strong&gt;:&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    ...
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;event&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;type&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;pre-update&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
        &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;listener&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;NHibernate.Validator.Event.ValidatePreUpdateEventListener&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;event&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;event&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;type&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;pre-insert&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
        &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;listener&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;NHibernate.Validator.Event.ValidatePreInsertEventListener&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;event&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 class="title" style="clear:both;"&gt;Application-level validation&lt;/h2&gt;
&lt;p&gt;In order to interact with NHibernate Validator in your application you must use the main class: &lt;em&gt;ValidatorEngine&lt;/em&gt;. &lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;ValidatorEngine validator = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ValidatorEngine();
&lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; isValid = validator.IsValid(customer);&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;These two lines are enough to validate an object with its validation rules, which can be configured using Attributes or XML. The first line is the instantiation of the validator engine of NHibernate Validator. The second line is the validation of an object, and the result of the &lt;em&gt;IsValid(...)&lt;/em&gt;method indicates if the object is valid or not. In order to get the invalid values of an object, you can do:&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;ValidatorEngine validator = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ValidatorEngine();
InvalidValue[] validationMessages = validator.GetInvalidValues(customer);&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Now instead of only knowing if the object is valid or not, we get an array of InvalidValues. If this array length is greater than zero then there are validation errors and you can traverse the InvalidValue array to get verbose information about the problems that cause the object to be invalid. &lt;/p&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 class="title" style="clear:both;"&gt;Presentation layer validation&lt;/h2&gt;
&lt;p&gt;[&lt;strong&gt;&lt;span style="color:#008000;"&gt;TODO&lt;/span&gt;&lt;/strong&gt;]&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 class="title" style="clear:both;"&gt;Validation informations&lt;/h2&gt;
&lt;p&gt;NHibernate Validator provides an array of InvalidValue. Each &lt;em&gt;InvalidValue&lt;/em&gt;&amp;nbsp;has several methods describing the reasons that caused the entity to be invalid.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;BeanClass&lt;/strong&gt; retrieves the failing bean type&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Bean&amp;nbsp;&lt;/strong&gt;retrieves the failing instance (if any ie not when using &lt;em&gt;GetPotentialInvalidValues()&lt;/em&gt;)&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Value&lt;/strong&gt;&amp;nbsp;retrieves the failing value&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Message&lt;/strong&gt;&amp;nbsp;retrieves the proper internationalized error message&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;RootBean&lt;/strong&gt;&amp;nbsp;retrieves the root bean instance generating the issue (useful in conjunction with &lt;em&gt;[Valid]&lt;/em&gt;), is null if &lt;em&gt;GetPotentialInvalidValues(...)&lt;/em&gt; is used.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;PropertyPath&lt;/strong&gt;&amp;nbsp;retrieves the dotted path of the failing property starting from the root bean&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class="chapter"&gt;
&lt;h1&gt;Defining constraints&lt;/h1&gt;
&lt;div class="section"&gt;
&lt;h2 class="title" style="clear:both;"&gt;What is a constraint?&lt;/h2&gt;
&lt;p&gt;A constraint is a rule that a given element (field, property or type) has to comply to. The rule semantic is expressed by an Attribute or configured in Xml files. A constraint may have some properties to configure the associated validator, for example a Min constraint will have a value property setting the minimum acceptable value for the element to be valid. &lt;/p&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 class="title" style="clear:both;"&gt;Built in constraints&lt;/h2&gt;
&lt;p&gt;Hibernate Validator comes with some built-in constraints, which cover most of the basic data checks. As we&amp;#39;ll see later, you&amp;#39;re not limited to them, you can literally in a minute write your own constraints.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Table Built-in constraints&lt;/strong&gt;&lt;/p&gt;
&lt;table bgcolor="#ffffc8" cellspacing="1" cellpadding="2"&gt;
&amp;lt;tbody&amp;gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;/tbody&amp;gt;&lt;/span&gt;
&lt;table cellpadding="2" cellspacing="1" bgcolor="#ffffc8"&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;tbody&amp;gt;&lt;/span&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;strong&gt;Constraint&lt;/strong&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;strong&gt;Apply on&lt;/strong&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;strong&gt;Runtime checking&lt;/strong&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;strong&gt;NHibernate Metadata impact&lt;/strong&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;strong&gt;Length(Min=, Max=)&lt;/strong&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property (String)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;checks if the string length is between the min-max range&lt;/span&gt;&lt;/td&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;Column length will be set to max&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;strong&gt;Max(Value=)&lt;/strong&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property (numeric or string representation of a numeric)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;checks if the value is less than or equals to max&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;Add a check constraint on the column&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;strong&gt;Min(Value=)&lt;/strong&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property (numeric or string representation of a numeric)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;checks if the value is more than or equals to min&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;Add a check constraint on the column&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;strong&gt;NotNull&lt;/strong&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;checks if the value is not null&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;Column(s) are not null&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;strong&gt;NotEmpty&lt;/strong&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;checks if the string is not empty.&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;strong&gt;NotEmptyOrNull&lt;/strong&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;Checks if the string is not null or not empty.&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;strong&gt;Past&lt;/strong&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property (DateTime)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;check if the date is in the past&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;strong&gt;Future&lt;/strong&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property (DateTime)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;check if the date is in the future&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;strong&gt;Pattern(Regex=&amp;quot;regexp&amp;quot;, Flag=)&lt;/strong&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property (string)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;check if the property matches the regular expression given a match flag. The &lt;span style="font-family:&amp;#39;Trebuchet MS&amp;#39;;"&gt;&amp;lt;tt&amp;gt;Flag&amp;lt;/tt&amp;gt; parameter is a &lt;em&gt;System.Text.RegularExpressions.RegexOptions&lt;/em&gt;&amp;nbsp;enum. You can also declare multiple Pattern attributes on the same member.&lt;/span&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;strong&gt;Range(Min=, Max=)&lt;/strong&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property (numeric or string representation of a numeric)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;check if the value is between Min and Max (included)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;strong&gt;Size(Min=, Max=)&lt;/strong&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property (array, collection, map)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;check if the element size is between Min and Max (included)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;strong&gt;AssertFalse&lt;/strong&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;check that the method evaluates to false (useful for constraints expressed in code rather than attributes)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;strong&gt;AssertTrue&lt;/strong&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;check that the method evaluates to true (useful for constraints expressed in code rather than attributes)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;strong&gt;Valid&lt;/strong&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property (object)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;perform validation recursively on the associated object. If the object is a Collection or an array, the elements are validated recursively. If the object is a Map, the value elements are validated recursively.&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;strong&gt;Email&lt;/strong&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property (String)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;check whether the string conforms to the email address specification&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;strong&gt;Digits&lt;/strong&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property (numeric or string representation of a numeric)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;checks whether the property is a number having up to&amp;nbsp;&lt;span style="font-family:&amp;#39;Trebuchet MS&amp;#39;;"&gt;&lt;em&gt;integerDigits&lt;/em&gt; integer digits and &lt;em&gt;fractionalDigits&lt;/em&gt;&amp;nbsp;fractonal digits&lt;/span&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;define column precision and scale&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&amp;lt;/tbody&amp;gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 class="title" style="clear:both;"&gt;Error messages&lt;/h2&gt;
&lt;p&gt;NHibernate Validator comes with a default set of error messages translated in several languages (if yours is not present, please send us a patch). You can add your own additional set of messages while writing your validator Attributes or at XML. If NHibernate Validator cannot resolve a key from your ResourceManager nor from ValidatorMessage, it falls back to the default built-in values.&lt;/p&gt;
&lt;p&gt;Alternatively you can provide a &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;tt&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;class=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;classname&amp;quot;&amp;gt;ResourceManager&amp;lt;/tt&amp;gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;ResourceManager&lt;/span&gt; while checking programmatically the validation rules on a bean or if you want a completly different interpolation mechanism, you can provide an implementation of &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;tt&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;class=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;literal&amp;quot;&amp;gt;NHibernate.Validator.Engine.IMessageInterpolator&amp;lt;/tt&amp;gt;.&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;NHibernate.Validator.Engine.IMessageInterpolator&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 class="title" style="clear:both;"&gt;Writing your own constraints&lt;/h2&gt;
&lt;p&gt;Extending the set of built-in constraints is extremely easy. Any constraint consists of two pieces: the constraint &lt;span class="emphasis"&gt;&lt;em&gt;descriptor&lt;/em&gt;&lt;/span&gt; (the attribute) and the constraint &lt;span class="emphasis"&gt;&lt;em&gt;validator&lt;/em&gt;&lt;/span&gt; (the implementation class). Here is a simple user-defined descriptor:&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
[ValidatorClass(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt;(PhoneValidator))]
&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; PhoneAttribute : Attribute, IRuleArgs
{
    &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; message = &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt;.Empty;
    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Message
    {
        get { &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; message; }
        set { message = &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;; }
    }
}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;To link a descriptor to its validator implementation, we use the &lt;em&gt;[ValidatorClass]&lt;/em&gt;&amp;nbsp;meta-attribute. Here you can see an implementation of a constraint validator:&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; PhoneValidator : IValidator
{
     &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; IsValid(&lt;span style="color:#0000ff;"&gt;object&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;)
     {
         Regex regex = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Regex(&lt;span style="color:#006080;"&gt;@&amp;quot;^[2-9]\d{2}-\d{3}-\d{4}$&amp;quot;&lt;/span&gt;);
         &lt;span style="color:#0000ff;"&gt;if&lt;/span&gt; (&lt;span style="color:#0000ff;"&gt;value&lt;/span&gt; == &lt;span style="color:#0000ff;"&gt;null&lt;/span&gt;) &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;;
         &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; regex.IsMatch(&lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;.ToString());
     }
}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;This PhoneValidator class is simple. The method &lt;em&gt;IsValid()&lt;/em&gt;&amp;nbsp;receives a string that should be validated with a Regex expression. The &lt;em&gt;IsValid()&lt;/em&gt;&amp;nbsp;method should return false if the constraint has been violated.&lt;/p&gt;
&lt;p&gt;If you need to initialize the class validator you can implement the interface &lt;em&gt;IInitializableValidator&lt;/em&gt;&lt;em&gt;&amp;lt;A&amp;gt;&amp;nbsp;&lt;/em&gt;and the class should look like this:&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; PhoneValidator : IInitializableValidator&amp;lt;PhoneAttribute&amp;gt;
{
    &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; Regex regex;

    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; IsValid(&lt;span style="color:#0000ff;"&gt;object&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;)
    {
        &lt;span style="color:#0000ff;"&gt;if&lt;/span&gt; (&lt;span style="color:#0000ff;"&gt;value&lt;/span&gt; == &lt;span style="color:#0000ff;"&gt;null&lt;/span&gt;) &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;;
        &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; regex.IsMatch(&lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;.ToString());
    }

    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Initialize(PhoneAttribute parameters)
    {
        regex = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Regex(&lt;span style="color:#006080;"&gt;@&amp;quot;^[2-9]\d{2}-\d{3}-\d{4}$&amp;quot;&lt;/span&gt;);
    }
}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;As you can see, implementing the &lt;em&gt;IInitializableValidator&amp;lt;A&amp;gt;&lt;/em&gt;&amp;nbsp;interface we have to implement the method &lt;em&gt;Initialize(...)&lt;/em&gt; that initializes the validator, and it&amp;#39;s executed first of all.&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;IsValid()&lt;/em&gt;&amp;nbsp;method should return &lt;em&gt;false&lt;/em&gt;&amp;nbsp;if the constraint has been violated. For more examples, refer to the built-in validator implementations.&lt;/p&gt;
&lt;p&gt;We now have to implement the validator (ie. the rule checking implementation). A validation implementation can check the value of the a property (by implementing PropertyConstraint ) and/or can modify the NHibernate mapping metadata to express the constraint at the database level (by implementing PersistentClassConstraint)&lt;/p&gt;
&lt;p&gt;We have only seen property level validation, but you can write a class level validation attribute. Instead of receiving the return instance of a property, the bean itself will be passed to the validator. To activate the validation checking, just put the validation attribute to the class itself instead of in a property. A small sample can be found in the unit test suite.&lt;/p&gt;
&lt;p&gt;If your constraint can be applied multiple times (with different parameters) on the same property or type, you can use set the &lt;em&gt;AllowMultiple&lt;/em&gt;&amp;nbsp;parameter to &lt;em&gt;true&lt;/em&gt;&amp;nbsp;on the &lt;em&gt;AttributeUsage&lt;/em&gt;&amp;nbsp;attribute. An example of this it&amp;#39;s the built-in validator Pattern, have a look:&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;[Serializable]
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = &lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;)]
[ValidatorClass(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt; (PatternValidator))]
&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; PatternAttribute : Attribute, IRuleArgs
{
    &amp;hellip;
}&lt;/pre&gt;
&lt;/div&gt;
&lt;pre class="programlisting"&gt;&amp;nbsp;&lt;/pre&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 class="title" style="clear:both;"&gt;Configuring your domain model with .Net Attributes&lt;/h2&gt;
&lt;p&gt;Since you are already familiar with attributes now, the syntax should be very familiar&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; Address
{ 
    &lt;span style="color:#008000;"&gt;// a static non null field  &lt;/span&gt;
    [NotNull]
    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;static&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; blacklistedZipCode;
    
    &lt;span style="color:#008000;"&gt;// field a not null string of 20 characters maximum&lt;/span&gt;
    [Length(Max = 20), NotNull]    
    &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; country;
    &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;long&lt;/span&gt; id;
    &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; internalValid = &lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;;
    &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; line1;
    &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; line2;
    &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; state;
    &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; zip;
    
    &lt;span style="color:#008000;"&gt;// a numeric between 1 and 2000&lt;/span&gt;
    [Min(1), Range(Max = 2000)]
    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;long&lt;/span&gt; Id
    {
        get { &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; id; }
        set { id = &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;; }
    }

    &lt;span style="color:#008000;"&gt;// respond to the constraint in the field: country&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Country
    {
        get { &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; country; }
        set { country = &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;; }
    }

    &lt;span style="color:#008000;"&gt;// a non null string&lt;/span&gt;
    [NotNull]
    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Line1
    {
        get { &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; line1; }
        set { line1 = &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;; }
    }

    &lt;span style="color:#008000;"&gt;// a not null string of 3 characters maximum&lt;/span&gt;
    [Length(Max = 3), NotNull]
    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; State
    {
        get { &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; state; }
        set { state = &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;; }
    }

    &lt;span style="color:#008000;"&gt;// a not null numeric string of 5 characters maximum&lt;/span&gt;
    &lt;span style="color:#008000;"&gt;// if the string is longer, the message will&lt;/span&gt;
    &lt;span style="color:#008000;"&gt;// be searched in the resource bundle at key &amp;#39;long&amp;#39;&lt;/span&gt;
    [Length(Max = 5, Message = &lt;span style="color:#006080;"&gt;&amp;quot;{long}&amp;quot;&lt;/span&gt;)]
    [Pattern(Regex = &lt;span style="color:#006080;"&gt;&amp;quot;[0-9]+&amp;quot;&lt;/span&gt;)]
    [NotNull]
    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Zip
    {
        get { &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; zip; }
        set { zip = &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;; }
    }

    &lt;span style="color:#008000;"&gt;// no constraint&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Line2
    {
        get { &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; line2; }
        set { line2 = &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;; }
    }

    &lt;span style="color:#008000;"&gt;// should always be true&lt;/span&gt;
    [AssertTrue]
    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; InternalValid
    {
        get { &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; internalValid; }
        set { internalValid = &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;; }
    }
} &lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;While the example only shows public property validation, you can also annotate fields of any kind of visibility. In the example &lt;strong&gt;MyBeanConstraint&lt;/strong&gt; is a custom Class Validator, we will talk about this later.&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;[MyBeanConstraint(Max=45)]
&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; Dog 
{
    [AssertTrue] &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; IsMale;
    [NotNull] &lt;span style="color:#0000ff;"&gt;protected&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Name { get {...} set{...} }
    &amp;hellip;
}     &lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;You can also annotate interfaces. NHibernate Validator will check all superclasses and interfaces extended or implemented by a given type to read the appropriate validator attributes.&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;interface&lt;/span&gt; Named 
{
    [NotNull] &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Name {get; set;}
    ...
}

&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; Dog : Named 
{
    [AssertTrue] &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; isMale;
    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Name { get {...} set{...} }
}
&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;The name property will be checked for nullity when the Dog type is validated.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 class="title" style="clear:both;"&gt;Configuring your domain model with XML&lt;/h2&gt;
&lt;p&gt;You also can configurate your domain model with constraints using xml files: the nhv.xml files. We prefer to embed these Xml files into the assemblies, and NHibernate Validator is prepared to inspect them and retrieve the resources with constrains, like NHibernate does with hbm.xml files.&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#800000;"&gt;xml&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;version&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;1.0&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;encoding&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;utf-8&amp;quot;&lt;/span&gt; ?&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;nhv-mapping&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;urn:nhibernate-validator-1.0&amp;quot;&lt;/span&gt;
                        &lt;span style="color:#ff0000;"&gt;namespace&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;NHibernate.Validator.Tests.Base&amp;quot;&lt;/span&gt;
                        &lt;span style="color:#ff0000;"&gt;assembly&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;NHibernate.Validator.Tests&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;class&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Address&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;blacklistedZipCode&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
                  &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;not-null&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;country&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
           &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;length&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;max&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;20&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
           &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;not-null&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;    
    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;floor&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;    
          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;range&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;min&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;-2&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;max&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;50&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;message&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;{floor.out.of.range} (escaping #{el})&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Id&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;    
          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;min&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;value&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;1&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;range&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;max&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;2000&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;max&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;value&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;2500&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Line1&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;    
          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;not-null&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;State&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;    
          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;not-null&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;length&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;max&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;3&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;

    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Zip&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;length&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;max&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;5&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;message&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;{long}&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;pattern&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;regex&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;[0-9]+&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;not-null&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;InternalValid&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;    
          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;asserttrue&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;nhv-mapping&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;      &lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;span class="bold"&gt;&lt;strong&gt;We strongly recommend&lt;/strong&gt;&lt;/span&gt; to validate every nhv.xml file of your application with the respective XSD file. Every &lt;strong&gt;nhv.xml&lt;/strong&gt; file should be validated with the &lt;strong&gt;nhv-mapping.xsd&lt;/strong&gt;&amp;nbsp;file included within the NHibernate Validator distribution whether in source or binaries. Indeed, if you are using an IDE like Visual Studio it offers IntelliSense support in the &lt;strong&gt;nhv.xml&lt;/strong&gt; file if you add the schema to Visual Studio&amp;#39;s xml schemas folder.&lt;/p&gt;
&lt;p&gt;&lt;span class="bold"&gt;&lt;strong&gt;Xml beauties:&lt;/strong&gt;&lt;/span&gt; The nicest part of Xml for validation is that you can put xml validators in a different dll so you don&amp;#39;t need to recompile your domain when validation rules change. Only the dll with the Xml files..&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class="chapter"&gt;
&lt;h1&gt;Configuration&lt;/h1&gt;
&lt;p&gt;NHibernate Validator has global parameters that you can configure at the startup. This configuration can be done in differents ways: programmaticaly or using xml files. If you are involved with NHibernate this could be very familiar to you.&lt;/p&gt;
&lt;p&gt;Configuration properties:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Table Configuration properties&lt;/strong&gt;&lt;/p&gt;
&lt;div class="table"&gt;
&lt;span style="background: SpringGreen;"&gt;&amp;lt;tbody&amp;gt;&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;&amp;lt;/tbody&amp;gt;&lt;/span&gt;
&amp;lt;tbody&amp;gt;
&amp;lt;/tbody&amp;gt;
&lt;table&gt;
&amp;lt;tbody&amp;gt;
&lt;tr&gt;
&lt;th align="middle"&gt;Property name&lt;/th&gt;
&lt;th&gt;Purpose&lt;/th&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="middle"&gt;&lt;strong&gt;apply_to_ddl&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Configure NHibernate to apply the constraints to the DDL.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="middle"&gt;&lt;strong&gt;autoregister_listeners&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Register the Event-Listeners automatically on Inserts and Updates.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="middle"&gt;&lt;strong&gt;default_validator_mode&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Define what are the constraints sources are and how to proceed. There are some built-in configurations:
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;div class="itemizedlist"&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;UseAttribute&lt;/strong&gt; (default): Use Attributes to configurate the validators on types and/or members. Xml is ignored.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;UseXml&lt;/strong&gt;: Use Xml to configurate the validators on types and/or members. Attributes are ignored.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;OverrideAttributeWithXml&lt;/strong&gt;: If the same type/member is annotated with an Attribute and has a Xml rule the Attribute annotation will be used.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;OverrideXmlWithAttribute&lt;/strong&gt;: If the same type/member is annotated with an Attribute and has a Xml rule the Attribute annotation will be used.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="middle"&gt;&lt;strong&gt;assembly&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Which are the assemblies with contains the nhv.xml files embedded. You can have more than one assembly.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="middle"&gt;&lt;strong&gt;shared_engine_provider&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;To inject a custom Shared Engine Provider.&lt;/td&gt;
&lt;/tr&gt;
&amp;lt;/tbody&amp;gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 class="title" style="clear:both;"&gt;Programmatic configuration&lt;/h2&gt;
&lt;p&gt;You can use the following code to configure NHibernate.Validator programmatically:&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NHibernate.Validator.Cfg;
&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NHibernate.Validator.Engine;
&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; Environment=NHibernate.Validator.Cfg.Environment;
&amp;hellip;
ValidatorEngine ve = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ValidatorEngine();

NHVConfiguration nhvc = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; NHVConfiguration();
nhvc.Properties[Environment.ApplyToDDL] = &lt;span style="color:#006080;"&gt;&amp;quot;false&amp;quot;&lt;/span&gt;;
nhvc.Properties[Environment.AutoregisterListeners] = &lt;span style="color:#006080;"&gt;&amp;quot;false&amp;quot;&lt;/span&gt;;
nhvc.Properties[Environment.ValidatorMode] = GetMode();
nhvc.Mappings.Add(&lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; MappingConfiguration(&lt;span style="color:#006080;"&gt;&amp;quot;NHibernate.Validator.Demo.Model&amp;quot;&lt;/span&gt;,&lt;span style="color:#0000ff;"&gt;null&lt;/span&gt;));
ve.Configure(nhvc);  &lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;The &lt;em&gt;ValidatorEngine&lt;/em&gt;&amp;nbsp;will be set with the configuration. The NHibernate Validator configuration is represented by the &lt;em&gt;NHibernate.Validator.Cfg.NHVConfiguration&lt;/em&gt;&amp;nbsp;type. Other class used to configure all the stuff is the &lt;em&gt;NHibernate.Validator.Cfg.Environment&lt;/em&gt;&amp;nbsp;type, this class contain the string with the names of the properties of the configuration of NHibernate Validator.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 class="title" style="clear:both;"&gt;Declarative configuration: using Xml files&lt;/h2&gt;
&lt;p&gt;The declarative configuration keeps configuration out of the code. This means configuring the application using a different file, in this case a XML file. But the options don&amp;#39;t end here: you can choose to configure NHibernate Validator using a simple Xml file or you can put the configuration in the well known App.Config/Web.Config. &lt;/p&gt;
&lt;div class="section"&gt;
&lt;h3 class="title"&gt;Simple xml file&lt;/h3&gt;
&lt;p&gt;This is a sample of a simple configuration in a xml file:&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#800000;"&gt;xml&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;version&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;1.0&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;encoding&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;utf-8&amp;quot;&lt;/span&gt; ?&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;nhv-configuration&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;#39;urn:nhv-configuration-1.0&amp;#39;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
  &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;#39;apply_to_ddl&amp;#39;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;true&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
  &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;#39;autoregister_listeners&amp;#39;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;true&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
  &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;#39;default_validator_mode&amp;#39;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;UseXml&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
  &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;mapping&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;assembly&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;#39;NHibernate.Validator.Demo.Model&amp;#39;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;nhv-configuration&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;We encourage to name the configuration file &lt;strong&gt;nhvalidator.cfg.xml&lt;/strong&gt;, because NHibernate Validator will pick up this file from the application directory in order to get the configuration parameters, however you can name this file as you wish.&lt;/p&gt;
&lt;p&gt;In order to configurate NHibernate Validator with the &lt;strong&gt;nhvalidator.cfg.xml&lt;/strong&gt;&amp;nbsp;you can use the following codeו:&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;ValidatorEngine engine = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ValidatorEngine();
engine.Configure(); &lt;span style="color:#008000;"&gt;// by convention reads the nhvalidator.cfg.xml file.&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;You can choose another name for your configuration file and proceed of this manner: &lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;ValidatorEngine engine = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ValidatorEngine();
engine.Configure(&lt;span style="color:#006080;"&gt;&amp;quot;path/to/MyNHVConfigurationFile.xml&amp;quot;&lt;/span&gt;); &lt;span style="color:#008000;"&gt;// you can use whatever path to point to the configuration file&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h3 class="title"&gt;Configuration Application file: app.config/web.config&lt;/h3&gt;
&lt;p&gt;The configuration can be declared into our configuration application .Net files: app.config or web.config. In the below example you will see how to do it.&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#800000;"&gt;xml&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;version&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;1.0&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;encoding&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;utf-8&amp;quot;&lt;/span&gt; ?&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;configuration&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;configSections&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
        &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;section&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;nhv-configuration&amp;quot;&lt;/span&gt; 
                            &lt;span style="color:#ff0000;"&gt;type&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;NHibernate.Validator.Cfg.ConfigurationSectionHandler, NHibernate.Validator&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
        ...
    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;configSections&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    ...
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;nhv-configuration&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;#39;urn:nhv-configuration-1.0&amp;#39;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
        &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;shared_engine_provider&lt;/span&gt; 
         &lt;span style="color:#ff0000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;#39;NHibernate.Validator.Event.NHibernateSharedEngineProvider, NHibernate.Validator&amp;#39;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;nhv-configuration&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    ...
&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;configuration&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;      &lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;</description></item><item><title>NHibernate Validator 1.0.0</title><link>http://nhforge.org/wikis/validator/nhibernate-validator-1-0-0-documentation/revision/4.aspx</link><pubDate>Sat, 13 Sep 2008 05:59:58 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:105</guid><dc:creator>Fabio Maulo</dc:creator><description>Revision 4 posted to Validator by Fabio Maulo on 13/09/2008 02:59:58 a.m.&lt;br /&gt;
&lt;h2&gt;NHibernate Validator 1.0.0&lt;/h2&gt;
&lt;h1&gt;Preface&lt;/h1&gt;
&lt;div class="preface"&gt;
&lt;p&gt;Attributes are a very convenient and elegant way to specify invariant constraints for a domain model. You can, for example, express that a property should never be null, that the account balance should be strictly positive, etc. These domain model constraints are declared in the class itself by annotating its properties. A validator can then read them and check for constraint violations. The validation mechanism can be executed in different layers in your application without having to duplicate any of these rules (presentation layer, data access layer). Following the DRY principle, NHibernate Validator has been designed for that purpose. NHibernate Validator also lets you declare the same rules in XML files. Both approaches (attributes and XML) give us interesting possibilities for configuring our domain rules. &lt;/p&gt;
&lt;p&gt;NHibernate Validator works on two levels. First, it is able to check in-memory instances of a class for constraint violations. Second, it can apply the constraints to the NHibernate metamodel and incorporate them into the generated database schema.&lt;/p&gt;
&lt;p&gt;Each constraint attribute is associated to a validator implementation responsible for checking the constraint on the entity instance. A validator can also (optionally) apply the constraint to the NHibernate metamodel, allowing NHibernate to generate DDL that expresses the constraint. With the appropriate event listener, you can execute the checking operation on inserts and updates done by NHibernate. NHibernate Validator is not limited to only be used with NHibernate. You can easily use it anywhere in your application as well, indeed, you can use it at UI validation with Winforms or Asp.Net technologies.&lt;/p&gt;
&lt;p&gt;When checking instances at runtime, NHibernate Validator returns information about constraint violations in an array of &lt;em&gt;InvalidValue&lt;/em&gt;s. Among other information, the &lt;em&gt;InvalidValue&lt;/em&gt; contains an error description message that can embed the parameter values bundle with the annotation (eg. length limit), and message strings that may be externalized to a ResourceManager.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="chapter"&gt;
&lt;h1&gt;About the project&lt;/h1&gt;
&lt;p&gt;NHibernate Validator is a powerful and extensible framework to validate objects using the .Net Platform. Born as a port of &lt;span class="italic"&gt;Hibernate Validator 3.0.0&lt;/span&gt; project at december of 2007. Later on, many improvements and changes were made to the Core and API, and now we can say that NHibernate Validator is more than just a port. NHibernate Validator is entirely written in C#. The first release of NHibernate Validator was entirely developed by &lt;span class="italic"&gt;Dario Quintana&lt;/span&gt;, &lt;span class="italic"&gt;Gustavo Ringel&lt;/span&gt; and &lt;span class="italic"&gt;Fabio Maulo&lt;/span&gt;. We appreciate the help of &lt;span class="italic"&gt;Davy Brion&lt;/span&gt; in the revision of this document.&lt;/p&gt;
&lt;p&gt;NHibernate Validator is part of the &lt;a target="_top" href="http://sourceforge.net/projects/nhcontrib/"&gt;NHibernate Contrib&lt;/a&gt; project and you can download it &lt;a target="_top" href="http://sourceforge.net/project/showfiles.php?group_id=216446&amp;amp;package_id=275108&amp;amp;release_id=597674"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;You can ask questions about the development and utilization of NHibernate Validator on the following mailing lists:&lt;/p&gt;
&lt;p&gt;The development mailing list can be found &lt;a target="_top" href="http://groups.google.com/group/nhcdevs"&gt;here&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;The user forum can be found &lt;a target="_top" href="http://groups.google.com/group/nhusers"&gt;here&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;Bug and issues report should be sent to the development mailing list.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="chapter"&gt;
&lt;h1&gt;Using the Validator framework&lt;/h1&gt;
&lt;p&gt;NHibernate Validator is intended to be used to implement multi-layered data validation, where constraints are expressed in a single place and checked in various different layers of the application.&lt;/p&gt;
&lt;p&gt;This chapter will cover NHibernate Validator usage for different layers&lt;/p&gt;
&lt;div class="section"&gt;
&lt;h2 style="clear:both;" class="title"&gt;Database schema-level validation&lt;/h2&gt;
&lt;p&gt;Out of the box, NHibernate Validator will translate the constraints you have defined for your entities into mapping metadata. For example, if a property of your entity has the attribute [NotNull], its columns will be declared as &lt;em&gt;not null&lt;/em&gt; in the DDL schema generated by NHibernate.&lt;/p&gt;
&lt;p&gt;Using hbm2ddl, domain model constraints will be expressed into the database schema.&lt;/p&gt;
&lt;p&gt;If, for some reason, the feature needs to be disabled, set &lt;strong&gt;&lt;em&gt;apply_to_ddl&lt;/em&gt;&lt;/strong&gt; to false.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 style="clear:both;" class="title"&gt;ORM integration&lt;/h2&gt;
&lt;p&gt;NHibernate Validator integrates with NHibernate in a native way, and knows how to treat every persistent entity.&lt;/p&gt;
&lt;div class="section"&gt;
&lt;h3 class="title"&gt;NHibernate event-based validation&lt;/h3&gt;
&lt;p&gt;NHibernate Validator has two built-in NHibernate event listeners. Whenever a PreInsertEvent or PreUpdateEvent occurs, the listeners will verify all constraints of the entity instance and throw an exception if any of them are violated. Basically, objects will be checked before any insert and before any update triggered by NHibernate. This includes cascading changes! This is the most convenient and easiest way to activate the validation process. If a constraint is violated, the event will raise a runtime InvalidStateException which contains an array of &lt;em&gt;InvalidValue&lt;/em&gt;s describing each failure.&lt;/p&gt;
&lt;p&gt;If, for some reason, you want to disable this integration, set &lt;strong&gt;autoregister_listeners&lt;/strong&gt;&amp;nbsp;to false&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;span style="text-decoration:underline;"&gt;Note&lt;/span&gt;: If the entities are not annotated with validation attributes, there is no runtime performance cost.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;In case you wish to set manually the event listeners for NHibernate Core, use the following configuration in &lt;strong&gt;hibernate.cfg.xml&lt;/strong&gt;:&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    ...
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;event&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;type&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;pre-update&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
        &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;listener&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;NHibernate.Validator.Event.ValidatePreUpdateEventListener&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;event&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;event&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;type&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;pre-insert&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
        &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;listener&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;NHibernate.Validator.Event.ValidatePreInsertEventListener&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;event&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 style="clear:both;" class="title"&gt;Application-level validation&lt;/h2&gt;
&lt;p&gt;In order to interact with NHibernate Validator in your application you must use the main class: &lt;em&gt;ValidatorEngine&lt;/em&gt;. &lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;ValidatorEngine validator = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ValidatorEngine();
&lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; isValid = validator.IsValid(customer);&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;These two lines are enough to validate an object with its validation rules, which can be configured using Attributes or XML. The first line is the instantiation of the validator engine of NHibernate Validator. The second line is the validation of an object, and the result of the &lt;em&gt;IsValid(...)&lt;/em&gt;method indicates if the object is valid or not. In order to get the invalid values of an object, you can do:&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;ValidatorEngine validator = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ValidatorEngine();
InvalidValue[] validationMessages = validator.GetInvalidValues(customer);&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Now instead of only knowing if the object is valid or not, we get an array of InvalidValues. If this array length is greater than zero then there are validation errors and you can traverse the InvalidValue array to get verbose information about the problems that cause the object to be invalid. &lt;/p&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 style="clear:both;" class="title"&gt;Presentation layer validation&lt;/h2&gt;
&lt;p&gt;[&lt;strong&gt;&lt;span style="color:#008000;"&gt;TODO&lt;/span&gt;&lt;/strong&gt;]&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 style="clear:both;" class="title"&gt;Validation informations&lt;/h2&gt;
&lt;p&gt;NHibernate Validator provides an array of InvalidValue. Each &lt;em&gt;InvalidValue&lt;/em&gt;&amp;nbsp;has several methods describing the reasons that caused the entity to be invalid.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;BeanClass&lt;/strong&gt; retrieves the failing bean type&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Bean&amp;nbsp;&lt;/strong&gt;retrieves the failing instance (if any ie not when using &lt;em&gt;GetPotentialInvalidValues()&lt;/em&gt;)&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Value&lt;/strong&gt;&amp;nbsp;retrieves the failing value&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Message&lt;/strong&gt;&amp;nbsp;retrieves the proper internationalized error message&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;RootBean&lt;/strong&gt;&amp;nbsp;retrieves the root bean instance generating the issue (useful in conjunction with &lt;em&gt;[Valid]&lt;/em&gt;), is null if &lt;em&gt;GetPotentialInvalidValues(...)&lt;/em&gt; is used.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;PropertyPath&lt;/strong&gt;&amp;nbsp;retrieves the dotted path of the failing property starting from the root bean&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class="chapter"&gt;
&lt;h1&gt;Defining constraints&lt;/h1&gt;
&lt;div class="section"&gt;
&lt;h2 style="clear:both;" class="title"&gt;What is a constraint?&lt;/h2&gt;
&lt;p&gt;A constraint is a rule that a given element (field, property or type) has to comply to. The rule semantic is expressed by an Attribute or configured in Xml files. A constraint may have some properties to configure the associated validator, for example a Min constraint will have a value property setting the minimum acceptable value for the element to be valid. &lt;/p&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 style="clear:both;" class="title"&gt;Built in constraints&lt;/h2&gt;
&lt;p&gt;Hibernate Validator comes with some built-in constraints, which cover most of the basic data checks. As we&amp;#39;ll see later, you&amp;#39;re not limited to them, you can literally in a minute write your own constraints.&lt;/p&gt;
&lt;div class="table"&gt;
&lt;p&gt;&lt;strong&gt;Table Built-in constraints&lt;/strong&gt;&lt;/p&gt;
&amp;lt;tbody&amp;gt;
&lt;span style="background: SpringGreen;"&gt;&amp;lt;/tbody&amp;gt;&lt;/span&gt;
&lt;table cellpadding="2" cellspacing="1" bgcolor="#ffffc8"&gt;
&lt;span style="background: SpringGreen;"&gt;&amp;lt;tbody&amp;gt;&lt;/span&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;strong&gt;Constraint&lt;/strong&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;strong&gt;Apply on&lt;/strong&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;strong&gt;Runtime checking&lt;/strong&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;strong&gt;NHibernate Metadata impact&lt;/strong&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;strong&gt;Length(Min=, Max=)&lt;/strong&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property (String)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;checks if the string length is between the min-max range&lt;/span&gt;&lt;/td&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;Column length will be set to max&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;strong&gt;Max(Value=)&lt;/strong&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property (numeric or string representation of a numeric)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;checks if the value is less than or equals to max&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;Add a check constraint on the column&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;strong&gt;Min(Value=)&lt;/strong&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property (numeric or string representation of a numeric)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;checks if the value is more than or equals to min&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;Add a check constraint on the column&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;strong&gt;NotNull&lt;/strong&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;checks if the value is not null&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;Column(s) are not null&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;strong&gt;NotEmpty&lt;/strong&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;checks if the string is not empty.&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;strong&gt;NotEmptyOrNull&lt;/strong&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;Checks if the string is not null or not empty.&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;strong&gt;Past&lt;/strong&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property (DateTime)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;check if the date is in the past&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;strong&gt;Future&lt;/strong&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property (DateTime)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;check if the date is in the future&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;strong&gt;Pattern(Regex=&amp;quot;regexp&amp;quot;, Flag=)&lt;/strong&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property (string)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;check if the property matches the regular expression given a match flag. The &lt;span style="font-family:&amp;#39;Trebuchet MS&amp;#39;;"&gt;&amp;lt;tt&amp;gt;Flag&amp;lt;/tt&amp;gt; parameter is a &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;tt&amp;gt;System.Text.RegularExpressions.RegexOptions&amp;lt;/tt&amp;gt;&lt;/span&gt; &lt;em&gt;&lt;span style="background: SpringGreen;"&gt;System.Text.RegularExpressions.RegexOptions&lt;/span&gt;&lt;/em&gt;&amp;nbsp;enum. You can also declare multiple Pattern attributes on the same member.&lt;/span&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;strong&gt;Range(Min=, Max=)&lt;/strong&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property (numeric or string representation of a numeric)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;check if the value is between Min and Max (included)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;strong&gt;Size(Min=, Max=)&lt;/strong&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property (array, collection, map)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;check if the element size is between Min and Max (included)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;strong&gt;AssertFalse&lt;/strong&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;check that the method evaluates to false (useful for constraints expressed in code rather than attributes)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;strong&gt;AssertTrue&lt;/strong&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;check that the method evaluates to true (useful for constraints expressed in code rather than attributes)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;strong&gt;Valid&lt;/strong&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property (object)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;perform validation recursively on the associated object. If the object is a Collection or an array, the elements are validated recursively. If the object is a Map, the value elements are validated recursively.&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;strong&gt;Email&lt;/strong&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property (String)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;check whether the string conforms to the email address specification&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;strong&gt;Digits&lt;/strong&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property (numeric or string representation of a numeric)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;checks whether the property is a number having up to&lt;span style="font-family:&amp;#39;Trebuchet MS&amp;#39;;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;tt&amp;gt;integerDigits&amp;lt;/tt&amp;gt;&lt;/span&gt; &amp;nbsp;&lt;span style="font-family:&amp;#39;Trebuchet MS&amp;#39;;"&gt;&lt;em&gt;&lt;span style="background: SpringGreen;"&gt;integerDigits&lt;/span&gt;&lt;/em&gt; integer digits and &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;tt&amp;gt;fractionalDigits&amp;lt;/tt&amp;gt;&lt;/span&gt; &lt;em&gt;&lt;span style="background: SpringGreen;"&gt;fractionalDigits&lt;/span&gt;&lt;/em&gt;&amp;nbsp;fractonal digits&lt;/span&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;define column precision and scale&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&amp;lt;/tbody&amp;gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 style="clear:both;" class="title"&gt;Error messages&lt;/h2&gt;
&lt;p&gt;NHibernate Validator comes with a default set of error messages translated in several languages (if yours is not present, please send us a patch). You can add your own additional set of messages while writing your validator Attributes or at XML. If NHibernate Validator cannot resolve a key from your ResourceManager nor from ValidatorMessage, it falls back to the default built-in values.&lt;/p&gt;
&lt;p&gt;Alternatively you can provide a &amp;lt;tt class=&amp;quot;classname&amp;quot;&amp;gt;ResourceManager&amp;lt;/tt&amp;gt; while checking programmatically the validation rules on a bean or if you want a completly different interpolation mechanism, you can provide an implementation of &amp;lt;tt class=&amp;quot;literal&amp;quot;&amp;gt;NHibernate.Validator.Engine.IMessageInterpolator&amp;lt;/tt&amp;gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 style="clear:both;" class="title"&gt;Writing your own constraints&lt;/h2&gt;
&lt;p&gt;Extending the set of built-in constraints is extremely easy. Any constraint consists of two pieces: the constraint &lt;span class="emphasis"&gt;&lt;em&gt;descriptor&lt;/em&gt;&lt;/span&gt; (the attribute) and the constraint &lt;span class="emphasis"&gt;&lt;em&gt;validator&lt;/em&gt;&lt;/span&gt; (the implementation class). Here is a simple user-defined descriptor:&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
[ValidatorClass(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt;(PhoneValidator))]
&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; PhoneAttribute : Attribute, IRuleArgs
{
    &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; message = &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt;.Empty;
    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Message
    {
        get { &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; message; }
        set { message = &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;; }
    }
}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;To link a descriptor to its validator implementation, we use the &lt;em&gt;[ValidatorClass]&lt;/em&gt;&amp;nbsp;meta-attribute. Here you can see an implementation of a constraint validator:&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; PhoneValidator : IValidator
{
     &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; IsValid(&lt;span style="color:#0000ff;"&gt;object&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;)
     {
         Regex regex = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Regex(&lt;span style="color:#006080;"&gt;@&amp;quot;^[2-9]\d{2}-\d{3}-\d{4}$&amp;quot;&lt;/span&gt;);
         &lt;span style="color:#0000ff;"&gt;if&lt;/span&gt; (&lt;span style="color:#0000ff;"&gt;value&lt;/span&gt; == &lt;span style="color:#0000ff;"&gt;null&lt;/span&gt;) &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;;
         &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; regex.IsMatch(&lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;.ToString());
     }
}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;This PhoneValidator class is simple. The method &lt;em&gt;IsValid()&lt;/em&gt;&amp;nbsp;receives a string that should be validated with a Regex expression. The &lt;em&gt;IsValid()&lt;/em&gt;&amp;nbsp;method should return false if the constraint has been violated.&lt;/p&gt;
&lt;p&gt;If you need to initialize the class validator you can implement the interface &lt;em&gt;IInitializableValidator&lt;/em&gt;&lt;em&gt;&amp;lt;A&amp;gt;&amp;nbsp;&lt;/em&gt;and the class should look like this:&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; PhoneValidator : IInitializableValidator&amp;lt;PhoneAttribute&amp;gt;
{
    &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; Regex regex;

    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; IsValid(&lt;span style="color:#0000ff;"&gt;object&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;)
    {
        &lt;span style="color:#0000ff;"&gt;if&lt;/span&gt; (&lt;span style="color:#0000ff;"&gt;value&lt;/span&gt; == &lt;span style="color:#0000ff;"&gt;null&lt;/span&gt;) &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;;
        &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; regex.IsMatch(&lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;.ToString());
    }

    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Initialize(PhoneAttribute parameters)
    {
        regex = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Regex(&lt;span style="color:#006080;"&gt;@&amp;quot;^[2-9]\d{2}-\d{3}-\d{4}$&amp;quot;&lt;/span&gt;);
    }
}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;As you can see, implementing the &lt;em&gt;IInitializableValidator&amp;lt;A&amp;gt;&lt;/em&gt;&amp;nbsp;interface we have to implement the method &lt;em&gt;Initialize(...)&lt;/em&gt; that initializes the validator, and it&amp;#39;s executed first of all.&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;IsValid()&lt;/em&gt;&amp;nbsp;method should return &lt;em&gt;false&lt;/em&gt;&amp;nbsp;if the constraint has been violated. For more examples, refer to the built-in validator implementations.&lt;/p&gt;
&lt;p&gt;We now have to implement the validator (ie. the rule checking implementation). A validation implementation can check the value of the a property (by implementing PropertyConstraint ) and/or can modify the NHibernate mapping metadata to express the constraint at the database level (by implementing PersistentClassConstraint)&lt;/p&gt;
&lt;p&gt;We have only seen property level validation, but you can write a class level validation attribute. Instead of receiving the return instance of a property, the bean itself will be passed to the validator. To activate the validation checking, just put the validation attribute to the class itself instead of in a property. A small sample can be found in the unit test suite.&lt;/p&gt;
&lt;p&gt;If your constraint can be applied multiple times (with different parameters) on the same property or type, you can use set the &lt;em&gt;AllowMultiple&lt;/em&gt;&amp;nbsp;parameter to &lt;em&gt;true&lt;/em&gt;&amp;nbsp;on the &lt;em&gt;AttributeUsage&lt;/em&gt;&amp;nbsp;attribute. An example of this it&amp;#39;s the built-in validator Pattern, have a look:&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;[Serializable]
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = &lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;)]
[ValidatorClass(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt; (PatternValidator))]
&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; PatternAttribute : Attribute, IRuleArgs
{
    &amp;hellip;
}&lt;/pre&gt;
&lt;/div&gt;
&lt;pre class="programlisting"&gt;&amp;nbsp;&lt;/pre&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 style="clear:both;" class="title"&gt;Configuring your domain model with .Net Attributes&lt;/h2&gt;
&lt;p&gt;Since you are already familiar with attributes now, the syntax should be very familiar&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; Address
{ 
    &lt;span style="color:#008000;"&gt;// a static non null field  &lt;/span&gt;
    [NotNull]
    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;static&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; blacklistedZipCode;
    
    &lt;span style="color:#008000;"&gt;// field a not null string of 20 characters maximum&lt;/span&gt;
    [Length(Max = 20), NotNull]    
    &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; country;
    &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;long&lt;/span&gt; id;
    &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; internalValid = &lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;;
    &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; line1;
    &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; line2;
    &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; state;
    &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; zip;
    
    &lt;span style="color:#008000;"&gt;// a numeric between 1 and 2000&lt;/span&gt;
    [Min(1), Range(Max = 2000)]
    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;long&lt;/span&gt; Id
    {
        get { &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; id; }
        set { id = &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;; }
    }

    &lt;span style="color:#008000;"&gt;// respond to the constraint in the field: country&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Country
    {
        get { &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; country; }
        set { country = &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;; }
    }

    &lt;span style="color:#008000;"&gt;// a non null string&lt;/span&gt;
    [NotNull]
    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Line1
    {
        get { &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; line1; }
        set { line1 = &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;; }
    }

    &lt;span style="color:#008000;"&gt;// a not null string of 3 characters maximum&lt;/span&gt;
    [Length(Max = 3), NotNull]
    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; State
    {
        get { &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; state; }
        set { state = &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;; }
    }

    &lt;span style="color:#008000;"&gt;// a not null numeric string of 5 characters maximum&lt;/span&gt;
    &lt;span style="color:#008000;"&gt;// if the string is longer, the message will&lt;/span&gt;
    &lt;span style="color:#008000;"&gt;// be searched in the resource bundle at key &amp;#39;long&amp;#39;&lt;/span&gt;
    [Length(Max = 5, Message = &lt;span style="color:#006080;"&gt;&amp;quot;{long}&amp;quot;&lt;/span&gt;)]
    [Pattern(Regex = &lt;span style="color:#006080;"&gt;&amp;quot;[0-9]+&amp;quot;&lt;/span&gt;)]
    [NotNull]
    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Zip
    {
        get { &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; zip; }
        set { zip = &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;; }
    }

    &lt;span style="color:#008000;"&gt;// no constraint&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Line2
    {
        get { &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; line2; }
        set { line2 = &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;; }
    }

    &lt;span style="color:#008000;"&gt;// should always be true&lt;/span&gt;
    [AssertTrue]
    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; InternalValid
    {
        get { &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; internalValid; }
        set { internalValid = &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;; }
    }
} &lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;While the example only shows public property validation, you can also annotate fields of any kind of visibility. In the example &lt;strong&gt;MyBeanConstraint&lt;/strong&gt; is a custom Class Validator, we will talk about this later.&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;[MyBeanConstraint(Max=45)]
&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; Dog 
{
    [AssertTrue] &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; IsMale;
    [NotNull] &lt;span style="color:#0000ff;"&gt;protected&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Name { get {...} set{...} }
    &amp;hellip;
}     &lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;You can also annotate interfaces. NHibernate Validator will check all superclasses and interfaces extended or implemented by a given type to read the appropriate validator attributes.&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;interface&lt;/span&gt; Named 
{
    [NotNull] &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Name {get; set;}
    ...
}

&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; Dog : Named 
{
    [AssertTrue] &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; isMale;
    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Name { get {...} set{...} }
}
&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;The name property will be checked for nullity when the Dog type is validated.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 style="clear:both;" class="title"&gt;Configuring your domain model with XML&lt;/h2&gt;
&lt;p&gt;You also can configurate your domain model with constraints using xml files: the nhv.xml files. We prefer to embed these Xml files into the assemblies, and NHibernate Validator is prepared to inspect them and retrieve the resources with constrains, like NHibernate does with hbm.xml files.&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#800000;"&gt;xml&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;version&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;1.0&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;encoding&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;utf-8&amp;quot;&lt;/span&gt; ?&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;nhv-mapping&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;urn:nhibernate-validator-1.0&amp;quot;&lt;/span&gt;
                        &lt;span style="color:#ff0000;"&gt;namespace&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;NHibernate.Validator.Tests.Base&amp;quot;&lt;/span&gt;
                        &lt;span style="color:#ff0000;"&gt;assembly&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;NHibernate.Validator.Tests&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;class&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Address&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;blacklistedZipCode&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
                  &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;not-null&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;country&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
           &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;length&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;max&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;20&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
           &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;not-null&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;    
    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;floor&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;    
          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;range&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;min&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;-2&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;max&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;50&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;message&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;{floor.out.of.range} (escaping #{el})&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Id&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;    
          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;min&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;value&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;1&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;range&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;max&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;2000&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;max&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;value&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;2500&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Line1&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;    
          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;not-null&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;State&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;    
          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;not-null&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;length&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;max&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;3&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;

    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Zip&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;length&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;max&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;5&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;message&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;{long}&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;pattern&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;regex&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;[0-9]+&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;not-null&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;InternalValid&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;    
          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;asserttrue&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;nhv-mapping&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;      &lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;span class="bold"&gt;&lt;strong&gt;We strongly recommend&lt;/strong&gt;&lt;/span&gt; to validate every nhv.xml file of your application with the respective XSD file. Every &lt;strong&gt;nhv.xml&lt;/strong&gt; file should be validated with the &lt;strong&gt;nhv-mapping.xsd&lt;/strong&gt;&amp;nbsp;file included within the NHibernate Validator distribution whether in source or binaries. Indeed, if you are using an IDE like Visual Studio it offers IntelliSense support in the &lt;strong&gt;nhv.xml&lt;/strong&gt; file if you add the schema to Visual Studio&amp;#39;s xml schemas folder.&lt;/p&gt;
&lt;p&gt;&lt;span class="bold"&gt;&lt;strong&gt;Xml beauties:&lt;/strong&gt;&lt;/span&gt; The nicest part of Xml for validation is that you can put xml validators in a different dll so you don&amp;#39;t need to recompile your domain when validation rules change. Only the dll with the Xml files..&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class="chapter"&gt;
&lt;h1&gt;Configuration&lt;/h1&gt;
&lt;p&gt;NHibernate Validator has global parameters that you can configure at the startup. This configuration can be done in differents ways: programmaticaly or using xml files. If you are involved with NHibernate this could be very familiar to you.&lt;/p&gt;
&lt;p&gt;Configuration properties:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Table Configuration properties&lt;/strong&gt;&lt;/p&gt;
&lt;div class="table"&gt;
&lt;span style="background: SpringGreen;"&gt;&amp;lt;tbody&amp;gt;&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;&amp;lt;/tbody&amp;gt;&lt;/span&gt;
&lt;table&gt;
&amp;lt;tbody&amp;gt;
&lt;tr&gt;
&lt;th align="middle"&gt;Property name&lt;/th&gt;
&lt;th&gt;Purpose&lt;/th&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="middle"&gt;&lt;strong&gt;apply_to_ddl&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Configure NHibernate to apply the constraints to the DDL.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="middle"&gt;&lt;strong&gt;autoregister_listeners&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Register the Event-Listeners automatically on Inserts and Updates.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="middle"&gt;&lt;strong&gt;default_validator_mode&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Define what are the constraints sources are and how to proceed. There are some built-in configurations:
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;div class="itemizedlist"&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;UseAttribute&lt;/strong&gt; (default): Use Attributes to configurate the validators on types and/or members. Xml is ignored.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;UseXml&lt;/strong&gt;: Use Xml to configurate the validators on types and/or members. Attributes are ignored.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;OverrideAttributeWithXml&lt;/strong&gt;: If the same type/member is annotated with an Attribute and has a Xml rule the Attribute annotation will be used.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;OverrideXmlWithAttribute&lt;/strong&gt;: If the same type/member is annotated with an Attribute and has a Xml rule the Attribute annotation will be used.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="middle"&gt;&lt;strong&gt;assembly&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Which are the assemblies with contains the nhv.xml files embedded. You can have more than one assembly.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="middle"&gt;&lt;strong&gt;shared_engine_provider&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;To inject a custom Shared Engine Provider.&lt;/td&gt;
&lt;/tr&gt;
&amp;lt;/tbody&amp;gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 style="clear:both;" class="title"&gt;Programmatic configuration&lt;/h2&gt;
&lt;p&gt;You can use the following code to configure NHibernate.Validator programmatically:&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NHibernate.Validator.Cfg;
&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NHibernate.Validator.Engine;
&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; Environment=NHibernate.Validator.Cfg.Environment;
&amp;hellip;
ValidatorEngine ve = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ValidatorEngine();

NHVConfiguration nhvc = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; NHVConfiguration();
nhvc.Properties[Environment.ApplyToDDL] = &lt;span style="color:#006080;"&gt;&amp;quot;false&amp;quot;&lt;/span&gt;;
nhvc.Properties[Environment.AutoregisterListeners] = &lt;span style="color:#006080;"&gt;&amp;quot;false&amp;quot;&lt;/span&gt;;
nhvc.Properties[Environment.ValidatorMode] = GetMode();
nhvc.Mappings.Add(&lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; MappingConfiguration(&lt;span style="color:#006080;"&gt;&amp;quot;NHibernate.Validator.Demo.Model&amp;quot;&lt;/span&gt;,&lt;span style="color:#0000ff;"&gt;null&lt;/span&gt;));
ve.Configure(nhvc);  &lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;The &lt;em&gt;ValidatorEngine&lt;/em&gt;&amp;nbsp;will be set with the configuration. The NHibernate Validator configuration is represented by the &lt;em&gt;NHibernate.Validator.Cfg.NHVConfiguration&lt;/em&gt;&amp;nbsp;type. Other class used to configure all the stuff is the &lt;em&gt;NHibernate.Validator.Cfg.Environment&lt;/em&gt;&amp;nbsp;type, this class contain the string with the names of the properties of the configuration of NHibernate Validator.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 style="clear:both;" class="title"&gt;Declarative configuration: using Xml files&lt;/h2&gt;
&lt;p&gt;The declarative configuration keeps configuration out of the code. This means configuring the application using a different file, in this case a XML file. But the options don&amp;#39;t end here: you can choose to configure NHibernate Validator using a simple Xml file or you can put the configuration in the well known App.Config/Web.Config. &lt;/p&gt;
&lt;div class="section"&gt;
&lt;h3 class="title"&gt;Simple xml file&lt;/h3&gt;
&lt;p&gt;This is a sample of a simple configuration in a xml file:&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#800000;"&gt;xml&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;version&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;1.0&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;encoding&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;utf-8&amp;quot;&lt;/span&gt; ?&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;nhv-configuration&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;#39;urn:nhv-configuration-1.0&amp;#39;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
  &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;#39;apply_to_ddl&amp;#39;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;true&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
  &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;#39;autoregister_listeners&amp;#39;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;true&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
  &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;#39;default_validator_mode&amp;#39;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;UseXml&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
  &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;mapping&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;assembly&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;#39;NHibernate.Validator.Demo.Model&amp;#39;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;nhv-configuration&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;We encourage to name the configuration file &lt;strong&gt;nhvalidator.cfg.xml&lt;/strong&gt;, because NHibernate Validator will pick up this file from the application directory in order to get the configuration parameters, however you can name this file as you wish.&lt;/p&gt;
&lt;p&gt;In order to configurate NHibernate Validator with the &lt;strong&gt;nhvalidator.cfg.xml&lt;/strong&gt;&amp;nbsp;you can use the following codeו:&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;ValidatorEngine engine = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ValidatorEngine();
engine.Configure(); &lt;span style="color:#008000;"&gt;// by convention reads the nhvalidator.cfg.xml file.&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;You can choose another name for your configuration file and proceed of this manner: &lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;ValidatorEngine engine = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ValidatorEngine();
engine.Configure(&lt;span style="color:#006080;"&gt;&amp;quot;path/to/MyNHVConfigurationFile.xml&amp;quot;&lt;/span&gt;); &lt;span style="color:#008000;"&gt;// you can use whatever path to point to the configuration file&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h3 class="title"&gt;Configuration Application file: app.config/web.config&lt;/h3&gt;
&lt;p&gt;The configuration can be declared into our configuration application .Net files: app.config or web.config. In the below example you will see how to do it.&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#800000;"&gt;xml&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;version&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;1.0&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;encoding&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;utf-8&amp;quot;&lt;/span&gt; ?&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;configuration&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;configSections&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
        &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;section&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;nhv-configuration&amp;quot;&lt;/span&gt; 
                            &lt;span style="color:#ff0000;"&gt;type&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;NHibernate.Validator.Cfg.ConfigurationSectionHandler, NHibernate.Validator&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
        ...
    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;configSections&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    ...
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;nhv-configuration&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;#39;urn:nhv-configuration-1.0&amp;#39;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
        &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;shared_engine_provider&lt;/span&gt; 
         &lt;span style="color:#ff0000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;#39;NHibernate.Validator.Event.NHibernateSharedEngineProvider, NHibernate.Validator&amp;#39;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;nhv-configuration&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    ...
&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;configuration&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;      &lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;</description></item><item><title>NHibernate Validator 1.0.0</title><link>http://nhforge.org/wikis/validator/nhibernate-validator-1-0-0-documentation/revision/3.aspx</link><pubDate>Sat, 13 Sep 2008 05:48:36 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:104</guid><dc:creator>Fabio Maulo</dc:creator><description>Revision 3 posted to Validator by Fabio Maulo on 13/09/2008 02:48:36 a.m.&lt;br /&gt;
&lt;h2&gt;NHibernate Validator 1.0.0&lt;/h2&gt;
&lt;h1&gt;Preface&lt;/h1&gt;
&lt;div class="preface"&gt;
&lt;p&gt;Attributes are a very convenient and elegant way to specify invariant constraints for a domain model. You can, for example, express that a property should never be null, that the account balance should be strictly positive, etc. These domain model constraints are declared in the class itself by annotating its properties. A validator can then read them and check for constraint violations. The validation mechanism can be executed in different layers in your application without having to duplicate any of these rules (presentation layer, data access layer). Following the DRY principle, NHibernate Validator has been designed for that purpose. NHibernate Validator also lets you declare the same rules in XML files. Both approaches (attributes and XML) give us interesting possibilities for configuring our domain rules. &lt;/p&gt;
&lt;p&gt;NHibernate Validator works on two levels. First, it is able to check in-memory instances of a class for constraint violations. Second, it can apply the constraints to the NHibernate metamodel and incorporate them into the generated database schema.&lt;/p&gt;
&lt;p&gt;Each constraint attribute is associated to a validator implementation responsible for checking the constraint on the entity instance. A validator can also (optionally) apply the constraint to the NHibernate metamodel, allowing NHibernate to generate DDL that expresses the constraint. With the appropriate event listener, you can execute the checking operation on inserts and updates done by NHibernate. NHibernate Validator is not limited to only be used with NHibernate. You can easily use it anywhere in your application as well, indeed, you can use it at UI validation with Winforms or Asp.Net technologies.&lt;/p&gt;
&lt;p&gt;When checking instances at runtime, NHibernate Validator returns information about constraint violations in an array of &lt;em&gt;InvalidValue&lt;/em&gt;s. Among other information, the &lt;em&gt;InvalidValue&lt;/em&gt; contains an error description message that can embed the parameter values bundle with the annotation (eg. length limit), and message strings that may be externalized to a ResourceManager.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="chapter"&gt;
&lt;h1&gt;About the project&lt;/h1&gt;
&lt;p&gt;NHibernate Validator is a powerful and extensible framework to validate objects using the .Net Platform. Born as a port of &lt;span class="italic"&gt;Hibernate Validator 3.0.0&lt;/span&gt; project at december of 2007. Later on, many improvements and changes were made to the Core and API, and now we can say that NHibernate Validator is more than just a port. NHibernate Validator is entirely written in C#. The first release of NHibernate Validator was entirely developed by &lt;span class="italic"&gt;Dario Quintana&lt;/span&gt;, &lt;span class="italic"&gt;Gustavo Ringel&lt;/span&gt; and &lt;span class="italic"&gt;Fabio Maulo&lt;/span&gt;. We appreciate the help of &lt;span class="italic"&gt;Davy Brion&lt;/span&gt; in the revision of this document.&lt;/p&gt;
&lt;p&gt;NHibernate Validator is part of the &lt;a href="http://sourceforge.net/projects/nhcontrib/" target="_top"&gt;NHibernate Contrib&lt;/a&gt; project and you can download it &lt;a href="http://sourceforge.net/project/showfiles.php?group_id=216446&amp;amp;package_id=275108&amp;amp;release_id=597674" target="_top"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;You can ask questions about the development and utilization of NHibernate Validator on the following mailing lists:&lt;/p&gt;
&lt;p&gt;The development mailing list can be found &lt;a href="http://groups.google.com/group/nhcdevs" target="_top"&gt;here&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;The user forum can be found &lt;a href="http://groups.google.com/group/nhusers" target="_top"&gt;here&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;Bug and issues report should be sent to the development mailing list.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="chapter"&gt;
&lt;h1&gt;Using the Validator framework&lt;/h1&gt;
&lt;p&gt;NHibernate Validator is intended to be used to implement multi-layered data validation, where constraints are expressed in a single place and checked in various different layers of the application.&lt;/p&gt;
&lt;p&gt;This chapter will cover NHibernate Validator usage for different layers&lt;/p&gt;
&lt;div class="section"&gt;
&lt;h2 class="title" style="clear:both;"&gt;Database schema-level validation&lt;/h2&gt;
&lt;p&gt;Out of the box, NHibernate Validator will translate the constraints you have defined for your entities into mapping metadata. For example, if a property of your entity has the attribute [NotNull], its columns will be declared as &lt;em&gt;not null&lt;/em&gt; in the DDL schema generated by NHibernate.&lt;/p&gt;
&lt;p&gt;Using hbm2ddl, domain model constraints will be expressed into the database schema.&lt;/p&gt;
&lt;p&gt;If, for some reason, the feature needs to be disabled, set &lt;strong&gt;&lt;em&gt;apply_to_ddl&lt;/em&gt;&lt;/strong&gt; to false.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 class="title" style="clear:both;"&gt;ORM integration&lt;/h2&gt;
&lt;p&gt;NHibernate Validator integrates with NHibernate in a native way, and knows how to treat every persistent entity.&lt;/p&gt;
&lt;div class="section"&gt;
&lt;h3 class="title"&gt;NHibernate event-based validation&lt;/h3&gt;
&lt;p&gt;NHibernate Validator has two built-in NHibernate event listeners. Whenever a PreInsertEvent or PreUpdateEvent occurs, the listeners will verify all constraints of the entity instance and throw an exception if any of them are violated. Basically, objects will be checked before any insert and before any update triggered by NHibernate. This includes cascading changes! This is the most convenient and easiest way to activate the validation process. If a constraint is violated, the event will raise a runtime InvalidStateException which contains an array of &lt;em&gt;InvalidValue&lt;/em&gt;s describing each failure.&lt;/p&gt;
&lt;p&gt;If, for some reason, you want to disable this integration, set &lt;strong&gt;autoregister_listeners&lt;/strong&gt;&amp;nbsp;to false&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;span style="text-decoration:underline;"&gt;Note&lt;/span&gt;: If the entities are not annotated with validation attributes, there is no runtime performance cost.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;In case you wish to set manually the event listeners for NHibernate Core, use the following configuration in &lt;strong&gt;hibernate.cfg.xml&lt;/strong&gt;:&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    ...
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;event&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;type&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;pre-update&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
        &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;listener&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;NHibernate.Validator.Event.ValidatePreUpdateEventListener&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;event&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;event&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;type&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;pre-insert&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
        &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;listener&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;NHibernate.Validator.Event.ValidatePreInsertEventListener&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;event&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 class="title" style="clear:both;"&gt;Application-level validation&lt;/h2&gt;
&lt;p&gt;In order to interact with NHibernate Validator in your application you must use the main class: &lt;em&gt;ValidatorEngine&lt;/em&gt;. &lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;ValidatorEngine validator = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ValidatorEngine();
&lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; isValid = validator.IsValid(customer);&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;These two lines are enough to validate an object with its validation rules, which can be configured using Attributes or XML. The first line is the instantiation of the validator engine of NHibernate Validator. The second line is the validation of an object, and the result of the &lt;em&gt;IsValid(...)&lt;/em&gt;method indicates if the object is valid or not. In order to get the invalid values of an object, you can do:&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;ValidatorEngine validator = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ValidatorEngine();
InvalidValue[] validationMessages = validator.GetInvalidValues(customer);&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Now instead of only knowing if the object is valid or not, we get an array of InvalidValues. If this array length is greater than zero then there are validation errors and you can traverse the InvalidValue array to get verbose information about the problems that cause the object to be invalid. &lt;/p&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 class="title" style="clear:both;"&gt;Presentation layer validation&lt;/h2&gt;
&lt;p&gt;[&lt;strong&gt;&lt;span style="color:#008000;"&gt;TODO&lt;/span&gt;&lt;/strong&gt;]&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 class="title" style="clear:both;"&gt;Validation informations&lt;/h2&gt;
&lt;p&gt;NHibernate Validator provides an array of InvalidValue. Each &lt;em&gt;InvalidValue&lt;/em&gt;&amp;nbsp;has several methods describing the reasons that caused the entity to be invalid.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;BeanClass&lt;/strong&gt; retrieves the failing bean type&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Bean&amp;nbsp;&lt;/strong&gt;retrieves the failing instance (if any ie not when using &lt;em&gt;GetPotentialInvalidValues()&lt;/em&gt;)&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Value&lt;/strong&gt;&amp;nbsp;retrieves the failing value&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Message&lt;/strong&gt;&amp;nbsp;retrieves the proper internationalized error message&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;RootBean&lt;/strong&gt;&amp;nbsp;retrieves the root bean instance generating the issue (useful in conjunction with &lt;em&gt;[Valid]&lt;/em&gt;), is null if &lt;em&gt;GetPotentialInvalidValues(...)&lt;/em&gt; is used.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;PropertyPath&lt;/strong&gt;&amp;nbsp;retrieves the dotted path of the failing property starting from the root bean&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class="chapter"&gt;
&lt;h1&gt;Defining constraints&lt;/h1&gt;
&lt;div class="section"&gt;
&lt;h2 class="title" style="clear:both;"&gt;What is a constraint?&lt;/h2&gt;
&lt;p&gt;A constraint is a rule that a given element (field, property or type) has to comply to. The rule semantic is expressed by an Attribute or configured in Xml files. A constraint may have some properties to configure the associated validator, for example a Min constraint will have a value property setting the minimum acceptable value for the element to be valid. &lt;/p&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 class="title" style="clear:both;"&gt;Built in constraints&lt;/h2&gt;
&lt;p&gt;Hibernate Validator comes with some built-in constraints, which cover most of the basic data checks. As we&amp;#39;ll see later, you&amp;#39;re not limited to them, you can literally in a minute write your own constraints.&lt;/p&gt;
&lt;div class="table"&gt;
&lt;p&gt;&lt;strong&gt;Table Built-in constraints&lt;/strong&gt;&lt;/p&gt;
&lt;table cellspacing="0" cellpadding="2"&gt;
&amp;lt;tbody&amp;gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;&lt;strong&gt;Constraint&lt;/strong&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;&lt;strong&gt;Apply on&lt;/strong&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;&lt;strong&gt;Runtime checking&lt;/strong&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;&lt;strong&gt;NHibernate Metadata impact&lt;/strong&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;Length(Min=, Max=)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property (String)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;checks if the string length is between the min-max range&lt;/span&gt;&lt;/td&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;Column length will be set to max&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;Max(Value=)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property (numeric or string representation of a numeric)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;checks if the value is less than or equals to max&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;Add a check constraint on the column&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;Min(Value=)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property (numeric or string representation of a numeric)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;checks if the value is more than or equals to min&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;Add a check constraint on the column&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;NotNull&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;checks if the value is not null&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;Column(s) are not null&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;NotEmpty&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;checks if the string is not empty.&lt;/td&gt;
&lt;td&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Column(s&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;)&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;are&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;not&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;null&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;(&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;for&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;String&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;NotEmptyOrNull&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;Checks if the string is not null or not empty.&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;Past&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property (&lt;span style="text-decoration: line-through; color: red;"&gt;date&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;or&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;calendar&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;)&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;DateTime)&lt;/span&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;check if the date is in the past&lt;/td&gt;
&lt;td&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Add&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;a&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;check&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;constraint&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;on&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;column&lt;/span&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;Future&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property (&lt;span style="text-decoration: line-through; color: red;"&gt;date&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;or&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;calendar&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;)&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;DateTime)&lt;/span&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;check if the date is in the future&lt;/td&gt;
&lt;td&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;none&lt;/span&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;Pattern(Regex=&amp;quot;regexp&amp;quot;, Flag=)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property (string)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;check if the property matches the regular expression given a match flag. The &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;tt&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;class=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;methodname&amp;quot;&amp;gt;Flag&amp;lt;/tt&amp;gt;&lt;/span&gt; &lt;span style="font-family:&amp;#39;Trebuchet MS&amp;#39;;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;tt&amp;gt;Flag&amp;lt;/tt&amp;gt;&lt;/span&gt; parameter is a &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;tt&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;class=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;classname&amp;quot;&amp;gt;System.Text.RegularExpressions.RegexOptions&amp;lt;/tt&amp;gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;&amp;lt;tt&amp;gt;System.Text.RegularExpressions.RegexOptions&amp;lt;/tt&amp;gt;&lt;/span&gt; enum. You can also declare multiple Pattern attributes on the same member.&lt;/td&gt;
&lt;td&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;none&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;Range(Min=, Max=)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property (numeric or string representation of a numeric)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;check if the value is between Min and Max (included)&lt;/td&gt;
&lt;td&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Add&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;a&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;check&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;constraint&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;on&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;column&lt;/span&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;Size(Min=, Max=)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property (array, collection, map)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;check if the element size is between Min and Max (included)&lt;/td&gt;
&lt;td&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;none&lt;/span&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;AssertFalse&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;check that the method evaluates to false (useful for constraints expressed in code rather than &lt;span style="text-decoration: line-through; color: red;"&gt;annotations&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;attributes&lt;/span&gt;)&lt;/td&gt;
&lt;td&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;none&lt;/span&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;AssertTrue&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;check that the method evaluates to true (useful for constraints expressed in code rather than &lt;span style="text-decoration: line-through; color: red;"&gt;annotations&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;attributes&lt;/span&gt;)&lt;/td&gt;
&lt;td&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;none&lt;/span&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;Valid&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property (object)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;perform validation recursively on the associated object. If the object is a Collection or an array, the elements are validated recursively. If the object is a Map, the value elements are validated recursively.&lt;/td&gt;
&lt;td&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;none&lt;/span&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;Email&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property (String)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;check whether the string conforms to the email address specification&lt;/td&gt;
&lt;td&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;none&lt;/span&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;span style="font-size:xx-small;"&gt;Digits&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;property (numeric or string representation of a numeric)&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;checks whether the property is a number having up to &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;tt&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;class=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;literal&amp;quot;&amp;gt;integerDigits&amp;lt;/tt&amp;gt;&lt;/span&gt; &lt;span style="font-family:&amp;#39;Trebuchet MS&amp;#39;;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;tt&amp;gt;integerDigits&amp;lt;/tt&amp;gt;&lt;/span&gt; integer digits and &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;tt&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;class=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;literal&amp;quot;&amp;gt;fractionalDigits&amp;lt;/tt&amp;gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;&amp;lt;tt&amp;gt;fractionalDigits&amp;lt;/tt&amp;gt;&lt;/span&gt; fractonal digits&lt;/span&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;span style="font-size:xx-small;"&gt;define column precision and scale&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&amp;lt;/tbody&amp;gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 class="title" style="clear:both;"&gt;Error messages&lt;/h2&gt;
&lt;p&gt;NHibernate Validator comes with a default set of error messages translated in several languages (if yours is not present, please send us a patch). You can add your own additional set of messages while writing your validator Attributes or at XML. If NHibernate Validator cannot resolve a key from your ResourceManager nor from ValidatorMessage, it falls back to the default built-in values.&lt;/p&gt;
&lt;p&gt;Alternatively you can provide a &amp;lt;tt class=&amp;quot;classname&amp;quot;&amp;gt;ResourceManager&amp;lt;/tt&amp;gt; while checking programmatically the validation rules on a bean or if you want a completly different interpolation mechanism, you can provide an implementation of &amp;lt;tt class=&amp;quot;literal&amp;quot;&amp;gt;NHibernate.Validator.Engine.IMessageInterpolator&amp;lt;/tt&amp;gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 class="title" style="clear:both;"&gt;Writing your own constraints&lt;/h2&gt;
&lt;p&gt;Extending the set of built-in constraints is extremely easy. Any constraint consists of two pieces: the constraint &lt;span class="emphasis"&gt;&lt;em&gt;descriptor&lt;/em&gt;&lt;/span&gt; (the attribute) and the constraint &lt;span class="emphasis"&gt;&lt;em&gt;validator&lt;/em&gt;&lt;/span&gt; (the implementation class). Here is a simple user-defined descriptor:&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
[ValidatorClass(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt;(PhoneValidator))]
&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; PhoneAttribute : Attribute, IRuleArgs
{
    &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; message = &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt;.Empty;
    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Message
    {
        get { &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; message; }
        set { message = &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;; }
    }
}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;To link a descriptor to its validator implementation, we use the &lt;em&gt;[ValidatorClass]&lt;/em&gt;&amp;nbsp;meta-attribute. Here you can see an implementation of a constraint validator:&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; PhoneValidator : IValidator
{
     &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; IsValid(&lt;span style="color:#0000ff;"&gt;object&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;)
     {
         Regex regex = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Regex(&lt;span style="color:#006080;"&gt;@&amp;quot;^[2-9]\d{2}-\d{3}-\d{4}$&amp;quot;&lt;/span&gt;);
         &lt;span style="color:#0000ff;"&gt;if&lt;/span&gt; (&lt;span style="color:#0000ff;"&gt;value&lt;/span&gt; == &lt;span style="color:#0000ff;"&gt;null&lt;/span&gt;) &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;;
         &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; regex.IsMatch(&lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;.ToString());
     }
}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;This PhoneValidator class is simple. The method &lt;em&gt;IsValid()&lt;/em&gt;&amp;nbsp;receives a string that should be validated with a Regex expression. The &lt;em&gt;IsValid()&lt;/em&gt;&amp;nbsp;method should return false if the constraint has been violated.&lt;/p&gt;
&lt;p&gt;If you need to initialize the class validator you can implement the interface &lt;em&gt;IInitializableValidator&lt;/em&gt;&lt;em&gt;&amp;lt;A&amp;gt;&amp;nbsp;&lt;/em&gt;and the class should look like this:&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; PhoneValidator : IInitializableValidator&amp;lt;PhoneAttribute&amp;gt;
{
    &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; Regex regex;

    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; IsValid(&lt;span style="color:#0000ff;"&gt;object&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;)
    {
        &lt;span style="color:#0000ff;"&gt;if&lt;/span&gt; (&lt;span style="color:#0000ff;"&gt;value&lt;/span&gt; == &lt;span style="color:#0000ff;"&gt;null&lt;/span&gt;) &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;;
        &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; regex.IsMatch(&lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;.ToString());
    }

    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Initialize(PhoneAttribute parameters)
    {
        regex = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Regex(&lt;span style="color:#006080;"&gt;@&amp;quot;^[2-9]\d{2}-\d{3}-\d{4}$&amp;quot;&lt;/span&gt;);
    }
}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;As you can see, implementing the &lt;em&gt;IInitializableValidator&amp;lt;A&amp;gt;&lt;/em&gt;&amp;nbsp;interface we have to implement the method &lt;em&gt;Initialize(...)&lt;/em&gt; that initializes the validator, and it&amp;#39;s executed first of all.&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;IsValid()&lt;/em&gt;&amp;nbsp;method should return &lt;em&gt;false&lt;/em&gt;&amp;nbsp;if the constraint has been violated. For more examples, refer to the built-in validator implementations.&lt;/p&gt;
&lt;p&gt;We now have to implement the validator (ie. the rule checking implementation). A validation implementation can check the value of the a property (by implementing PropertyConstraint ) and/or can modify the NHibernate mapping metadata to express the constraint at the database level (by implementing PersistentClassConstraint)&lt;/p&gt;
&lt;p&gt;We have only seen property level validation, but you can write a class level validation attribute. Instead of receiving the return instance of a property, the bean itself will be passed to the validator. To activate the validation checking, just put the validation attribute to the class itself instead of in a property. A small sample can be found in the unit test suite.&lt;/p&gt;
&lt;p&gt;If your constraint can be applied multiple times (with different parameters) on the same property or type, you can use set the &lt;em&gt;AllowMultiple&lt;/em&gt;&amp;nbsp;parameter to &lt;em&gt;true&lt;/em&gt;&amp;nbsp;on the &lt;em&gt;AttributeUsage&lt;/em&gt;&amp;nbsp;attribute. An example of this it&amp;#39;s the built-in validator Pattern, have a look:&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;[Serializable]
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = &lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;)]
[ValidatorClass(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt; (PatternValidator))]
&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; PatternAttribute : Attribute, IRuleArgs
{
    &amp;hellip;
}&lt;/pre&gt;
&lt;/div&gt;
&lt;pre class="programlisting"&gt;&amp;nbsp;&lt;/pre&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 class="title" style="clear:both;"&gt;Configuring your domain model with .Net Attributes&lt;/h2&gt;
&lt;p&gt;Since you are already familiar with attributes now, the syntax should be very familiar&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; Address
{ 
    &lt;span style="color:#008000;"&gt;// a static non null field  &lt;/span&gt;
    [NotNull]
    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;static&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; blacklistedZipCode;
    
    &lt;span style="color:#008000;"&gt;// field a not null string of 20 characters maximum&lt;/span&gt;
    [Length(Max = 20), NotNull]    
    &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; country;
    &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;long&lt;/span&gt; id;
    &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; internalValid = &lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;;
    &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; line1;
    &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; line2;
    &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; state;
    &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; zip;
    
    &lt;span style="color:#008000;"&gt;// a numeric between 1 and 2000&lt;/span&gt;
    [Min(1), Range(Max = 2000)]
    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;long&lt;/span&gt; Id
    {
        get { &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; id; }
        set { id = &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;; }
    }

    &lt;span style="color:#008000;"&gt;// respond to the constraint in the field: country&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Country
    {
        get { &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; country; }
        set { country = &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;; }
    }

    &lt;span style="color:#008000;"&gt;// a non null string&lt;/span&gt;
    [NotNull]
    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Line1
    {
        get { &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; line1; }
        set { line1 = &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;; }
    }

    &lt;span style="color:#008000;"&gt;// a not null string of 3 characters maximum&lt;/span&gt;
    [Length(Max = 3), NotNull]
    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; State
    {
        get { &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; state; }
        set { state = &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;; }
    }

    &lt;span style="color:#008000;"&gt;// a not null numeric string of 5 characters maximum&lt;/span&gt;
    &lt;span style="color:#008000;"&gt;// if the string is longer, the message will&lt;/span&gt;
    &lt;span style="color:#008000;"&gt;// be searched in the resource bundle at key &amp;#39;long&amp;#39;&lt;/span&gt;
    [Length(Max = 5, Message = &lt;span style="color:#006080;"&gt;&amp;quot;{long}&amp;quot;&lt;/span&gt;)]
    [Pattern(Regex = &lt;span style="color:#006080;"&gt;&amp;quot;[0-9]+&amp;quot;&lt;/span&gt;)]
    [NotNull]
    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Zip
    {
        get { &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; zip; }
        set { zip = &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;; }
    }

    &lt;span style="color:#008000;"&gt;// no constraint&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Line2
    {
        get { &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; line2; }
        set { line2 = &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;; }
    }

    &lt;span style="color:#008000;"&gt;// should always be true&lt;/span&gt;
    [AssertTrue]
    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; InternalValid
    {
        get { &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; internalValid; }
        set { internalValid = &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;; }
    }
} &lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;While the example only shows public property validation, you can also annotate fields of any kind of visibility. In the example &lt;strong&gt;MyBeanConstraint&lt;/strong&gt; is a custom Class Validator, we will talk about this later.&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;[MyBeanConstraint(Max=45)]
&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; Dog 
{
    [AssertTrue] &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; IsMale;
    [NotNull] &lt;span style="color:#0000ff;"&gt;protected&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Name { get {...} set{...} }
    &amp;hellip;
}     &lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;You can also annotate interfaces. NHibernate Validator will check all superclasses and interfaces extended or implemented by a given type to read the appropriate validator attributes.&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;interface&lt;/span&gt; Named 
{
    [NotNull] &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Name {get; set;}
    ...
}

&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; Dog : Named 
{
    [AssertTrue] &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; isMale;
    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Name { get {...} set{...} }
}
&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;The name property will be checked for nullity when the Dog type is validated.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 class="title" style="clear:both;"&gt;Configuring your domain model with XML&lt;/h2&gt;
&lt;p&gt;You also can configurate your domain model with constraints using xml files: the nhv.xml files. We prefer to embed these Xml files into the assemblies, and NHibernate Validator is prepared to inspect them and retrieve the resources with constrains, like NHibernate does with hbm.xml files.&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#800000;"&gt;xml&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;version&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;1.0&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;encoding&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;utf-8&amp;quot;&lt;/span&gt; ?&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;nhv-mapping&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;urn:nhibernate-validator-1.0&amp;quot;&lt;/span&gt;
                        &lt;span style="color:#ff0000;"&gt;namespace&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;NHibernate.Validator.Tests.Base&amp;quot;&lt;/span&gt;
                        &lt;span style="color:#ff0000;"&gt;assembly&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;NHibernate.Validator.Tests&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;class&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Address&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;blacklistedZipCode&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
                  &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;not-null&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;country&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
           &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;length&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;max&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;20&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
           &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;not-null&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;    
    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;floor&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;    
          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;range&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;min&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;-2&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;max&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;50&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;message&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;{floor.out.of.range} (escaping #{el})&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Id&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;    
          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;min&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;value&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;1&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;range&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;max&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;2000&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;max&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;value&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;2500&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Line1&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;    
          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;not-null&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;State&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;    
          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;not-null&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;length&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;max&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;3&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;

    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Zip&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;length&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;max&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;5&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;message&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;{long}&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;pattern&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;regex&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;[0-9]+&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;not-null&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;InternalValid&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;    
          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;asserttrue&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;nhv-mapping&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;      &lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;span class="bold"&gt;&lt;strong&gt;We strongly recommend&lt;/strong&gt;&lt;/span&gt; to validate every nhv.xml file of your application with the respective XSD file. Every &lt;strong&gt;nhv.xml&lt;/strong&gt; file should be validated with the &lt;strong&gt;nhv-mapping.xsd&lt;/strong&gt;&amp;nbsp;file included within the NHibernate Validator distribution whether in source or binaries. Indeed, if you are using an IDE like Visual Studio it offers IntelliSense support in the &lt;strong&gt;nhv.xml&lt;/strong&gt; file if you add the schema to Visual Studio&amp;#39;s xml schemas folder.&lt;/p&gt;
&lt;p&gt;&lt;span class="bold"&gt;&lt;strong&gt;Xml beauties:&lt;/strong&gt;&lt;/span&gt; The nicest part of Xml for validation is that you can put xml validators in a different dll so you don&amp;#39;t need to recompile your domain when validation rules change. Only the dll with the Xml files..&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class="chapter"&gt;
&lt;h1&gt;Configuration&lt;/h1&gt;
&lt;p&gt;NHibernate Validator has global parameters that you can configure at the startup. This configuration can be done in differents ways: programmaticaly or using xml files. If you are involved with NHibernate this could be very familiar to you.&lt;/p&gt;
&lt;p&gt;Configuration properties:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Table Configuration properties&lt;/strong&gt;&lt;/p&gt;
&lt;div class="table"&gt;
&lt;table&gt;
&amp;lt;tbody&amp;gt;
&lt;tr&gt;
&lt;th align="middle"&gt;Property name&lt;/th&gt;
&lt;th&gt;Purpose&lt;/th&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="middle"&gt;&lt;strong&gt;apply_to_ddl&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Configure NHibernate to apply the constraints to the DDL.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="middle"&gt;&lt;strong&gt;autoregister_listeners&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Register the Event-Listeners automatically on Inserts and Updates.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="middle"&gt;&lt;strong&gt;default_validator_mode&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Define what are the constraints sources are and how to proceed. There are some built-in configurations:
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;div class="itemizedlist"&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;UseAttribute&lt;/strong&gt; (default): Use Attributes to configurate the validators on types and/or members. Xml is ignored.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;UseXml&lt;/strong&gt;: Use Xml to configurate the validators on types and/or members. Attributes are ignored.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;OverrideAttributeWithXml&lt;/strong&gt;: If the same type/member is annotated with an Attribute and has a Xml rule the Attribute annotation will be used.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;OverrideXmlWithAttribute&lt;/strong&gt;: If the same type/member is annotated with an Attribute and has a Xml rule the Attribute annotation will be used.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="middle"&gt;&lt;strong&gt;assembly&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Which are the assemblies with contains the nhv.xml files embedded. You can have more than one assembly.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="middle"&gt;&lt;strong&gt;shared_engine_provider&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;To inject a custom Shared Engine Provider.&lt;/td&gt;
&lt;/tr&gt;
&amp;lt;/tbody&amp;gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 class="title" style="clear:both;"&gt;Programmatic configuration&lt;/h2&gt;
&lt;p&gt;You can use the following code to configure NHibernate.Validator programmatically:&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NHibernate.Validator.Cfg;
&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NHibernate.Validator.Engine;
&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; Environment=NHibernate.Validator.Cfg.Environment;
&amp;hellip;
ValidatorEngine ve = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ValidatorEngine();

NHVConfiguration nhvc = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; NHVConfiguration();
nhvc.Properties[Environment.ApplyToDDL] = &lt;span style="color:#006080;"&gt;&amp;quot;false&amp;quot;&lt;/span&gt;;
nhvc.Properties[Environment.AutoregisterListeners] = &lt;span style="color:#006080;"&gt;&amp;quot;false&amp;quot;&lt;/span&gt;;
nhvc.Properties[Environment.ValidatorMode] = GetMode();
nhvc.Mappings.Add(&lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; MappingConfiguration(&lt;span style="color:#006080;"&gt;&amp;quot;NHibernate.Validator.Demo.Model&amp;quot;&lt;/span&gt;,&lt;span style="color:#0000ff;"&gt;null&lt;/span&gt;));
ve.Configure(nhvc);  &lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;The &lt;em&gt;ValidatorEngine&lt;/em&gt;&amp;nbsp;will be set with the configuration. The NHibernate Validator configuration is represented by the &lt;em&gt;NHibernate.Validator.Cfg.NHVConfiguration&lt;/em&gt;&amp;nbsp;type. Other class used to configure all the stuff is the &lt;em&gt;NHibernate.Validator.Cfg.Environment&lt;/em&gt;&amp;nbsp;type, this class contain the string with the names of the properties of the configuration of NHibernate Validator.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 class="title" style="clear:both;"&gt;Declarative configuration: using Xml files&lt;/h2&gt;
&lt;p&gt;The declarative configuration keeps configuration out of the code. This means configuring the application using a different file, in this case a XML file. But the options don&amp;#39;t end here: you can choose to configure NHibernate Validator using a simple Xml file or you can put the configuration in the well known App.Config/Web.Config. &lt;/p&gt;
&lt;div class="section"&gt;
&lt;h3 class="title"&gt;Simple xml file&lt;/h3&gt;
&lt;p&gt;This is a sample of a simple configuration in a xml file:&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#800000;"&gt;xml&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;version&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;1.0&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;encoding&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;utf-8&amp;quot;&lt;/span&gt; ?&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;nhv-configuration&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;#39;urn:nhv-configuration-1.0&amp;#39;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
  &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;#39;apply_to_ddl&amp;#39;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;true&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
  &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;#39;autoregister_listeners&amp;#39;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;true&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
  &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;#39;default_validator_mode&amp;#39;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;UseXml&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
  &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;mapping&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;assembly&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;#39;NHibernate.Validator.Demo.Model&amp;#39;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;nhv-configuration&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;We encourage to name the configuration file &lt;strong&gt;nhvalidator.cfg.xml&lt;/strong&gt;, because NHibernate Validator will pick up this file from the application directory in order to get the configuration parameters, however you can name this file as you wish.&lt;/p&gt;
&lt;p&gt;In order to configurate NHibernate Validator with the &lt;strong&gt;nhvalidator.cfg.xml&lt;/strong&gt;&amp;nbsp;you can use the following codeו:&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;ValidatorEngine engine = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ValidatorEngine();
engine.Configure(); &lt;span style="color:#008000;"&gt;// by convention reads the nhvalidator.cfg.xml file.&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;You can choose another name for your configuration file and proceed of this manner: &lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;ValidatorEngine engine = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ValidatorEngine();
engine.Configure(&lt;span style="color:#006080;"&gt;&amp;quot;path/to/MyNHVConfigurationFile.xml&amp;quot;&lt;/span&gt;); &lt;span style="color:#008000;"&gt;// you can use whatever path to point to the configuration file&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h3 class="title"&gt;Configuration Application file: app.config/web.config&lt;/h3&gt;
&lt;p&gt;The configuration can be declared into our configuration application .Net files: app.config or web.config. In the below example you will see how to do it.&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#800000;"&gt;xml&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;version&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;1.0&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;encoding&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;utf-8&amp;quot;&lt;/span&gt; ?&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;configuration&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;configSections&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
        &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;section&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;nhv-configuration&amp;quot;&lt;/span&gt; 
                            &lt;span style="color:#ff0000;"&gt;type&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;NHibernate.Validator.Cfg.ConfigurationSectionHandler, NHibernate.Validator&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
        ...
    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;configSections&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    ...
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;nhv-configuration&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;#39;urn:nhv-configuration-1.0&amp;#39;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
        &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;shared_engine_provider&lt;/span&gt; 
         &lt;span style="color:#ff0000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;#39;NHibernate.Validator.Event.NHibernateSharedEngineProvider, NHibernate.Validator&amp;#39;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;nhv-configuration&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    ...
&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;configuration&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;      &lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;</description></item><item><title>NHibernate Validator 1.0.0</title><link>http://nhforge.org/wikis/validator/nhibernate-validator-1-0-0-documentation/revision/2.aspx</link><pubDate>Sat, 13 Sep 2008 05:20:53 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:103</guid><dc:creator>Fabio Maulo</dc:creator><description>Revision 2 posted to Validator by Fabio Maulo on 13/09/2008 02:20:53 a.m.&lt;br /&gt;
&lt;h2&gt;NHibernate Validator 1.0.0&lt;/h2&gt;
&lt;h1&gt;Preface&lt;/h1&gt;
&lt;div class="preface"&gt;
&lt;p&gt;Attributes are a very convenient and elegant way to specify invariant constraints for a domain model. You can, for example, express that a property should never be null, that the account balance should be strictly positive, etc. These domain model constraints are declared in the class itself by annotating its properties. A validator can then read them and check for constraint violations. The validation mechanism can be executed in different layers in your application without having to duplicate any of these rules (presentation layer, data access layer). Following the DRY principle, NHibernate Validator has been designed for that purpose. NHibernate Validator also lets you declare the same rules in XML files. Both approaches (attributes and XML) give us interesting possibilities for configuring our domain rules. &lt;/p&gt;
&lt;p&gt;NHibernate Validator works on two levels. First, it is able to check in-memory instances of a class for constraint violations. Second, it can apply the constraints to the NHibernate metamodel and incorporate them into the generated database schema.&lt;/p&gt;
&lt;p&gt;Each constraint attribute is associated to a validator implementation responsible for checking the constraint on the entity instance. A validator can also (optionally) apply the constraint to the NHibernate metamodel, allowing NHibernate to generate DDL that expresses the constraint. With the appropriate event listener, you can execute the checking operation on inserts and updates done by NHibernate. NHibernate Validator is not limited to only be used with NHibernate. You can easily use it anywhere in your application as well, indeed, you can use it at UI validation with Winforms or Asp.Net technologies.&lt;/p&gt;
&lt;p&gt;When checking instances at runtime, NHibernate Validator returns information about constraint violations in an array of &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;tt&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;class=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;classname&amp;quot;&amp;gt;InvalidValue&amp;lt;/tt&amp;gt;&lt;/span&gt; &lt;em&gt;&lt;span style="background: SpringGreen;"&gt;InvalidValue&lt;/span&gt;&lt;/em&gt;s. Among other information, the &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;tt&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;class=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;classname&amp;quot;&amp;gt;InvalidValue&amp;lt;/tt&amp;gt;&lt;/span&gt; &lt;em&gt;&lt;span style="background: SpringGreen;"&gt;InvalidValue&lt;/span&gt;&lt;/em&gt; contains an error description message that can embed the parameter values bundle with the annotation (eg. length limit), and message strings that may be externalized to a &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;tt&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;class=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;classname&amp;quot;&amp;gt;ResourceManager&amp;lt;/tt&amp;gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;ResourceManager&lt;/span&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="chapter"&gt;
&lt;h1&gt;About the project&lt;/h1&gt;
&lt;p&gt;NHibernate Validator is a powerful and extensible framework to validate objects using the .Net Platform. Born as a port of &lt;span class="italic"&gt;Hibernate Validator 3.0.0&lt;/span&gt; project at december of 2007. Later on, many improvements and changes were made to the Core and API, and now we can say that NHibernate Validator is more than just a port. NHibernate Validator is entirely written in C#. The first release of NHibernate Validator was entirely developed by &lt;span class="italic"&gt;Dario Quintana&lt;/span&gt;, &lt;span class="italic"&gt;Gustavo Ringel&lt;/span&gt; and &lt;span class="italic"&gt;Fabio Maulo&lt;/span&gt;. We appreciate the help of &lt;span class="italic"&gt;Davy Brion&lt;/span&gt; in the revision of this document.&lt;/p&gt;
&lt;p&gt;NHibernate Validator is part of the &lt;a target="_top" href="http://sourceforge.net/projects/nhcontrib/"&gt;NHibernate Contrib&lt;/a&gt; project and you can download it &lt;a target="_top" href="http://sourceforge.net/project/showfiles.php?group_id=216446&amp;amp;package_id=275108&amp;amp;release_id=597674"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;You can ask questions about the development and utilization of NHibernate Validator on the following mailing lists:&lt;/p&gt;
&lt;p&gt;The development mailing list can be found &lt;a target="_top" href="http://groups.google.com/group/nhcdevs"&gt;here&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;The user forum can be found &lt;a target="_top" href="http://groups.google.com/group/nhusers"&gt;here&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;Bug and issues report should be sent to the development mailing list.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="chapter"&gt;
&lt;h1&gt;Using the Validator framework&lt;/h1&gt;
&lt;p&gt;NHibernate Validator is intended to be used to implement multi-layered data validation, where constraints are expressed in a single place and checked in various different layers of the application.&lt;/p&gt;
&lt;p&gt;This chapter will cover NHibernate Validator usage for different layers&lt;/p&gt;
&lt;div class="section"&gt;
&lt;h2 style="clear:both;" class="title"&gt;Database schema-level validation&lt;/h2&gt;
&lt;p&gt;Out of the box, NHibernate Validator will translate the constraints you have defined for your entities into mapping metadata. For example, if a property of your entity has the attribute &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;tt&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;class=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;literal&amp;quot;&amp;gt;[NotNull]&amp;lt;/tt&amp;gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;[&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;NotNull],&lt;/span&gt; its columns will be declared as &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;tt&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;class=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;literal&amp;quot;&amp;gt;not&lt;/span&gt; &lt;em&gt;&lt;span style="background: SpringGreen;"&gt;not&lt;/span&gt; null&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;/tt&amp;gt;&lt;/span&gt; &lt;/em&gt; in the DDL schema generated by NHibernate.&lt;/p&gt;
&lt;p&gt;Using hbm2ddl, domain model constraints will be expressed into the database schema.&lt;/p&gt;
&lt;p&gt;If, for some reason, the feature needs to be disabled, set &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;tt&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;class=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;literal&amp;quot;&amp;gt;apply_to_ddl&amp;lt;/tt&amp;gt;&lt;/span&gt; &lt;strong&gt;&lt;em&gt;&lt;span style="background: SpringGreen;"&gt;apply_to_ddl&lt;/span&gt;&lt;/em&gt;&lt;/strong&gt; to &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;tt&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;class=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;literal&amp;quot;&amp;gt;false&amp;lt;/tt&amp;gt;.&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;false&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 style="clear:both;" class="title"&gt;ORM integration&lt;/h2&gt;
&lt;p&gt;NHibernate Validator integrates with NHibernate in a native way, and knows how to treat every persistent entity.&lt;/p&gt;
&lt;div class="section"&gt;
&lt;h3 class="title"&gt;NHibernate event-based validation&lt;/h3&gt;
&lt;p&gt;NHibernate Validator has two built-in NHibernate event listeners. Whenever a &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;tt&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;class=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;literal&amp;quot;&amp;gt;PreInsertEvent&amp;lt;/tt&amp;gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;PreInsertEvent&lt;/span&gt; or &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;tt&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;class=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;literal&amp;quot;&amp;gt;PreUpdateEvent&amp;lt;/tt&amp;gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;PreUpdateEvent&lt;/span&gt; occurs, the listeners will verify all constraints of the entity instance and throw an exception if any of them are violated. Basically, objects will be checked before any insert and before any update triggered by NHibernate. This includes cascading changes! This is the most convenient and easiest way to activate the validation process. If a constraint is violated, the event will raise a runtime &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;tt&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;class=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;classname&amp;quot;&amp;gt;InvalidStateException&amp;lt;/tt&amp;gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;InvalidStateException&lt;/span&gt; which contains an array of &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;tt&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;class=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;literal&amp;quot;&amp;gt;InvalidValue&amp;lt;/tt&amp;gt;s&lt;/span&gt; &lt;em&gt;&lt;span style="background: SpringGreen;"&gt;InvalidValue&lt;/span&gt;&lt;/em&gt;&lt;span style="background: SpringGreen;"&gt;s&lt;/span&gt; describing each failure.&lt;/p&gt;
&lt;p&gt;If, for some reason, you want to disable this integration, set &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;tt&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;class=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;literal&amp;quot;&amp;gt;autoregister_listeners&amp;lt;/tt&amp;gt;&lt;/span&gt; &lt;strong&gt;&lt;span style="background: SpringGreen;"&gt;autoregister_listeners&lt;/span&gt;&lt;/strong&gt;&amp;nbsp;to &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;tt&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;class=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;literal&amp;quot;&amp;gt;false&amp;lt;/tt&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;tt&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;class=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;literal&amp;quot;&amp;gt;&amp;lt;/tt&amp;gt;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;false&lt;/span&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;span style="text-decoration:underline;"&gt;Note&lt;/span&gt;: If the entities are not annotated with validation attributes, there is no runtime performance cost.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;In case you wish to set manually the event listeners for NHibernate Core, use the following configuration in &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;tt&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;class=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;literal&amp;quot;&amp;gt;hibernate.cfg.xml&amp;lt;/tt&amp;gt;:&lt;/span&gt;&lt;strong&gt;&lt;span style="background: SpringGreen;"&gt;hibernate.cfg.xml&lt;/span&gt;&lt;/strong&gt;&lt;span style="background: SpringGreen;"&gt;:&lt;/span&gt;&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    ...
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;event&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;type&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;pre-update&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
        &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;listener&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;NHibernate.Validator.Event.ValidatePreUpdateEventListener&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;event&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;event&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;type&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;pre-insert&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
        &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;listener&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;NHibernate.Validator.Event.ValidatePreInsertEventListener&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;event&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 style="clear:both;" class="title"&gt;Application-level validation&lt;/h2&gt;
&lt;p&gt;In order to interact with NHibernate Validator in your application you must use the main class: &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;tt&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;class=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;classname&amp;quot;&amp;gt;ValidatorEngine&amp;lt;/tt&amp;gt;.&lt;/span&gt; &lt;em&gt;&lt;span style="background: SpringGreen;"&gt;ValidatorEngine&lt;/span&gt;&lt;/em&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;ValidatorEngine validator = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ValidatorEngine();
&lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; isValid = validator.IsValid(customer);&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;These two lines are enough to validate an object with its validation rules, which can be configured using Attributes or XML. The first line is the instantiation of the validator engine of NHibernate Validator. The second line is the validation of an object, and the result of the &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;tt&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;class=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;methodname&amp;quot;&amp;gt;IsValid(...)&amp;lt;/tt&amp;gt;&lt;/span&gt; &lt;em&gt;&lt;span style="background: SpringGreen;"&gt;IsValid(...&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;)&lt;/span&gt;&lt;/em&gt;method indicates if the object is valid or not. In order to get the invalid values of an object, you can do:&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;ValidatorEngine validator = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ValidatorEngine();
InvalidValue[] validationMessages = validator.GetInvalidValues(customer);&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Now instead of only knowing if the object is valid or not, we get an array of InvalidValues. If this array length is greater than zero then there are validation errors and you can traverse the InvalidValue array to get verbose information about the problems that cause the object to be invalid. &lt;/p&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 style="clear:both;" class="title"&gt;Presentation layer validation&lt;/h2&gt;
&lt;p&gt;[&lt;strong&gt;&lt;span style="color:#008000;"&gt;TODO&lt;/span&gt;&lt;/strong&gt;]&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 style="clear:both;" class="title"&gt;Validation informations&lt;/h2&gt;
&lt;p&gt;NHibernate Validator provides an array of &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;tt&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;class=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;classname&amp;quot;&amp;gt;InvalidValue&amp;lt;/tt&amp;gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;InvalidValue&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; Each &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;tt&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;class=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;literal&amp;quot;&amp;gt;InvalidValue&amp;lt;/tt&amp;gt;&lt;/span&gt; &lt;em&gt;&lt;span style="background: SpringGreen;"&gt;InvalidValue&lt;/span&gt;&lt;/em&gt;&amp;nbsp;has several methods describing the reasons that caused the entity to be invalid.&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;tt&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;class=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;methodname&amp;quot;&amp;gt;BeanClass&amp;lt;/tt&amp;gt;&lt;/span&gt; &lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;span style="background: SpringGreen;"&gt;BeanClass&lt;/span&gt;&lt;/strong&gt; retrieves the failing bean type&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;tt&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;class=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;methodname&amp;quot;&amp;gt;Bean&amp;lt;/tt&amp;gt;retrieves&lt;/span&gt; &lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;span style="background: SpringGreen;"&gt;Bean&lt;/span&gt;&amp;nbsp;&lt;/strong&gt;&lt;span style="background: SpringGreen;"&gt;retrieves&lt;/span&gt; the failing instance (if any ie not when using &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;tt&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;class=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;methodname&amp;quot;&amp;gt;GetPotentialInvalidValues()&amp;lt;/tt&amp;gt;)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;tt&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;class=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;methodname&amp;quot;&amp;gt;Value&amp;lt;/tt&amp;gt;&lt;/span&gt; &lt;em&gt;&lt;span style="background: SpringGreen;"&gt;GetPotentialInvalidValues(&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;)&lt;/span&gt;&lt;/em&gt;&lt;span style="background: SpringGreen;"&gt;)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;span style="background: SpringGreen;"&gt;Value&lt;/span&gt;&lt;/strong&gt;&amp;nbsp;retrieves the failing value&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;tt&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;class=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;methodname&amp;quot;&amp;gt;Message&amp;lt;/tt&amp;gt;&lt;/span&gt; &lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;span style="background: SpringGreen;"&gt;Message&lt;/span&gt;&lt;/strong&gt;&amp;nbsp;retrieves the proper internationalized error message&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;tt&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;class=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;methodname&amp;quot;&amp;gt;RootBean&amp;lt;/tt&amp;gt;&lt;/span&gt; &lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;span style="background: SpringGreen;"&gt;RootBean&lt;/span&gt;&lt;/strong&gt;&amp;nbsp;retrieves the root bean instance generating the issue (useful in conjunction with &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;tt&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;class=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;literal&amp;quot;&amp;gt;[Valid]&amp;lt;/tt&amp;gt;),&lt;/span&gt; &lt;em&gt;&lt;span style="background: SpringGreen;"&gt;[&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;Valid]&lt;/span&gt;&lt;/em&gt;&lt;span style="background: SpringGreen;"&gt;)&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; is null if &lt;em&gt;GetPotentialInvalidValues(...)&lt;/em&gt; is used.&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;tt&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;class=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;literal&amp;quot;&amp;gt;PropertyPath&amp;lt;/tt&amp;gt;&lt;/span&gt; &lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;span style="background: SpringGreen;"&gt;PropertyPath&lt;/span&gt;&lt;/strong&gt;&amp;nbsp;retrieves the dotted path of the failing property starting from the root bean&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class="chapter"&gt;
&lt;h1&gt;Defining constraints&lt;/h1&gt;
&lt;div class="section"&gt;
&lt;h2 style="clear:both;" class="title"&gt;What is a constraint?&lt;/h2&gt;
&lt;p&gt;A constraint is a rule that a given element (field, property or type) has to comply to. The rule semantic is expressed by an Attribute or configured in Xml files. A constraint may have some properties to configure the associated validator, for example a Min constraint will have a value property setting the minimum acceptable value for the element to be valid. &lt;/p&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 style="clear:both;" class="title"&gt;Built in constraints&lt;/h2&gt;
&lt;p&gt;Hibernate Validator comes with some built-in constraints, which cover most of the basic data checks. As we&amp;#39;ll see later, you&amp;#39;re not limited to them, you can literally in a minute write your own constraints.&lt;/p&gt;
&lt;div class="table"&gt;
&lt;p&gt;&lt;strong&gt;Table Built-in constraints&lt;/strong&gt;&lt;/p&gt;
&lt;table&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;colgroup&amp;gt;&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;col&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;align=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;middle&amp;quot;&amp;gt;&amp;lt;/col&amp;gt;&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;col&amp;gt;&amp;lt;/col&amp;gt;&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;col&amp;gt;&amp;lt;/col&amp;gt;&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;col&amp;gt;&amp;lt;/col&amp;gt;&amp;lt;/colgroup&amp;gt;&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;thead&amp;gt;&lt;/span&gt;
&lt;/strong&gt;&lt;/p&gt;
&lt;br /&gt;
&lt;table&gt;
&lt;span style="background: SpringGreen;"&gt;&amp;lt;tbody&amp;gt;&lt;/span&gt;
&lt;tr&gt;
&lt;th align="middle"&gt;Constraint&lt;/th&gt;
&lt;th&gt;Apply on&lt;/th&gt;
&lt;th&gt;Runtime checking&lt;/th&gt;
&lt;th&gt;NHibernate Metadata impact&lt;/th&gt;
&lt;/tr&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;/thead&amp;gt;&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;tbody&amp;gt;&lt;/span&gt;
&lt;/th&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="middle"&gt;Length(Min=, Max=)&lt;/td&gt;
&lt;td&gt;property (String)&lt;/td&gt;
&lt;td&gt;checks if the string length is between the min-max range&lt;/td&gt;
&lt;td&gt;Column length will be set to max&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="middle"&gt;Max(Value=)&lt;/td&gt;
&lt;td&gt;property (numeric or string representation of a numeric)&lt;/td&gt;
&lt;td&gt;checks if the value is less than or equals to max&lt;/td&gt;
&lt;td&gt;Add a check constraint on the column&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="middle"&gt;Min(Value=)&lt;/td&gt;
&lt;td&gt;property (numeric or string representation of a numeric)&lt;/td&gt;
&lt;td&gt;checks if the value is more than or equals to min&lt;/td&gt;
&lt;td&gt;Add a check constraint on the column&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="middle"&gt;NotNull&lt;/td&gt;
&lt;td&gt;property&lt;/td&gt;
&lt;td&gt;checks if the value is not null&lt;/td&gt;
&lt;td&gt;Column(s) are not null&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="middle"&gt;NotEmpty&lt;/td&gt;
&lt;td&gt;property&lt;/td&gt;
&lt;td&gt;checks if the string is not empty. &lt;/td&gt;
&lt;td&gt;Column(s) are not null (for String)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="middle"&gt;NotEmptyOrNull&lt;/td&gt;
&lt;td&gt;property&lt;/td&gt;
&lt;td&gt;Checks if the string is not null or not empty.&lt;/td&gt;
&lt;td&gt;&amp;nbsp;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="middle"&gt;Past&lt;/td&gt;
&lt;td&gt;property (date or calendar)&lt;/td&gt;
&lt;td&gt;check if the date is in the past&lt;/td&gt;
&lt;td&gt;Add a check constraint on the column&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="middle"&gt;Future&lt;/td&gt;
&lt;td&gt;property (date or calendar)&lt;/td&gt;
&lt;td&gt;check if the date is in the future&lt;/td&gt;
&lt;td&gt;none&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="middle"&gt;Pattern(Regex=&amp;quot;regexp&amp;quot;, Flag=) &lt;/td&gt;
&lt;td&gt;property (string)&lt;/td&gt;
&lt;td&gt;check if the property matches the regular expression given a match flag. The &amp;lt;tt class=&amp;quot;methodname&amp;quot;&amp;gt;Flag&amp;lt;/tt&amp;gt; parameter is a &amp;lt;tt class=&amp;quot;classname&amp;quot;&amp;gt;System.Text.RegularExpressions.RegexOptions&amp;lt;/tt&amp;gt; enum. You can also declare multiple Pattern attributes on the same member. &lt;/td&gt;
&lt;td&gt;none&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="middle"&gt;Range(Min=, Max=)&lt;/td&gt;
&lt;td&gt;property (numeric or string representation of a numeric)&lt;/td&gt;
&lt;td&gt;check if the value is between Min and Max (included)&lt;/td&gt;
&lt;td&gt;Add a check constraint on the column&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="middle"&gt;Size(Min=, Max=)&lt;/td&gt;
&lt;td&gt;property (array, collection, map)&lt;/td&gt;
&lt;td&gt;check if the element size is between Min and Max (included)&lt;/td&gt;
&lt;td&gt;none&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="middle"&gt;AssertFalse&lt;/td&gt;
&lt;td&gt;property&lt;/td&gt;
&lt;td&gt;check that the method evaluates to false (useful for constraints expressed in code rather than annotations)&lt;/td&gt;
&lt;td&gt;none&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="middle"&gt;AssertTrue&lt;/td&gt;
&lt;td&gt;property&lt;/td&gt;
&lt;td&gt;check that the method evaluates to true (useful for constraints expressed in code rather than annotations)&lt;/td&gt;
&lt;td&gt;none&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="middle"&gt;Valid&lt;/td&gt;
&lt;td&gt;property (object)&lt;/td&gt;
&lt;td&gt;perform validation recursively on the associated object. If the object is a Collection or an array, the elements are validated recursively. If the object is a Map, the value elements are validated recursively.&lt;/td&gt;
&lt;td&gt;none&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="middle"&gt;Email&lt;/td&gt;
&lt;td&gt;property (String)&lt;/td&gt;
&lt;td&gt;check whether the string conforms to the email address specification&lt;/td&gt;
&lt;td&gt;none&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="middle"&gt;Digits&lt;/td&gt;
&lt;td&gt;property (numeric or string representation of a numeric)&lt;/td&gt;
&lt;td&gt;checks whether the property is a number having up to &amp;lt;tt class=&amp;quot;literal&amp;quot;&amp;gt;integerDigits&amp;lt;/tt&amp;gt; integer digits and &amp;lt;tt class=&amp;quot;literal&amp;quot;&amp;gt;fractionalDigits&amp;lt;/tt&amp;gt; fractonal digits&lt;/td&gt;
&lt;td&gt;define column precision and scale&lt;/td&gt;
&lt;/tr&gt;
&amp;lt;/tbody&amp;gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 style="clear:both;" class="title"&gt;Error messages&lt;/h2&gt;
&lt;p&gt;NHibernate Validator comes with a default set of error messages translated in several languages (if yours is not present, please send us a patch). You can add your own additional set of messages while writing your validator Attributes or at XML. If NHibernate Validator cannot resolve a key from your ResourceManager nor from ValidatorMessage, it falls back to the default built-in values.&lt;/p&gt;
&lt;p&gt;Alternatively you can provide a &amp;lt;tt class=&amp;quot;classname&amp;quot;&amp;gt;ResourceManager&amp;lt;/tt&amp;gt; while checking programmatically the validation rules on a bean or if you want a completly different interpolation mechanism, you can provide an implementation of &amp;lt;tt class=&amp;quot;literal&amp;quot;&amp;gt;NHibernate.Validator.Engine.IMessageInterpolator&amp;lt;/tt&amp;gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 style="clear:both;" class="title"&gt;Writing your own constraints&lt;/h2&gt;
&lt;p&gt;Extending the set of built-in constraints is extremely easy. Any constraint consists of two pieces: the constraint &lt;span class="emphasis"&gt;&lt;em&gt;descriptor&lt;/em&gt;&lt;/span&gt; (the attribute) and the constraint &lt;span class="emphasis"&gt;&lt;em&gt;validator&lt;/em&gt;&lt;/span&gt; (the implementation class). Here is a simple user-defined descriptor:&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
[ValidatorClass(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt;(PhoneValidator))]
&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; PhoneAttribute : Attribute, IRuleArgs
{
    &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; message = &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt;.Empty;
    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Message
    {
        get { &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; message; }
        set { message = &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;; }
    }
}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;To link a descriptor to its validator implementation, we use the &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;tt&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;class=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;literal&amp;quot;&amp;gt;[ValidatorClass]&amp;lt;/tt&amp;gt;&lt;/span&gt; &lt;em&gt;&lt;span style="background: SpringGreen;"&gt;[&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;ValidatorClass]&lt;/span&gt;&lt;/em&gt;&amp;nbsp;meta-attribute. Here you can see an implementation of a constraint validator:&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; PhoneValidator : IValidator
{
     &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; IsValid(&lt;span style="color:#0000ff;"&gt;object&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;)
     {
         Regex regex = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Regex(&lt;span style="color:#006080;"&gt;@&amp;quot;^[2-9]\d{2}-\d{3}-\d{4}$&amp;quot;&lt;/span&gt;);
         &lt;span style="color:#0000ff;"&gt;if&lt;/span&gt; (&lt;span style="color:#0000ff;"&gt;value&lt;/span&gt; == &lt;span style="color:#0000ff;"&gt;null&lt;/span&gt;) &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;;
         &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; regex.IsMatch(&lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;.ToString());
     }
}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;This PhoneValidator class is simple. The method &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;tt&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;class=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;literal&amp;quot;&amp;gt;IsValid()&amp;lt;/tt&amp;gt;&lt;/span&gt; &lt;em&gt;&lt;span style="background: SpringGreen;"&gt;IsValid(&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;)&lt;/span&gt;&lt;/em&gt;&amp;nbsp;receives a string that should be validated with a Regex expression. The &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;tt&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;class=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;literal&amp;quot;&amp;gt;IsValid()&amp;lt;/tt&amp;gt;&lt;/span&gt; &lt;em&gt;&lt;span style="background: SpringGreen;"&gt;IsValid(&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;)&lt;/span&gt;&lt;/em&gt;&amp;nbsp;method should return false if the constraint has been violated.&lt;/p&gt;
&lt;p&gt;If you need to initialize the class validator you can implement the interface &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;tt&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;class=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;literal&amp;quot;&amp;gt;IInitializableValidator&amp;lt;A&amp;gt;&amp;lt;/tt&amp;gt;&lt;/span&gt; &lt;em&gt;&lt;span style="background: SpringGreen;"&gt;IInitializableValidator&lt;/span&gt;&lt;/em&gt;&lt;em&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;A&amp;gt;&amp;nbsp;&lt;/span&gt;&lt;/em&gt;and the class should look like this:&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; PhoneValidator : IInitializableValidator&amp;lt;PhoneAttribute&amp;gt;
{
    &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; Regex regex;

    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; IsValid(&lt;span style="color:#0000ff;"&gt;object&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;)
    {
        &lt;span style="color:#0000ff;"&gt;if&lt;/span&gt; (&lt;span style="color:#0000ff;"&gt;value&lt;/span&gt; == &lt;span style="color:#0000ff;"&gt;null&lt;/span&gt;) &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;;
        &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; regex.IsMatch(&lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;.ToString());
    }

    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Initialize(PhoneAttribute parameters)
    {
        regex = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Regex(&lt;span style="color:#006080;"&gt;@&amp;quot;^[2-9]\d{2}-\d{3}-\d{4}$&amp;quot;&lt;/span&gt;);
    }
}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;As you can see, implementing the &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;tt&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;class=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;literal&amp;quot;&amp;gt;IInitializableValidator&amp;lt;A&amp;gt;&amp;lt;/tt&amp;gt;&lt;/span&gt; &lt;em&gt;&lt;span style="background: SpringGreen;"&gt;IInitializableValidator&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;A&amp;gt;&lt;/span&gt;&lt;/em&gt;&amp;nbsp;interface we have to implement the method &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;tt&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;class=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;methodname&amp;quot;&amp;gt;Initialize(...)&amp;lt;/tt&amp;gt;&lt;/span&gt; &lt;em&gt;&lt;span style="background: SpringGreen;"&gt;Initialize(...&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;)&lt;/span&gt;&lt;/em&gt; that initializes the validator, and it&amp;#39;s executed first of all.&lt;/p&gt;
&lt;p&gt;The &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;tt&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;class=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;literal&amp;quot;&amp;gt;IsValid()&amp;lt;/tt&amp;gt;&lt;/span&gt; &lt;em&gt;&lt;span style="background: SpringGreen;"&gt;IsValid(&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;)&lt;/span&gt;&lt;/em&gt;&amp;nbsp;method should return &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;tt&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;class=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;literal&amp;quot;&amp;gt;false&amp;lt;/tt&amp;gt;&lt;/span&gt; &lt;em&gt;&lt;span style="background: SpringGreen;"&gt;false&lt;/span&gt;&lt;/em&gt;&amp;nbsp;if the constraint has been violated. For more examples, refer to the built-in validator implementations.&lt;/p&gt;
&lt;p&gt;We now have to implement the validator (ie. the rule checking implementation). A validation implementation can check the value of the a property (by implementing &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;tt&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;class=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;literal&amp;quot;&amp;gt;PropertyConstraint&amp;lt;/tt&amp;gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;PropertyConstraint&lt;/span&gt; ) and/or can modify the NHibernate mapping metadata to express the constraint at the database level (by implementing &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;tt&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;class=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;literal&amp;quot;&amp;gt;PersistentClassConstraint&amp;lt;/tt&amp;gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;PersistentClassConstraint&lt;/span&gt;)&lt;/p&gt;
&lt;p&gt;We have only seen property level validation, but you can write a class level validation attribute. Instead of receiving the return instance of a property, the bean itself will be passed to the validator. To activate the validation checking, just put the validation attribute to the class itself instead of in a property. A small sample can be found in the unit test suite.&lt;/p&gt;
&lt;p&gt;If your constraint can be applied multiple times (with different parameters) on the same property or type, you can use set the &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;tt&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;class=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;literal&amp;quot;&amp;gt;AllowMultiple&amp;lt;/tt&amp;gt;&lt;/span&gt; &lt;em&gt;&lt;span style="background: SpringGreen;"&gt;AllowMultiple&lt;/span&gt;&lt;/em&gt;&amp;nbsp;parameter to &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;tt&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;class=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;literal&amp;quot;&amp;gt;true&amp;lt;/tt&amp;gt;&lt;/span&gt; &lt;em&gt;&lt;span style="background: SpringGreen;"&gt;true&lt;/span&gt;&lt;/em&gt;&amp;nbsp;on the &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;tt&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;class=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;literal&amp;quot;&amp;gt;AttributeUsage&amp;lt;/tt&amp;gt;&lt;/span&gt; &lt;em&gt;&lt;span style="background: SpringGreen;"&gt;AttributeUsage&lt;/span&gt;&lt;/em&gt;&amp;nbsp;attribute. An example of this it&amp;#39;s the built-in validator Pattern, have a look:&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;[Serializable]
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = &lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;)]
[ValidatorClass(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt; (PatternValidator))]
&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; PatternAttribute : Attribute, IRuleArgs
{
    &amp;hellip;
}&lt;/pre&gt;
&lt;/div&gt;
&lt;pre class="programlisting"&gt;&amp;nbsp;&lt;/pre&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 style="clear:both;" class="title"&gt;Configuring your domain model with .Net Attributes&lt;/h2&gt;
&lt;p&gt;Since you are already familiar with attributes now, the syntax should be very familiar&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; Address
{ 
    &lt;span style="color:#008000;"&gt;// a static non null field  &lt;/span&gt;
    [NotNull]
    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;static&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; blacklistedZipCode;
    
    &lt;span style="color:#008000;"&gt;// field a not null string of 20 characters maximum&lt;/span&gt;
    [Length(Max = 20), NotNull]    
    &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; country;
    &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;long&lt;/span&gt; id;
    &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; internalValid = &lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;;
    &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; line1;
    &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; line2;
    &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; state;
    &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; zip;
    
    &lt;span style="color:#008000;"&gt;// a numeric between 1 and 2000&lt;/span&gt;
    [Min(1), Range(Max = 2000)]
    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;long&lt;/span&gt; Id
    {
        get { &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; id; }
        set { id = &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;; }
    }

    &lt;span style="color:#008000;"&gt;// respond to the constraint in the field: country&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Country
    {
        get { &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; country; }
        set { country = &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;; }
    }

    &lt;span style="color:#008000;"&gt;// a non null string&lt;/span&gt;
    [NotNull]
    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Line1
    {
        get { &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; line1; }
        set { line1 = &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;; }
    }

    &lt;span style="color:#008000;"&gt;// a not null string of 3 characters maximum&lt;/span&gt;
    [Length(Max = 3), NotNull]
    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; State
    {
        get { &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; state; }
        set { state = &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;; }
    }

    &lt;span style="color:#008000;"&gt;// a not null numeric string of 5 characters maximum&lt;/span&gt;
    &lt;span style="color:#008000;"&gt;// if the string is longer, the message will&lt;/span&gt;
    &lt;span style="color:#008000;"&gt;// be searched in the resource bundle at key &amp;#39;long&amp;#39;&lt;/span&gt;
    [Length(Max = 5, Message = &lt;span style="color:#006080;"&gt;&amp;quot;{long}&amp;quot;&lt;/span&gt;)]
    [Pattern(Regex = &lt;span style="color:#006080;"&gt;&amp;quot;[0-9]+&amp;quot;&lt;/span&gt;)]
    [NotNull]
    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Zip
    {
        get { &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; zip; }
        set { zip = &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;; }
    }

    &lt;span style="color:#008000;"&gt;// no constraint&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Line2
    {
        get { &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; line2; }
        set { line2 = &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;; }
    }

    &lt;span style="color:#008000;"&gt;// should always be true&lt;/span&gt;
    [AssertTrue]
    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; InternalValid
    {
        get { &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; internalValid; }
        set { internalValid = &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;; }
    }
} &lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;While the example only shows public property validation, you can also annotate fields of any kind of visibility. In the example &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;tt&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;class=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;classname&amp;quot;&amp;gt;MyBeanConstraint&amp;lt;/tt&amp;gt;&lt;/span&gt; &lt;strong&gt;&lt;span style="background: SpringGreen;"&gt;MyBeanConstraint&lt;/span&gt;&lt;/strong&gt; is a custom Class Validator, we will talk about this later.&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;[MyBeanConstraint(Max=45)]
&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; Dog 
{
    [AssertTrue] &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; IsMale;
    [NotNull] &lt;span style="color:#0000ff;"&gt;protected&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Name { get {...} set{...} }
    &amp;hellip;
}     &lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;You can also annotate interfaces. NHibernate Validator will check all superclasses and interfaces extended or implemented by a given type to read the appropriate validator attributes.&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;interface&lt;/span&gt; Named 
{
    [NotNull] &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Name {get; set;}
    ...
}

&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; Dog : Named 
{
    [AssertTrue] &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; isMale;
    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Name { get {...} set{...} }
}
&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;The name property will be checked for nullity when the Dog type is validated.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 style="clear:both;" class="title"&gt;Configuring your domain model with XML&lt;/h2&gt;
&lt;p&gt;You also can configurate your domain model with constraints using xml files: the nhv.xml files. We prefer to embed these Xml files into the assemblies, and NHibernate Validator is prepared to inspect them and retrieve the resources with constrains, like NHibernate does with hbm.xml files.&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#800000;"&gt;xml&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;version&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;1.0&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;encoding&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;utf-8&amp;quot;&lt;/span&gt; ?&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;nhv-mapping&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;urn:nhibernate-validator-1.0&amp;quot;&lt;/span&gt;
                        &lt;span style="color:#ff0000;"&gt;namespace&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;NHibernate.Validator.Tests.Base&amp;quot;&lt;/span&gt;
                        &lt;span style="color:#ff0000;"&gt;assembly&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;NHibernate.Validator.Tests&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;class&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Address&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;blacklistedZipCode&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
                  &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;not-null&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;country&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
           &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;length&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;max&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;20&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
           &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;not-null&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;    
    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;floor&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;    
          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;range&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;min&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;-2&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;max&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;50&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;message&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;{floor.out.of.range} (escaping #{el})&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Id&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;    
          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;min&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;value&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;1&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;range&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;max&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;2000&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;max&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;value&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;2500&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Line1&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;    
          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;not-null&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;State&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;    
          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;not-null&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;length&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;max&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;3&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;

    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Zip&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;length&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;max&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;5&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;message&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;{long}&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;pattern&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;regex&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;[0-9]+&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;not-null&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;InternalValid&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;    
          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;asserttrue&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;nhv-mapping&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;      &lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;span class="bold"&gt;&lt;strong&gt;We strongly recommend&lt;/strong&gt;&lt;/span&gt; to validate every nhv.xml file of your application with the respective XSD file. Every &lt;strong&gt;nhv.xml&lt;/strong&gt; file should be validated with the &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;tt&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;class=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;literal&amp;quot;&amp;gt;nhv-mapping.xsd&amp;lt;/tt&amp;gt;&lt;/span&gt; &lt;strong&gt;&lt;span style="background: SpringGreen;"&gt;nhv-mapping.xsd&lt;/span&gt;&lt;/strong&gt;&amp;nbsp;file included within the NHibernate Validator distribution whether in source or binaries. Indeed, if you are using an IDE like Visual Studio it offers IntelliSense support in the &lt;strong&gt;nhv.xml&lt;/strong&gt; file if you add the schema to Visual Studio&amp;#39;s xml schemas folder.&lt;/p&gt;
&lt;p&gt;&lt;span class="bold"&gt;&lt;strong&gt;Xml beauties:&lt;/strong&gt;&lt;/span&gt; The nicest part of Xml for validation is that you can put xml validators in a different dll so you don&amp;#39;t need to recompile your domain when validation rules change. Only the dll with the Xml files..&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class="chapter"&gt;
&lt;h1&gt;Configuration&lt;/h1&gt;
&lt;p&gt;NHibernate Validator has global parameters that you can configure at the startup. This configuration can be done in differents ways: programmaticaly or using xml files. If you are involved with NHibernate this could be very familiar to you.&lt;/p&gt;
&lt;p&gt;Configuration properties:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Table Configuration properties&lt;/strong&gt;&lt;/p&gt;
&lt;div class="table"&gt;
&lt;table&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;colgroup&amp;gt;&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;col&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;align=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;middle&amp;quot;&amp;gt;&amp;lt;/col&amp;gt;&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;col&amp;gt;&amp;lt;/col&amp;gt;&amp;lt;/colgroup&amp;gt;&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;thead&amp;gt;&lt;/span&gt;
&lt;/strong&gt;&lt;/p&gt;
&lt;div class="table"&gt;&lt;br /&gt;
&lt;table&gt;
&lt;span style="background: SpringGreen;"&gt;&amp;lt;tbody&amp;gt;&lt;/span&gt;
&lt;tr&gt;
&lt;th align="middle"&gt;Property name&lt;/th&gt;
&lt;th&gt;Purpose&lt;/th&gt;
&lt;/tr&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;/thead&amp;gt;&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;tbody&amp;gt;&lt;/span&gt;
&lt;tr&gt;
&lt;td align="middle"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;tt&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;class=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;literal&amp;quot;&amp;gt;&lt;/span&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="middle"&gt;&lt;strong&gt;apply_to_ddl&lt;/strong&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;/tt&amp;gt;&lt;/span&gt;&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Configure NHibernate to apply the constraints to the DDL.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="middle"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;tt&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;class=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;literal&amp;quot;&amp;gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="middle"&gt;&lt;strong&gt;autoregister_listeners&lt;/strong&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;/tt&amp;gt;&lt;/span&gt;&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Register the Event-Listeners automatically on Inserts and Updates.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="middle"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;tt&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;class=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;literal&amp;quot;&amp;gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="middle"&gt;&lt;strong&gt;default_validator_mode&lt;/strong&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;/tt&amp;gt;&lt;/span&gt;&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Define what are the constraints sources are and how to proceed. There are some built-in configurations:
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;div class="itemizedlist"&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;tt&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;class=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;literal&amp;quot;&amp;gt;UseAttribute&amp;lt;/tt&amp;gt;&lt;/span&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;div class="itemizedlist"&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;span style="background: SpringGreen;"&gt;UseAttribute&lt;/span&gt;&lt;/strong&gt; (&lt;span style="text-decoration: line-through; color: red;"&gt;by&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;default)&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;default):&lt;/span&gt; Use Attributes to configurate the validators on types and/or members. Xml is ignored.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;tt&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;class=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;literal&amp;quot;&amp;gt;UseXml&amp;lt;/tt&amp;gt;:&lt;/span&gt; &lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;span style="background: SpringGreen;"&gt;UseXml&lt;/span&gt;&lt;/strong&gt;&lt;span style="background: SpringGreen;"&gt;:&lt;/span&gt; Use Xml to configurate the validators on types and/or members. Attributes are ignored.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;tt&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;class=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;literal&amp;quot;&amp;gt;OverrideAttributeWithXml&amp;lt;/tt&amp;gt;:&lt;/span&gt; &lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;span style="background: SpringGreen;"&gt;OverrideAttributeWithXml&lt;/span&gt;&lt;/strong&gt;&lt;span style="background: SpringGreen;"&gt;:&lt;/span&gt; If the same type/member is annotated with an Attribute and has a Xml rule the Attribute annotation will be used.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;tt&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;class=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;literal&amp;quot;&amp;gt;OverrideXmlWithAttribute&amp;lt;/tt&amp;gt;:&lt;/span&gt; &lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;span style="background: SpringGreen;"&gt;OverrideXmlWithAttribute&lt;/span&gt;&lt;/strong&gt;&lt;span style="background: SpringGreen;"&gt;:&lt;/span&gt; If the same type/member is annotated with an Attribute and has a Xml rule the Attribute annotation will be used.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="middle"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;tt&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;class=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;literal&amp;quot;&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="middle"&gt;&lt;strong&gt;assembly&lt;/strong&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;/tt&amp;gt;&lt;/span&gt;&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Which are the assemblies with contains the nhv.xml files embedded. You can have more than one assembly.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="middle"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;tt&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;class=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;literal&amp;quot;&amp;gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="middle"&gt;&lt;strong&gt;shared_engine_provider&lt;/strong&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;/tt&amp;gt;&lt;/span&gt;&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;To inject a custom Shared Engine Provider.&lt;/td&gt;
&lt;/tr&gt;
&amp;lt;/tbody&amp;gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 style="clear:both;" class="title"&gt;Programmatic configuration&lt;/h2&gt;
&lt;p&gt;You can use the following code to configure NHibernate.Validator programmatically:&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NHibernate.Validator.Cfg;
&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NHibernate.Validator.Engine;
&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; Environment=NHibernate.Validator.Cfg.Environment;
&amp;hellip;
ValidatorEngine ve = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ValidatorEngine();

NHVConfiguration nhvc = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; NHVConfiguration();
nhvc.Properties[Environment.ApplyToDDL] = &lt;span style="color:#006080;"&gt;&amp;quot;false&amp;quot;&lt;/span&gt;;
nhvc.Properties[Environment.AutoregisterListeners] = &lt;span style="color:#006080;"&gt;&amp;quot;false&amp;quot;&lt;/span&gt;;
nhvc.Properties[Environment.ValidatorMode] = GetMode();
nhvc.Mappings.Add(&lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; MappingConfiguration(&lt;span style="color:#006080;"&gt;&amp;quot;NHibernate.Validator.Demo.Model&amp;quot;&lt;/span&gt;,&lt;span style="color:#0000ff;"&gt;null&lt;/span&gt;));
ve.Configure(nhvc);  &lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;The &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;tt&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;class=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;literal&amp;quot;&amp;gt;ValidatorEngine&amp;lt;/tt&amp;gt;&lt;/span&gt; &lt;em&gt;&lt;span style="background: SpringGreen;"&gt;ValidatorEngine&lt;/span&gt;&lt;/em&gt;&amp;nbsp;will be set with the configuration. The NHibernate Validator configuration is represented by the &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;tt&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;class=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;literal&amp;quot;&amp;gt;NHibernate.Validator.Cfg.NHVConfiguration&amp;lt;/tt&amp;gt;&lt;/span&gt; &lt;em&gt;&lt;span style="background: SpringGreen;"&gt;NHibernate.Validator.Cfg.NHVConfiguration&lt;/span&gt;&lt;/em&gt;&amp;nbsp;type. Other class used to configure all the stuff is the &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;tt&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;class=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;literal&amp;quot;&amp;gt;NHibernate.Validator.Cfg.Environment&amp;lt;/tt&amp;gt;&lt;/span&gt; &lt;em&gt;&lt;span style="background: SpringGreen;"&gt;NHibernate.Validator.Cfg.Environment&lt;/span&gt;&lt;/em&gt;&amp;nbsp;type, this class contain the string with the names of the properties of the configuration of NHibernate Validator.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 style="clear:both;" class="title"&gt;Declarative configuration: using Xml files&lt;/h2&gt;
&lt;p&gt;The declarative configuration keeps configuration out of the code. This means configuring the application using a different file, in this case a XML file. But the options don&amp;#39;t end here: you can choose to configure NHibernate Validator using a simple Xml file or you can put the configuration in the well known App.Config/Web.Config. &lt;/p&gt;
&lt;div class="section"&gt;
&lt;h3 class="title"&gt;Simple xml file&lt;/h3&gt;
&lt;p&gt;This is a sample of a simple configuration in a xml file:&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#800000;"&gt;xml&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;version&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;1.0&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;encoding&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;utf-8&amp;quot;&lt;/span&gt; ?&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;nhv-configuration&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;#39;urn:nhv-configuration-1.0&amp;#39;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
  &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;#39;apply_to_ddl&amp;#39;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;true&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
  &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;#39;autoregister_listeners&amp;#39;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;true&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
  &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;#39;default_validator_mode&amp;#39;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;UseXml&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
  &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;mapping&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;assembly&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;#39;NHibernate.Validator.Demo.Model&amp;#39;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;nhv-configuration&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;We encourage to name the configuration file &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;tt&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;class=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;literal&amp;quot;&amp;gt;nhvalidator.cfg.xml&amp;lt;/tt&amp;gt;,&lt;/span&gt; &lt;strong&gt;&lt;span style="background: SpringGreen;"&gt;nhvalidator.cfg.xml&lt;/span&gt;&lt;/strong&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; because NHibernate Validator will pick up this file from the application directory in order to get the configuration parameters, however you can name this file as you wish.&lt;/p&gt;
&lt;p&gt;In order to configurate NHibernate Validator with the &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;tt&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;class=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;literal&amp;quot;&amp;gt;nhvalidator.cfg.xml&amp;lt;/tt&amp;gt;&lt;/span&gt; &lt;strong&gt;&lt;span style="background: SpringGreen;"&gt;nhvalidator.cfg.xml&lt;/span&gt;&lt;/strong&gt;&amp;nbsp;you can use the following codeו:&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;ValidatorEngine engine = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ValidatorEngine();
engine.Configure(); &lt;span style="color:#008000;"&gt;// by convention reads the nhvalidator.cfg.xml file.&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;You can choose another name for your configuration file and proceed of this manner: &lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;ValidatorEngine engine = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ValidatorEngine();
engine.Configure(&lt;span style="color:#006080;"&gt;&amp;quot;path/to/MyNHVConfigurationFile.xml&amp;quot;&lt;/span&gt;); &lt;span style="color:#008000;"&gt;// you can use whatever path to point to the configuration file&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h3 class="title"&gt;Configuration Application file: app.config/web.config&lt;/h3&gt;
&lt;p&gt;The configuration can be declared into our configuration application .Net files: app.config or web.config. In the below example you will see how to do it.&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#800000;"&gt;xml&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;version&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;1.0&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;encoding&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;utf-8&amp;quot;&lt;/span&gt; ?&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;configuration&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;configSections&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
        &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;section&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;nhv-configuration&amp;quot;&lt;/span&gt; 
                            &lt;span style="color:#ff0000;"&gt;type&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;NHibernate.Validator.Cfg.ConfigurationSectionHandler, NHibernate.Validator&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
        ...
    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;configSections&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    ...
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;nhv-configuration&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;#39;urn:nhv-configuration-1.0&amp;#39;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
        &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;shared_engine_provider&lt;/span&gt; 
         &lt;span style="color:#ff0000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;#39;NHibernate.Validator.Event.NHibernateSharedEngineProvider, NHibernate.Validator&amp;#39;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;nhv-configuration&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    ...
&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;configuration&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;      &lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;</description></item><item><title>NHibernate Validator 1.0.0</title><link>http://nhforge.org/wikis/validator/nhibernate-validator-1-0-0-documentation/revision/1.aspx</link><pubDate>Sat, 13 Sep 2008 05:04:29 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:102</guid><dc:creator>Fabio Maulo</dc:creator><description>Revision 1 posted to Validator by Fabio Maulo on 13/09/2008 02:04:29 a.m.&lt;br /&gt;
&lt;h1&gt;Preface&lt;/h1&gt;
&lt;div class="preface"&gt;
&lt;p&gt;Attributes are a very convenient and elegant way to specify invariant constraints for a domain model. You can, for example, express that a property should never be null, that the account balance should be strictly positive, etc. These domain model constraints are declared in the class itself by annotating its properties. A validator can then read them and check for constraint violations. The validation mechanism can be executed in different layers in your application without having to duplicate any of these rules (presentation layer, data access layer). Following the DRY principle, NHibernate Validator has been designed for that purpose. NHibernate Validator also lets you declare the same rules in XML files. Both approaches (attributes and XML) give us interesting possibilities for configuring our domain rules. &lt;/p&gt;
&lt;p&gt;NHibernate Validator works on two levels. First, it is able to check in-memory instances of a class for constraint violations. Second, it can apply the constraints to the NHibernate metamodel and incorporate them into the generated database schema.&lt;/p&gt;
&lt;p&gt;Each constraint attribute is associated to a validator implementation responsible for checking the constraint on the entity instance. A validator can also (optionally) apply the constraint to the NHibernate metamodel, allowing NHibernate to generate DDL that expresses the constraint. With the appropriate event listener, you can execute the checking operation on inserts and updates done by NHibernate. NHibernate Validator is not limited to only be used with NHibernate. You can easily use it anywhere in your application as well, indeed, you can use it at UI validation with Winforms or Asp.Net technologies.&lt;/p&gt;
&lt;p&gt;When checking instances at runtime, NHibernate Validator returns information about constraint violations in an array of &amp;lt;tt class=&amp;quot;classname&amp;quot;&amp;gt;InvalidValue&amp;lt;/tt&amp;gt; s. Among other information, the &amp;lt;tt class=&amp;quot;classname&amp;quot;&amp;gt;InvalidValue&amp;lt;/tt&amp;gt; contains an error description message that can embed the parameter values bundle with the annotation (eg. length limit), and message strings that may be externalized to a &amp;lt;tt class=&amp;quot;classname&amp;quot;&amp;gt;ResourceManager&amp;lt;/tt&amp;gt; .&lt;/p&gt;
&lt;/div&gt;
&lt;div class="chapter"&gt;
&lt;h1&gt;About the project&lt;/h1&gt;
&lt;p&gt;NHibernate Validator is a powerful and extensible framework to validate objects using the .Net Platform. Born as a port of &lt;span class="italic"&gt;Hibernate Validator 3.0.0&lt;/span&gt; project at december of 2007. Later on, many improvements and changes were made to the Core and API, and now we can say that NHibernate Validator is more than just a port. NHibernate Validator is entirely written in C#. The first release of NHibernate Validator was entirely developed by &lt;span class="italic"&gt;Dario Quintana&lt;/span&gt;, &lt;span class="italic"&gt;Gustavo Ringel&lt;/span&gt; and &lt;span class="italic"&gt;Fabio Maulo&lt;/span&gt;. We appreciate the help of &lt;span class="italic"&gt;Davy Brion&lt;/span&gt; in the revision of this document.&lt;/p&gt;
&lt;p&gt;NHibernate Validator is part of the &lt;a href="http://sourceforge.net/projects/nhcontrib/" target="_top"&gt;NHibernate Contrib&lt;/a&gt; project and you can download it &lt;a href="http://sourceforge.net/project/showfiles.php?group_id=216446&amp;amp;package_id=275108&amp;amp;release_id=597674" target="_top"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;You can ask questions about the development and utilization of NHibernate Validator on the following mailing lists:&lt;/p&gt;
&lt;p&gt;The development mailing list can be found &lt;a href="http://groups.google.com/group/nhcdevs" target="_top"&gt;here&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;The user forum can be found &lt;a href="http://groups.google.com/group/nhusers" target="_top"&gt;here&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;Bug and issues report should be sent to the development mailing list.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="chapter"&gt;
&lt;h1&gt;Using the Validator framework&lt;/h1&gt;
&lt;p&gt;NHibernate Validator is intended to be used to implement multi-layered data validation, where constraints are expressed in a single place and checked in various different layers of the application.&lt;/p&gt;
&lt;p&gt;This chapter will cover NHibernate Validator usage for different layers&lt;/p&gt;
&lt;div class="section"&gt;
&lt;h2 class="title" style="clear:both;"&gt;Database schema-level validation&lt;/h2&gt;
&lt;p&gt;Out of the box, NHibernate Validator will translate the constraints you have defined for your entities into mapping metadata. For example, if a property of your entity has the attribute &amp;lt;tt class=&amp;quot;literal&amp;quot;&amp;gt;[NotNull]&amp;lt;/tt&amp;gt;, its columns will be declared as &amp;lt;tt class=&amp;quot;literal&amp;quot;&amp;gt;not null&amp;lt;/tt&amp;gt; in the DDL schema generated by NHibernate.&lt;/p&gt;
&lt;p&gt;Using hbm2ddl, domain model constraints will be expressed into the database schema.&lt;/p&gt;
&lt;p&gt;If, for some reason, the feature needs to be disabled, set &amp;lt;tt class=&amp;quot;literal&amp;quot;&amp;gt;apply_to_ddl&amp;lt;/tt&amp;gt; to &amp;lt;tt class=&amp;quot;literal&amp;quot;&amp;gt;false&amp;lt;/tt&amp;gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 class="title" style="clear:both;"&gt;ORM integration&lt;/h2&gt;
&lt;p&gt;NHibernate Validator integrates with NHibernate in a native way, and knows how to treat every persistent entity.&lt;/p&gt;
&lt;div class="section"&gt;
&lt;h3 class="title"&gt;NHibernate event-based validation&lt;/h3&gt;
&lt;p&gt;NHibernate Validator has two built-in NHibernate event listeners. Whenever a &amp;lt;tt class=&amp;quot;literal&amp;quot;&amp;gt;PreInsertEvent&amp;lt;/tt&amp;gt; or &amp;lt;tt class=&amp;quot;literal&amp;quot;&amp;gt;PreUpdateEvent&amp;lt;/tt&amp;gt; occurs, the listeners will verify all constraints of the entity instance and throw an exception if any of them are violated. Basically, objects will be checked before any insert and before any update triggered by NHibernate. This includes cascading changes! This is the most convenient and easiest way to activate the validation process. If a constraint is violated, the event will raise a runtime &amp;lt;tt class=&amp;quot;classname&amp;quot;&amp;gt;InvalidStateException&amp;lt;/tt&amp;gt; which contains an array of &amp;lt;tt class=&amp;quot;literal&amp;quot;&amp;gt;InvalidValue&amp;lt;/tt&amp;gt;s describing each failure.&lt;/p&gt;
&lt;p&gt;If, for some reason, you want to disable this integration, set &amp;lt;tt class=&amp;quot;literal&amp;quot;&amp;gt;autoregister_listeners&amp;lt;/tt&amp;gt; to &amp;lt;tt class=&amp;quot;literal&amp;quot;&amp;gt;false&amp;lt;/tt&amp;gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&amp;lt;tt class=&amp;quot;literal&amp;quot;&amp;gt;&amp;lt;/tt&amp;gt;&lt;span style="text-decoration:underline;"&gt;Note&lt;/span&gt;: If the entities are not annotated with validation attributes, there is no runtime performance cost.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;In case you wish to set manually the event listeners for NHibernate Core, use the following configuration in &amp;lt;tt class=&amp;quot;literal&amp;quot;&amp;gt;hibernate.cfg.xml&amp;lt;/tt&amp;gt;:&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    ...
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;event&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;type&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;pre-update&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
        &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;listener&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;NHibernate.Validator.Event.ValidatePreUpdateEventListener&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;event&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;event&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;type&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;pre-insert&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
        &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;listener&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;NHibernate.Validator.Event.ValidatePreInsertEventListener&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;event&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 class="title" style="clear:both;"&gt;Application-level validation&lt;/h2&gt;
&lt;p&gt;In order to interact with NHibernate Validator in your application you must use the main class: &amp;lt;tt class=&amp;quot;classname&amp;quot;&amp;gt;ValidatorEngine&amp;lt;/tt&amp;gt;. &lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;ValidatorEngine validator = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ValidatorEngine();
&lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; isValid = validator.IsValid(customer);&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;These two lines are enough to validate an object with its validation rules, which can be configured using Attributes or XML. The first line is the instantiation of the validator engine of NHibernate Validator. The second line is the validation of an object, and the result of the &amp;lt;tt class=&amp;quot;methodname&amp;quot;&amp;gt;IsValid(...)&amp;lt;/tt&amp;gt; method indicates if the object is valid or not. In order to get the invalid values of an object, you can do:&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;ValidatorEngine validator = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ValidatorEngine();
InvalidValue[] validationMessages = validator.GetInvalidValues(customer);&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Now instead of only knowing if the object is valid or not, we get an array of InvalidValues. If this array length is greater than zero then there are validation errors and you can traverse the InvalidValue array to get verbose information about the problems that cause the object to be invalid. &lt;/p&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 class="title" style="clear:both;"&gt;Presentation layer validation&lt;/h2&gt;
&lt;p&gt;[&lt;strong&gt;&lt;span style="color:#008000;"&gt;TODO&lt;/span&gt;&lt;/strong&gt;]&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 class="title" style="clear:both;"&gt;Validation informations&lt;/h2&gt;
&lt;p&gt;NHibernate Validator provides an array of &amp;lt;tt class=&amp;quot;classname&amp;quot;&amp;gt;InvalidValue&amp;lt;/tt&amp;gt;. Each &amp;lt;tt class=&amp;quot;literal&amp;quot;&amp;gt;InvalidValue&amp;lt;/tt&amp;gt; has several methods describing the reasons that caused the entity to be invalid.&lt;/p&gt;
&lt;p&gt;&amp;lt;tt class=&amp;quot;methodname&amp;quot;&amp;gt;BeanClass&amp;lt;/tt&amp;gt; retrieves the failing bean type&lt;/p&gt;
&lt;p&gt;&amp;lt;tt class=&amp;quot;methodname&amp;quot;&amp;gt;Bean&amp;lt;/tt&amp;gt;retrieves the failing instance (if any ie not when using &amp;lt;tt class=&amp;quot;methodname&amp;quot;&amp;gt;GetPotentialInvalidValues()&amp;lt;/tt&amp;gt;)&lt;/p&gt;
&lt;p&gt;&amp;lt;tt class=&amp;quot;methodname&amp;quot;&amp;gt;Value&amp;lt;/tt&amp;gt; retrieves the failing value&lt;/p&gt;
&lt;p&gt;&amp;lt;tt class=&amp;quot;methodname&amp;quot;&amp;gt;Message&amp;lt;/tt&amp;gt; retrieves the proper internationalized error message&lt;/p&gt;
&lt;p&gt;&amp;lt;tt class=&amp;quot;methodname&amp;quot;&amp;gt;RootBean&amp;lt;/tt&amp;gt; retrieves the root bean instance generating the issue (useful in conjunction with &amp;lt;tt class=&amp;quot;literal&amp;quot;&amp;gt;[Valid]&amp;lt;/tt&amp;gt;), is null if GetPotentialInvalidValues(...) is used.&lt;/p&gt;
&lt;p&gt;&amp;lt;tt class=&amp;quot;literal&amp;quot;&amp;gt;PropertyPath&amp;lt;/tt&amp;gt; retrieves the dotted path of the failing property starting from the root bean&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class="chapter"&gt;
&lt;h1&gt;Defining constraints&lt;/h1&gt;
&lt;div class="section"&gt;
&lt;h2 class="title" style="clear:both;"&gt;What is a constraint?&lt;/h2&gt;
&lt;p&gt;A constraint is a rule that a given element (field, property or type) has to comply to. The rule semantic is expressed by an Attribute or configured in Xml files. A constraint may have some properties to configure the associated validator, for example a Min constraint will have a value property setting the minimum acceptable value for the element to be valid. &lt;/p&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 class="title" style="clear:both;"&gt;Built in constraints&lt;/h2&gt;
&lt;p&gt;Hibernate Validator comes with some built-in constraints, which cover most of the basic data checks. As we&amp;#39;ll see later, you&amp;#39;re not limited to them, you can literally in a minute write your own constraints.&lt;/p&gt;
&lt;div class="table"&gt;
&lt;p&gt;&lt;strong&gt;Table Built-in constraints&lt;/strong&gt;&lt;/p&gt;
&lt;table&gt;
&amp;lt;colgroup&amp;gt;
&amp;lt;col align=&amp;quot;middle&amp;quot;&amp;gt;&amp;lt;/col&amp;gt;
&amp;lt;col&amp;gt;&amp;lt;/col&amp;gt;
&amp;lt;col&amp;gt;&amp;lt;/col&amp;gt;
&amp;lt;col&amp;gt;&amp;lt;/col&amp;gt;&amp;lt;/colgroup&amp;gt;
&amp;lt;thead&amp;gt;
&lt;tr&gt;
&lt;th align="middle"&gt;Constraint&lt;/th&gt;
&lt;th&gt;Apply on&lt;/th&gt;
&lt;th&gt;Runtime checking&lt;/th&gt;
&lt;th&gt;NHibernate Metadata impact&lt;/th&gt;
&lt;/tr&gt;
&amp;lt;/thead&amp;gt;
&amp;lt;tbody&amp;gt;
&lt;tr&gt;
&lt;td align="middle"&gt;Length(Min=, Max=)&lt;/td&gt;
&lt;td&gt;property (String)&lt;/td&gt;
&lt;td&gt;checks if the string length is between the min-max range&lt;/td&gt;
&lt;td&gt;Column length will be set to max&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="middle"&gt;Max(Value=)&lt;/td&gt;
&lt;td&gt;property (numeric or string representation of a numeric)&lt;/td&gt;
&lt;td&gt;checks if the value is less than or equals to max&lt;/td&gt;
&lt;td&gt;Add a check constraint on the column&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="middle"&gt;Min(Value=)&lt;/td&gt;
&lt;td&gt;property (numeric or string representation of a numeric)&lt;/td&gt;
&lt;td&gt;checks if the value is more than or equals to min&lt;/td&gt;
&lt;td&gt;Add a check constraint on the column&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="middle"&gt;NotNull&lt;/td&gt;
&lt;td&gt;property&lt;/td&gt;
&lt;td&gt;checks if the value is not null&lt;/td&gt;
&lt;td&gt;Column(s) are not null&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="middle"&gt;NotEmpty&lt;/td&gt;
&lt;td&gt;property&lt;/td&gt;
&lt;td&gt;checks if the string is not empty. &lt;/td&gt;
&lt;td&gt;Column(s) are not null (for String)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="middle"&gt;NotEmptyOrNull&lt;/td&gt;
&lt;td&gt;property&lt;/td&gt;
&lt;td&gt;Checks if the string is not null or not empty.&lt;/td&gt;
&lt;td&gt;&amp;nbsp;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="middle"&gt;Past&lt;/td&gt;
&lt;td&gt;property (date or calendar)&lt;/td&gt;
&lt;td&gt;check if the date is in the past&lt;/td&gt;
&lt;td&gt;Add a check constraint on the column&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="middle"&gt;Future&lt;/td&gt;
&lt;td&gt;property (date or calendar)&lt;/td&gt;
&lt;td&gt;check if the date is in the future&lt;/td&gt;
&lt;td&gt;none&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="middle"&gt;Pattern(Regex=&amp;quot;regexp&amp;quot;, Flag=) &lt;/td&gt;
&lt;td&gt;property (string)&lt;/td&gt;
&lt;td&gt;check if the property matches the regular expression given a match flag. The &amp;lt;tt class=&amp;quot;methodname&amp;quot;&amp;gt;Flag&amp;lt;/tt&amp;gt; parameter is a &amp;lt;tt class=&amp;quot;classname&amp;quot;&amp;gt;System.Text.RegularExpressions.RegexOptions&amp;lt;/tt&amp;gt; enum. You can also declare multiple Pattern attributes on the same member. &lt;/td&gt;
&lt;td&gt;none&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="middle"&gt;Range(Min=, Max=)&lt;/td&gt;
&lt;td&gt;property (numeric or string representation of a numeric)&lt;/td&gt;
&lt;td&gt;check if the value is between Min and Max (included)&lt;/td&gt;
&lt;td&gt;Add a check constraint on the column&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="middle"&gt;Size(Min=, Max=)&lt;/td&gt;
&lt;td&gt;property (array, collection, map)&lt;/td&gt;
&lt;td&gt;check if the element size is between Min and Max (included)&lt;/td&gt;
&lt;td&gt;none&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="middle"&gt;AssertFalse&lt;/td&gt;
&lt;td&gt;property&lt;/td&gt;
&lt;td&gt;check that the method evaluates to false (useful for constraints expressed in code rather than annotations)&lt;/td&gt;
&lt;td&gt;none&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="middle"&gt;AssertTrue&lt;/td&gt;
&lt;td&gt;property&lt;/td&gt;
&lt;td&gt;check that the method evaluates to true (useful for constraints expressed in code rather than annotations)&lt;/td&gt;
&lt;td&gt;none&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="middle"&gt;Valid&lt;/td&gt;
&lt;td&gt;property (object)&lt;/td&gt;
&lt;td&gt;perform validation recursively on the associated object. If the object is a Collection or an array, the elements are validated recursively. If the object is a Map, the value elements are validated recursively.&lt;/td&gt;
&lt;td&gt;none&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="middle"&gt;Email&lt;/td&gt;
&lt;td&gt;property (String)&lt;/td&gt;
&lt;td&gt;check whether the string conforms to the email address specification&lt;/td&gt;
&lt;td&gt;none&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="middle"&gt;Digits&lt;/td&gt;
&lt;td&gt;property (numeric or string representation of a numeric)&lt;/td&gt;
&lt;td&gt;checks whether the property is a number having up to &amp;lt;tt class=&amp;quot;literal&amp;quot;&amp;gt;integerDigits&amp;lt;/tt&amp;gt; integer digits and &amp;lt;tt class=&amp;quot;literal&amp;quot;&amp;gt;fractionalDigits&amp;lt;/tt&amp;gt; fractonal digits&lt;/td&gt;
&lt;td&gt;define column precision and scale&lt;/td&gt;
&lt;/tr&gt;
&amp;lt;/tbody&amp;gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 class="title" style="clear:both;"&gt;Error messages&lt;/h2&gt;
&lt;p&gt;NHibernate Validator comes with a default set of error messages translated in several languages (if yours is not present, please send us a patch). You can add your own additional set of messages while writing your validator Attributes or at XML. If NHibernate Validator cannot resolve a key from your ResourceManager nor from ValidatorMessage, it falls back to the default built-in values.&lt;/p&gt;
&lt;p&gt;Alternatively you can provide a &amp;lt;tt class=&amp;quot;classname&amp;quot;&amp;gt;ResourceManager&amp;lt;/tt&amp;gt; while checking programmatically the validation rules on a bean or if you want a completly different interpolation mechanism, you can provide an implementation of &amp;lt;tt class=&amp;quot;literal&amp;quot;&amp;gt;NHibernate.Validator.Engine.IMessageInterpolator&amp;lt;/tt&amp;gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 class="title" style="clear:both;"&gt;Writing your own constraints&lt;/h2&gt;
&lt;p&gt;Extending the set of built-in constraints is extremely easy. Any constraint consists of two pieces: the constraint &lt;span class="emphasis"&gt;&lt;em&gt;descriptor&lt;/em&gt;&lt;/span&gt; (the attribute) and the constraint &lt;span class="emphasis"&gt;&lt;em&gt;validator&lt;/em&gt;&lt;/span&gt; (the implementation class). Here is a simple user-defined descriptor:&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
[ValidatorClass(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt;(PhoneValidator))]
&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; PhoneAttribute : Attribute, IRuleArgs
{
    &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; message = &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt;.Empty;
    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Message
    {
        get { &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; message; }
        set { message = &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;; }
    }
}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;To link a descriptor to its validator implementation, we use the &amp;lt;tt class=&amp;quot;literal&amp;quot;&amp;gt;[ValidatorClass]&amp;lt;/tt&amp;gt; meta-attribute. Here you can see an implementation of a constraint validator:&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; PhoneValidator : IValidator
{
     &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; IsValid(&lt;span style="color:#0000ff;"&gt;object&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;)
     {
         Regex regex = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Regex(&lt;span style="color:#006080;"&gt;@&amp;quot;^[2-9]\d{2}-\d{3}-\d{4}$&amp;quot;&lt;/span&gt;);
         &lt;span style="color:#0000ff;"&gt;if&lt;/span&gt; (&lt;span style="color:#0000ff;"&gt;value&lt;/span&gt; == &lt;span style="color:#0000ff;"&gt;null&lt;/span&gt;) &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;;
         &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; regex.IsMatch(&lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;.ToString());
     }
}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;This PhoneValidator class is simple. The method &amp;lt;tt class=&amp;quot;literal&amp;quot;&amp;gt;IsValid()&amp;lt;/tt&amp;gt; receives a string that should be validated with a Regex expression. The &amp;lt;tt class=&amp;quot;literal&amp;quot;&amp;gt;IsValid()&amp;lt;/tt&amp;gt; method should return false if the constraint has been violated. &lt;/p&gt;
&lt;p&gt;If you need to initialize the class validator you can implement the interface &amp;lt;tt class=&amp;quot;literal&amp;quot;&amp;gt;IInitializableValidator&amp;lt;A&amp;gt;&amp;lt;/tt&amp;gt; and the class should look like this: &lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; PhoneValidator : IInitializableValidator&amp;lt;PhoneAttribute&amp;gt;
{
    &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; Regex regex;

    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; IsValid(&lt;span style="color:#0000ff;"&gt;object&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;)
    {
        &lt;span style="color:#0000ff;"&gt;if&lt;/span&gt; (&lt;span style="color:#0000ff;"&gt;value&lt;/span&gt; == &lt;span style="color:#0000ff;"&gt;null&lt;/span&gt;) &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;;
        &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; regex.IsMatch(&lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;.ToString());
    }

    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Initialize(PhoneAttribute parameters)
    {
        regex = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Regex(&lt;span style="color:#006080;"&gt;@&amp;quot;^[2-9]\d{2}-\d{3}-\d{4}$&amp;quot;&lt;/span&gt;);
    }
}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;As you can see, implementing the &amp;lt;tt class=&amp;quot;literal&amp;quot;&amp;gt;IInitializableValidator&amp;lt;A&amp;gt;&amp;lt;/tt&amp;gt; interface we have to implement the method &amp;lt;tt class=&amp;quot;methodname&amp;quot;&amp;gt;Initialize(...)&amp;lt;/tt&amp;gt; that initializes the validator, and it&amp;#39;s executed first of all.&lt;/p&gt;
&lt;p&gt;The &amp;lt;tt class=&amp;quot;literal&amp;quot;&amp;gt;IsValid()&amp;lt;/tt&amp;gt; method should return &amp;lt;tt class=&amp;quot;literal&amp;quot;&amp;gt;false&amp;lt;/tt&amp;gt; if the constraint has been violated. For more examples, refer to the built-in validator implementations.&lt;/p&gt;
&lt;p&gt;We now have to implement the validator (ie. the rule checking implementation). A validation implementation can check the value of the a property (by implementing &amp;lt;tt class=&amp;quot;literal&amp;quot;&amp;gt;PropertyConstraint&amp;lt;/tt&amp;gt; ) and/or can modify the NHibernate mapping metadata to express the constraint at the database level (by implementing &amp;lt;tt class=&amp;quot;literal&amp;quot;&amp;gt;PersistentClassConstraint&amp;lt;/tt&amp;gt; )&lt;/p&gt;
&lt;p&gt;We have only seen property level validation, but you can write a class level validation attribute. Instead of receiving the return instance of a property, the bean itself will be passed to the validator. To activate the validation checking, just put the validation attribute to the class itself instead of in a property. A small sample can be found in the unit test suite.&lt;/p&gt;
&lt;p&gt;If your constraint can be applied multiple times (with different parameters) on the same property or type, you can use set the &amp;lt;tt class=&amp;quot;literal&amp;quot;&amp;gt;AllowMultiple&amp;lt;/tt&amp;gt; parameter to &amp;lt;tt class=&amp;quot;literal&amp;quot;&amp;gt;true&amp;lt;/tt&amp;gt; on the &amp;lt;tt class=&amp;quot;literal&amp;quot;&amp;gt;AttributeUsage&amp;lt;/tt&amp;gt; attribute. An example of this it&amp;#39;s the built-in validator Pattern, have a look:&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;[Serializable]
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = &lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;)]
[ValidatorClass(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt; (PatternValidator))]
&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; PatternAttribute : Attribute, IRuleArgs
{
    &amp;hellip;
}&lt;/pre&gt;
&lt;/div&gt;
&lt;pre class="programlisting"&gt;&amp;nbsp;&lt;/pre&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 class="title" style="clear:both;"&gt;Configuring your domain model with .Net Attributes&lt;/h2&gt;
&lt;p&gt;Since you are already familiar with attributes now, the syntax should be very familiar&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; Address
{ 
    &lt;span style="color:#008000;"&gt;// a static non null field  &lt;/span&gt;
    [NotNull]
    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;static&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; blacklistedZipCode;
    
    &lt;span style="color:#008000;"&gt;// field a not null string of 20 characters maximum&lt;/span&gt;
    [Length(Max = 20), NotNull]    
    &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; country;
    &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;long&lt;/span&gt; id;
    &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; internalValid = &lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;;
    &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; line1;
    &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; line2;
    &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; state;
    &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; zip;
    
    &lt;span style="color:#008000;"&gt;// a numeric between 1 and 2000&lt;/span&gt;
    [Min(1), Range(Max = 2000)]
    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;long&lt;/span&gt; Id
    {
        get { &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; id; }
        set { id = &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;; }
    }

    &lt;span style="color:#008000;"&gt;// respond to the constraint in the field: country&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Country
    {
        get { &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; country; }
        set { country = &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;; }
    }

    &lt;span style="color:#008000;"&gt;// a non null string&lt;/span&gt;
    [NotNull]
    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Line1
    {
        get { &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; line1; }
        set { line1 = &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;; }
    }

    &lt;span style="color:#008000;"&gt;// a not null string of 3 characters maximum&lt;/span&gt;
    [Length(Max = 3), NotNull]
    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; State
    {
        get { &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; state; }
        set { state = &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;; }
    }

    &lt;span style="color:#008000;"&gt;// a not null numeric string of 5 characters maximum&lt;/span&gt;
    &lt;span style="color:#008000;"&gt;// if the string is longer, the message will&lt;/span&gt;
    &lt;span style="color:#008000;"&gt;// be searched in the resource bundle at key &amp;#39;long&amp;#39;&lt;/span&gt;
    [Length(Max = 5, Message = &lt;span style="color:#006080;"&gt;&amp;quot;{long}&amp;quot;&lt;/span&gt;)]
    [Pattern(Regex = &lt;span style="color:#006080;"&gt;&amp;quot;[0-9]+&amp;quot;&lt;/span&gt;)]
    [NotNull]
    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Zip
    {
        get { &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; zip; }
        set { zip = &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;; }
    }

    &lt;span style="color:#008000;"&gt;// no constraint&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Line2
    {
        get { &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; line2; }
        set { line2 = &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;; }
    }

    &lt;span style="color:#008000;"&gt;// should always be true&lt;/span&gt;
    [AssertTrue]
    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; InternalValid
    {
        get { &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; internalValid; }
        set { internalValid = &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;; }
    }
} &lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;While the example only shows public property validation, you can also annotate fields of any kind of visibility. In the example &amp;lt;tt class=&amp;quot;classname&amp;quot;&amp;gt;MyBeanConstraint&amp;lt;/tt&amp;gt; is a custom Class Validator, we will talk about this later.&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;[MyBeanConstraint(Max=45)]
&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; Dog 
{
    [AssertTrue] &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; IsMale;
    [NotNull] &lt;span style="color:#0000ff;"&gt;protected&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Name { get {...} set{...} }
    &amp;hellip;
}     &lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;You can also annotate interfaces. NHibernate Validator will check all superclasses and interfaces extended or implemented by a given type to read the appropriate validator attributes.&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;interface&lt;/span&gt; Named 
{
    [NotNull] &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Name {get; set;}
    ...
}

&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; Dog : Named 
{
    [AssertTrue] &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; isMale;
    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Name { get {...} set{...} }
}
&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;The name property will be checked for nullity when the Dog type is validated.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 class="title" style="clear:both;"&gt;Configuring your domain model with XML&lt;/h2&gt;
&lt;p&gt;You also can configurate your domain model with constraints using xml files: the nhv.xml files. We prefer to embed these Xml files into the assemblies, and NHibernate Validator is prepared to inspect them and retrieve the resources with constrains, like NHibernate does with hbm.xml files.&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#800000;"&gt;xml&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;version&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;1.0&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;encoding&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;utf-8&amp;quot;&lt;/span&gt; ?&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;nhv-mapping&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;urn:nhibernate-validator-1.0&amp;quot;&lt;/span&gt;
                        &lt;span style="color:#ff0000;"&gt;namespace&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;NHibernate.Validator.Tests.Base&amp;quot;&lt;/span&gt;
                        &lt;span style="color:#ff0000;"&gt;assembly&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;NHibernate.Validator.Tests&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;class&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Address&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;blacklistedZipCode&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
                  &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;not-null&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;country&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
           &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;length&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;max&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;20&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
           &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;not-null&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;    
    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;floor&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;    
          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;range&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;min&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;-2&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;max&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;50&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;message&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;{floor.out.of.range} (escaping #{el})&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Id&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;    
          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;min&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;value&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;1&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;range&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;max&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;2000&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;max&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;value&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;2500&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Line1&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;    
          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;not-null&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;State&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;    
          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;not-null&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;length&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;max&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;3&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;

    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Zip&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;length&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;max&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;5&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;message&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;{long}&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;pattern&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;regex&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;[0-9]+&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;not-null&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;InternalValid&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;    
          &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;asserttrue&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;nhv-mapping&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;      &lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;span class="bold"&gt;&lt;strong&gt;We strongly recommend&lt;/strong&gt;&lt;/span&gt; to validate every nhv.xml file of your application with the respective XSD file. Every nhv.xml file should be validated with the &amp;lt;tt class=&amp;quot;literal&amp;quot;&amp;gt;nhv-mapping.xsd&amp;lt;/tt&amp;gt; file included within the NHibernate Validator distribution whether in source or binaries. Indeed, if you are using an IDE like Visual Studio it offers IntelliSense support in the nhv.xml file if you add the schema to Visual Studio&amp;#39;s xml schemas folder.&lt;/p&gt;
&lt;p&gt;&lt;span class="bold"&gt;&lt;strong&gt;Xml beauties:&lt;/strong&gt;&lt;/span&gt; The nicest part of Xml for validation is that you can put xml validators in a different dll so you don&amp;#39;t need to recompile your domain when validation rules change. Only the dll with the Xml files..&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class="chapter"&gt;
&lt;h1&gt;Configuration&lt;/h1&gt;
&lt;p&gt;NHibernate Validator has global parameters that you can configure at the startup. This configuration can be done in differents ways: programmaticaly or using xml files. If you are involved with NHibernate this could be very familiar to you.&lt;/p&gt;
&lt;p&gt;Configuration properties:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Table Configuration properties&lt;/strong&gt;&lt;/p&gt;
&lt;div class="table"&gt;
&lt;table&gt;
&amp;lt;colgroup&amp;gt;
&amp;lt;col align=&amp;quot;middle&amp;quot;&amp;gt;&amp;lt;/col&amp;gt;
&amp;lt;col&amp;gt;&amp;lt;/col&amp;gt;&amp;lt;/colgroup&amp;gt;
&amp;lt;thead&amp;gt;
&lt;tr&gt;
&lt;th align="middle"&gt;Property name&lt;/th&gt;
&lt;th&gt;Purpose&lt;/th&gt;
&lt;/tr&gt;
&amp;lt;/thead&amp;gt;
&amp;lt;tbody&amp;gt;
&lt;tr&gt;
&lt;td align="middle"&gt;&amp;lt;tt class=&amp;quot;literal&amp;quot;&amp;gt;&lt;strong&gt;apply_to_ddl&lt;/strong&gt;&amp;lt;/tt&amp;gt;&lt;/td&gt;
&lt;td&gt;Configure NHibernate to apply the constraints to the DDL.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="middle"&gt;&amp;lt;tt class=&amp;quot;literal&amp;quot;&amp;gt;&lt;strong&gt;autoregister_listeners&lt;/strong&gt;&amp;lt;/tt&amp;gt;&lt;/td&gt;
&lt;td&gt;Register the Event-Listeners automatically on Inserts and Updates.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="middle"&gt;&amp;lt;tt class=&amp;quot;literal&amp;quot;&amp;gt;&lt;strong&gt;default_validator_mode&lt;/strong&gt;&amp;lt;/tt&amp;gt;&lt;/td&gt;
&lt;td&gt;Define what are the constraints sources are and how to proceed. There are some built-in configurations:
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;div class="itemizedlist"&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&amp;lt;tt class=&amp;quot;literal&amp;quot;&amp;gt;UseAttribute&amp;lt;/tt&amp;gt; (by default): Use Attributes to configurate the validators on types and/or members. Xml is ignored.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&amp;lt;tt class=&amp;quot;literal&amp;quot;&amp;gt;UseXml&amp;lt;/tt&amp;gt;: Use Xml to configurate the validators on types and/or members. Attributes are ignored.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&amp;lt;tt class=&amp;quot;literal&amp;quot;&amp;gt;OverrideAttributeWithXml&amp;lt;/tt&amp;gt;: If the same type/member is annotated with an Attribute and has a Xml rule the Attribute annotation will be used.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&amp;lt;tt class=&amp;quot;literal&amp;quot;&amp;gt;OverrideXmlWithAttribute&amp;lt;/tt&amp;gt;: If the same type/member is annotated with an Attribute and has a Xml rule the Attribute annotation will be used.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="middle"&gt;&amp;lt;tt class=&amp;quot;literal&amp;quot;&amp;gt;&lt;strong&gt;assembly&lt;/strong&gt;&amp;lt;/tt&amp;gt;&lt;/td&gt;
&lt;td&gt;Which are the assemblies with contains the nhv.xml files embedded. You can have more than one assembly.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align="middle"&gt;&amp;lt;tt class=&amp;quot;literal&amp;quot;&amp;gt;&lt;strong&gt;shared_engine_provider&lt;/strong&gt;&amp;lt;/tt&amp;gt;&lt;/td&gt;
&lt;td&gt;To inject a custom Shared Engine Provider.&lt;/td&gt;
&lt;/tr&gt;
&amp;lt;/tbody&amp;gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 class="title" style="clear:both;"&gt;Programmatic configuration&lt;/h2&gt;
&lt;p&gt;You can use the following code to configure NHibernate.Validator programmatically:&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NHibernate.Validator.Cfg;
&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NHibernate.Validator.Engine;
&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; Environment=NHibernate.Validator.Cfg.Environment;
&amp;hellip;
ValidatorEngine ve = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ValidatorEngine();

NHVConfiguration nhvc = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; NHVConfiguration();
nhvc.Properties[Environment.ApplyToDDL] = &lt;span style="color:#006080;"&gt;&amp;quot;false&amp;quot;&lt;/span&gt;;
nhvc.Properties[Environment.AutoregisterListeners] = &lt;span style="color:#006080;"&gt;&amp;quot;false&amp;quot;&lt;/span&gt;;
nhvc.Properties[Environment.ValidatorMode] = GetMode();
nhvc.Mappings.Add(&lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; MappingConfiguration(&lt;span style="color:#006080;"&gt;&amp;quot;NHibernate.Validator.Demo.Model&amp;quot;&lt;/span&gt;,&lt;span style="color:#0000ff;"&gt;null&lt;/span&gt;));
ve.Configure(nhvc);  &lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;The &amp;lt;tt class=&amp;quot;literal&amp;quot;&amp;gt;ValidatorEngine&amp;lt;/tt&amp;gt; will be set with the configuration. The NHibernate Validator configuration is represented by the &amp;lt;tt class=&amp;quot;literal&amp;quot;&amp;gt;NHibernate.Validator.Cfg.NHVConfiguration&amp;lt;/tt&amp;gt; type. Other class used to configure all the stuff is the &amp;lt;tt class=&amp;quot;literal&amp;quot;&amp;gt;NHibernate.Validator.Cfg.Environment&amp;lt;/tt&amp;gt; type, this class contain the string with the names of the properties of the configuration of NHibernate Validator.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h2 class="title" style="clear:both;"&gt;Declarative configuration: using Xml files&lt;/h2&gt;
&lt;p&gt;The declarative configuration keeps configuration out of the code. This means configuring the application using a different file, in this case a XML file. But the options don&amp;#39;t end here: you can choose to configure NHibernate Validator using a simple Xml file or you can put the configuration in the well known App.Config/Web.Config. &lt;/p&gt;
&lt;div class="section"&gt;
&lt;h3 class="title"&gt;Simple xml file&lt;/h3&gt;
&lt;p&gt;This is a sample of a simple configuration in a xml file:&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#800000;"&gt;xml&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;version&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;1.0&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;encoding&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;utf-8&amp;quot;&lt;/span&gt; ?&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;nhv-configuration&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;#39;urn:nhv-configuration-1.0&amp;#39;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
  &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;#39;apply_to_ddl&amp;#39;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;true&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
  &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;#39;autoregister_listeners&amp;#39;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;true&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
  &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;#39;default_validator_mode&amp;#39;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;UseXml&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
  &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;mapping&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;assembly&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;#39;NHibernate.Validator.Demo.Model&amp;#39;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;nhv-configuration&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;We encourage to name the configuration file &amp;lt;tt class=&amp;quot;literal&amp;quot;&amp;gt;nhvalidator.cfg.xml&amp;lt;/tt&amp;gt;, because NHibernate Validator will pick up this file from the application directory in order to get the configuration parameters, however you can name this file as you wish.&lt;/p&gt;
&lt;p&gt;In order to configurate NHibernate Validator with the &amp;lt;tt class=&amp;quot;literal&amp;quot;&amp;gt;nhvalidator.cfg.xml&amp;lt;/tt&amp;gt; you can use the following codeו:&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;ValidatorEngine engine = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ValidatorEngine();
engine.Configure(); &lt;span style="color:#008000;"&gt;// by convention reads the nhvalidator.cfg.xml file.&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;You can choose another name for your configuration file and proceed of this manner: &lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;ValidatorEngine engine = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ValidatorEngine();
engine.Configure(&lt;span style="color:#006080;"&gt;&amp;quot;path/to/MyNHVConfigurationFile.xml&amp;quot;&lt;/span&gt;); &lt;span style="color:#008000;"&gt;// you can use whatever path to point to the configuration file&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class="section"&gt;
&lt;h3 class="title"&gt;Configuration Application file: app.config/web.config&lt;/h3&gt;
&lt;p&gt;The configuration can be declared into our configuration application .Net files: app.config or web.config. In the below example you will see how to do it.&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#800000;"&gt;xml&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;version&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;1.0&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;encoding&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;utf-8&amp;quot;&lt;/span&gt; ?&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;configuration&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;configSections&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
        &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;section&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;nhv-configuration&amp;quot;&lt;/span&gt; 
                            &lt;span style="color:#ff0000;"&gt;type&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;NHibernate.Validator.Cfg.ConfigurationSectionHandler, NHibernate.Validator&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
        ...
    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;configSections&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    ...
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;nhv-configuration&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;#39;urn:nhv-configuration-1.0&amp;#39;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
        &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;shared_engine_provider&lt;/span&gt; 
         &lt;span style="color:#ff0000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;#39;NHibernate.Validator.Event.NHibernateSharedEngineProvider, NHibernate.Validator&amp;#39;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;nhv-configuration&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    ...
&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;configuration&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;      &lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;</description></item></channel></rss>
