Sitecore Entity Service SSC - Custom Validation Attributes
One of the exciting features of Entity Service is the flexibility to create custom validation attributes for your model. As Sitecore.Services.Client (SSC) provides a framework based approach to creating services, and both server and client side APIs, validation can take place on both server and client side.
The first part of adding validation to the model is to create our server side validation. Create a class implementing ValidationAttribute, on the IsValid method add the valdation code. In the example below i've created a validation attribute to check a DateTime is in the future.
An example of creating the client side validation can be seen below. The validator provides the same functionality as the server side above. The value parameter passed into the function comes from the value set in the model.
To provide Entity Service with both the client and server validation methods a meta data class is required. The ValidationMetaDataBase wires up the client and server side validation.
The ValidationMetaDataBase is a generic class that takes a ValidationAttribute, so this allows wiring up the server side validation created above. To wire up the client side validator, on the constructor of the class, implement the base constructor passing in the name of the client side validation method.
Now the custom validation has been created, the properties of the model can be decorated with the custom ValidationAttribute.