Try our conversational search powered by Generative AI!

Johan Björnfot
May 22, 2018
  3552
(7 votes)

IContentSaveValidate - validation with context

This time I thought I should share a tip about a component IContentSaveValidate<TContent> that we added a while ago but that perhaps not all are aware of. 

In CMS 7 we added the EPiServer.Validation.IValidate<T> interface, which has a signature like:

 /// <summary>
    /// Defines the signature for a component that validates instances of <typeparamref name="T"/>.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public interface IValidate<T> : IValidate
    {
        /// <summary>
        /// Validates the specified instance.
        /// </summary>
        /// <param name="instance">The instance.</param>
        /// <returns></returns>
        IEnumerable<ValidationError> Validate(T instance);
    }

The idea with the interface was to make it possible to extend the validation possibilities offered by validation attributes. So when calling IValidationService.Validate for an item then will the service first locate all registered validators that has a generic type parameter T which the item can be assigned to and then call validate on them. So say for example that we are validating an instance of StandardPage then would validators implementing IValidate<StandardPage>, IValidate<PageData> and IValidate<IContent> be called but not a validator implementing IValidate<IContentMedia>.

The IValidate<T> interface does not have any type constraint but it is most commonly used during save of content instances. However there might be cases where you want to do the validation only for certain state transitions, for example when the content is published. With IValidate<T> it is not possible to distinguish which state transition it is (if any). Therefore we introduced the more specialized interface IContentSaveValidate<TContent> that looks like:

public interface IContentSaveValidate<TContent> : IContextValidate<TContent, ContentSaveValidationContext>
        where TContent : IContentData
    {
        /// <summary>
        /// Validates the specified instance given specified context
        /// </summary>
        /// <param name="instance">The instance that is validate</param>
        /// <param name="context">The context for the validation</param>
        /// <returns>A list of validation errors or empty list if instance is valid</returns>
        IEnumerable<ValidationError> Validate(TContent instance, ContentSaveValidationContext context);
    }

Now in addition to the content item to validate you also get a context where you can get the state transtion from. Below is an example of an implementation that only validates when a StandardPage is published.

    [ServiceConfiguration(IncludeServiceAccessor = false)]
    public class StandarPagePublishedValidator : IContentSaveValidate<StandardPage>
    {
        private readonly IStatusTransitionEvaluator _statusTransitionEvaluator;

        public StandarPagePublishedValidator(IStatusTransitionEvaluator statusTransitionEvaluator)
        {
            _statusTransitionEvaluator = statusTransitionEvaluator;
        }

        public IEnumerable<ValidationError> Validate(StandardPage instance, ContentSaveValidationContext context)
        {
            var transition = _statusTransitionEvaluator.Evaluate(instance, context.SaveAction);
            if (transition.NextStatus == VersionStatus.Published)
            {
                //do validation for publish here
            }
            return Enumerable.Empty<ValidationError>();
        }
    }



May 22, 2018

Comments

Please login to comment.
Latest blogs
Optimizely and the never-ending story of the missing globe!

I've worked with Optimizely CMS for 14 years, and there are two things I'm obsessed with: Link validation and the globe that keeps disappearing on...

Tomas Hensrud Gulla | Apr 18, 2024 | Syndicated blog

Visitor Groups Usage Report For Optimizely CMS 12

This add-on offers detailed information on how visitor groups are used and how effective they are within Optimizely CMS. Editors can monitor and...

Adnan Zameer | Apr 18, 2024 | Syndicated blog

Azure AI Language – Abstractive Summarisation in Optimizely CMS

In this article, I show how the abstraction summarisation feature provided by the Azure AI Language platform, can be used within Optimizely CMS to...

Anil Patel | Apr 18, 2024 | Syndicated blog

Fix your Search & Navigation (Find) indexing job, please

Once upon a time, a colleague asked me to look into a customer database with weird spikes in database log usage. (You might start to wonder why I a...

Quan Mai | Apr 17, 2024 | Syndicated blog