BCS Validation



Data ValidationBackground Check SystemIT Technical Design GuideVersion 01 SAVEDATE \@ "M/d/yyyy" \* MERGEFORMAT 1/29/2015Table of Contents TOC \o "1-2" \u 1BCS Validation PAGEREF _Toc410166264 \h 31.1Bcs.Validation Project PAGEREF _Toc410166265 \h 31.2IValidatable PAGEREF _Toc410166266 \h 41.3Validatable PAGEREF _Toc410166267 \h 41.4Validator PAGEREF _Toc410166268 \h 51.5ReferenceValidationAttribute PAGEREF _Toc410166269 \h 51.6PropertyReferenceValidationAttribute PAGEREF _Toc410166270 \h 61.7Requirable Attributes PAGEREF _Toc410166271 \h 71.8Validation Layers PAGEREF _Toc410166272 \h 82Client-Side Validation PAGEREF _Toc410166273 \h 92.1Web.config PAGEREF _Toc410166274 \h 92.2Required Scripts PAGEREF _Toc410166275 \h 92.3DataAnnotations to Client PAGEREF _Toc410166276 \h 103DTO Validation Attributes PAGEREF _Toc410166277 \h 12BCS ValidationThe purpose of this document is to describe the various methods in which the Background Check System (BCS) performs data validation.Bcs.Validation ProjectThe Bcs.Validation project contains a reference to the Microsoft.Practices.EnterpriseLibrary.Validation, but the Enterprise Library is only used in a limited fashion. The ponentModel.DataAnnotations library contains the majority of the validation attributes utilized by the BCS.IValidatableThe IValidatable interface declares a single method, Validate(), which is responsible for performing validation of an implementing object and returning the validation results.The Result class is used throughout the BCS system and contains an IsSuccessful indicator along with a list of ErrorMessages to contain any validation issues.ValidatableThe Validatable is an abstract base class implementing the IValidatable interface. The most commonly derived classes are model-view-controller (MVC) models.The Validate() method satisfies the IValidatable interface by leveraging the Enterprise Library to check any validation attributes and then calling the virtual method Validate(Result) to optionally merge any derived class’s validation efforts.Since not all validation can be performed internally to a class/model, the Validate(Func) method allows for additional validation to be performed by passing a function. For example, such a function could check the database to make sure the data record is unique.ValidatorThe Validator class is responsible for performing attribute-based validation for classes that do not (necessarily) implement the IValidatable interface.Additionally, there are several overloaded methods which are leveraged by the various validation layer projects (“Bcs.*.Val”). These methods perform validation and then call another method only if validation is successful.ReferenceValidationAttributeThe ReferenceValidationAttribute class implements the ability for classes to use another class’s validation attributes. This is commonly used in the BCS with MVC models in order to avoid the need to replicate validation attributes already defined on the corresponding data transfer object (DTO). In the BCS we avoid using DTO types as models. When a [ReferenceValidation] attribute is applied to a class, any properties that match the name of the referenced type are replicated. The following is an example of an MVC model class for editing User DTOs, which applies a [ReferenceValidation] attribute:Since the referenced User DTO contains several validation attributes applied to the UserName property, the MVC model’s corresponding UserName property will also contain these validation attributes implicitly. This approach has proven to be a great strategy to keep the BCS model and DTO types loosely coupled without the burden of replicating validation attributes.PropertyReferenceValidationAttributeThe PropertyReferenceValidationAttribute is similar to the ReferenceValidationAttribute, except that it applies to only a single property instead of an entire type. This attribute allows for a more targeted opt-in approach to using validation attributes from referenced types.Requirable AttributesThe following two validation attributes are currently located in the Bcs.Web.Mvc.Website.Models project, since they have too many dependencies to reside in the common Bcs.Validation project.RequirableAttribute: This attribute allows a property to be required on a per-department basis. Properties are referenced with a “Type.Property” naming convention and correspond to a configuration setting with a “IsRequired(Type.Property)” naming convention. When validating an object containing a Requirable attribute, the current user’s department is used to lookup the value of the configuration setting and require or not-require appropriately.FeatureRequireableAttribute: This attribute is similar to the RequirableAttribute, except it relies on the configuration setting “HasFeature(Feature)”.Validation LayersBCS modules contain a validation layer which is responsible for validating input before saving it to the database. Such validation layers are implemented in projects named “Bcs.*.Val” and implement validation for most “Save” methods.The majority of the methods implementing validation are similar to the following, where Validator.Validate is called to perform attribute-based validation (as most DTO types do not implement IValidatable).Some validation implementations perform more involved validation. The EmploymentSave example below performs the attribute-based validation and, if that succeeds, it calls another method to perform more extensive validation (which, in this example, eventually calls a stored procedure). If all of the validation attempts succeed, the actual save is performed.The EmploymentValidate method then performs any attribute based validation as well as a stored-procedure which performs much more robust data validation with department-specific rules.Client-Side ValidationWeb.configClient-side validation is enabled by two application settings in the web.config: (1) “ClientValidationEnabled” and (2) “UnobtrusiveJavaScriptEnabled.” Both are required.<appSettings><clear/><add key="ClientValidationEnabled" value="true"/><add key="UnobtrusiveJavaScriptEnabled" value="true”/></AppSettings>Required Scripts Two scripts must be included for unobtrusive client-side validation: (1) jquery.validate.js and (2) jquery.validate.unobtrusive.js. Both of these are jQuery plug-ins. jquery.validate.js sets up client validation capabilities. jquery.validate.unobtrusive.js, released by Microsoft, uses jQuery validation to add data-validation HTML attributes derived from any DataAnnotation attributes applied to server-side rendering models.DataAnnotations to ClientAs an example of this process, the ChangePasswordModel uses [Required] attributes to indicate required fields:public class ChangePasswordModel : Validatable{[Required(ErrorMessage = “Current Password is required.][DataType(DataType.Password)]public string CurrentPassword { get; set; }[Required(ErrorMessage = “New Password is required.”][DataType(DataType.Password)]public string NewPassword { get; set; }[Required(ErrorMessage = “Confirm Password is required.][DataType(DataType.Password)]public string ConfirmPassword { get; set; }}When this model gets rendered (using MVC Html Helpers), the HTML for the NewPassword input field is rendered as:<input data-val="true" data-val-required="New Password is required." id="NewPassword" name="NewPassword" type="password">The HTML attributes indicate that this input field is a data value field, that a value is required in this field prior to form post, and that if a value is not present in this field at form post time, display the message “New Password is required.” in the form validation message list. The space for this validation error list is rendered by the ValidationSummary Html Helper. This helper must be applied inside the form tag. In MVC 3, the form tag must be rendered via Html.BeginForm:@using (Html.BeginForm("", "", FormMethod.Post, new { @id="changePasswordForm" } )){@Html.ValidationSummary(Bcs.Web.Website.Constants.VALIDATION_SUMMARY)Etc…}Omitting the New Password field results in the validation summary below:These [Required] attributes are also detected by the custom HtmlLabel508For helper which outputs a red asterisk in front of the labels for required fields.DTO Validation AttributesThe DataAnnotation attributes that are rendered as HTML validation attributes do not have to be applied to the MVC view model that is getting rendered. The ReferenceValidation and PropertyReference attributes described earlier allow DataAnnotation attributes applied to upstream DTO’s to be applied dynamically to MVC view models when they are loaded for rendering.For example, the MVC view model ApplicantAddSearchCriteria does not include StringLength or RegularExpression attributes on the Social Security Number field (Ssn). However, when this model is rendered, the HTML input field looks like:<input data-val="true" data-val-length="SSN must be 11 characters." data-val-length-max="11" data-val-regex="Please enter a valid SSN in the form xxx-xx-xxxx" data-val-regex-pattern="^(?!\b(\d)\1+-(\d)\1+-(\d)\1+\b)(?!219-09-9999|078-05-1120)(?!666|000)\d{3}-(?!00)\d{2}-(?!0{4})\d{4}$" id="Ssn" name="Ssn" style="width:8em;" tabindex="1" type="text" value="" class="valid">The string length (data-val-length-max="11”) and regex pattern (data-val-regex-pattern="^(?!\b(\d)\1+-(\d)\1+-(\d)\1+\b)(?!219-09-9999|078-05-1120)(?!666|000)\d{3}-(?!00)\d{2}-(?!0{4})\d{4}$”) attributes have been copied at model load time from the Person DTO by virtue of the PropertyReferenceValidation attribute.public class ApplicantAddSearchCriteria : Validatable{ [PropertyReferenceValidation(typeof(Person), "Ssn", true)] public string Ssn { get; set; } Etc. …}The application of middle-tier DTO attributes to MVC view models is accomplished by a custom MVC Model Validator Provider - DtoDataAnnotationsModelValidatorProvider. This custom validator provider is added at Application_Start in Global.asax.cs:ModelValidatorProviders.Providers.Add(new DtoDataAnnotationsModelValidatorProvider()); ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download