Validating PropertyList items with a custom class

This will be a very simple blog post but it might come in handy when you want to add (complex) logic to validate property list items with a custom class. Let’s presume we have a page with a property list of type CustomData.

As I want to add some validation to CustomData, the first thing I tried was adding IValidatableObject to the CustomData class, however the Validate method was never called. Episerver has a couple of built-in validation attributes for validating the items of a PropertyList (with primitive types).

I decided to go the same route and write a custom validation attribute and add my validation logic using an attribute.

CustomDataValidationAttribute contains the actual validation logic. ItemCustomDataValidationAttribute is the attribute that will be added to the PropertyList property on the ExamplePage in order to validate the items in the list. Lastly ListItemValidator is a helper method to iterate the list and execute the validation for each of the items in the PropertyList.
Add the ItemCustomDataValidationAttribute to the property and you’ll receive an error message when the validation fails.