Documentation

Model Validation

Version 1.2   |   Last Updated: 1/28/2014 (v1.2)   |  

The library provides a validation infrastructure that is integrated with the base model classes provided and the general model processing logic during updates and inserts.

The library defines an IValidatableModel interface with a single method TryValidate. TryValidate validates the entire model object, returning false if invalid and populates a validation results dictionary with keys representing the property names causing the invalidation and values representing one or more reasons for validation failure. The BindableModelBase (and thus the SyncableModelBase) class implements IValidatableModel – this TryValidate method can be used as the basis for an IValidatableObject (a Microsoft validation framework) implementation for supporting platforms.

The library also defines an IValidator interface for external model validators. This interface defines two methods – TryValidate, to validate an entire model object and TryValidateProperty, to validate a specific property of a model object.

The BindableModelBase class optionally accepts an external validator object implementing IValidator in its constructor. BindableModelBase uses this external validator as the basis for its default TryValidate implementation and uses it to provide a protected ValidateProperty convenience method. This ValidateProperty method should be called within property setters of model classes derived from BindableModelBase or SyncableModelBase (and is called in the default model implementations for each module). If an external IValidator object is not provided to a model derived from BindableModelBase, the validation calls will simply have no effect, passing automatically.

Finally, the BindableModelBase (and thus the SyncableModelBase) class uses the external validator to provide an implementation of the INotifyDataErrorInfo interface to support the error notification framework commonly used in Silverlight, WPF or mobile .NET applications. This framework also relies on the ValidateProperty method being called in model property setters.

The library provides a base class, ValidatorBase, for implementing external validators. This simply adds convenience methods for derived classes to use in their TryValidate implementation. The library also provides a PropertyValidationRules static class which defines generic validation rules that can be used in specific validator implementations.

Base validators are provided for each CRM module that define validation rules for the standard module fields. These validators are assigned to the base syncable model implementations for each module (but can be overridden with custom validators using the SyncableModelBase constructor overloads).

When the library validates models prior to posting data to the Zoho API and validation fails, a ValidationException will be thrown. The ValidationException class extends the standard Exception class to add ModelInstance and ValidationResults properties that will be populated with a reference to the model object failing validation and the validation reason(s) associated with each property.

The provided code samples demonstrate the implementation of custom validators and provide the source code for the default validators for each model.