# Custom Validation

If you have a complex custom property, or want to do some custom checks on content or media before it's saved, this is where the IValidate comes in handy.

This is used internally to validate items before it's saved. To get one running you just need to implement the IValidate interface

```csharp
public interface IValidate<T>
{
    Task<ValidateResult> Validate(T item);
}
```

Where T is the type of item you want to validate.  Below shows our Content validate which firstly checks the Name is never null then checks for any properties that are required and makes sure a value is saved

```csharp
public class ValidateContent(IMediator mediator) : IValidate<Models.Content>
{
    public async Task<ValidateResult> Validate(Models.Content item)
    {
        var validateResult = new ValidateResult();
        if (item.Name.IsNullOrWhiteSpace())
        {
            validateResult.ErrorMessages.Add("You cannot leave the name empty");
        }
        
        // This might be new content, so we need to get the content type manually! 
        var contentType = await mediator.Send(new GetContentTypeCommand { Id = item.ContentTypeId });
        
        var valuesInDict = item.PropertyData.ToDictionary(x => x.ContentTypePropertyId, x => x);
        foreach (var p in contentType.ContentProperties.Where(x => x.IsRequired))
        {
            
            valuesInDict.TryGetValue(p.Id, out var contentValue);
            if (contentValue != null && contentValue.Value.IsNullOrWhiteSpace())
            {
                validateResult.ErrorMessages.Add($"{p.Name} is required");
            }
        }

        return validateResult;
    }
}
```

Any issues, you just add an error message to the ErrorMessages list and those are displayed to the editor. They will be unable to continue until these are addressed


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://aptitude.gitbook.io/zaubercms/extending-zaubercms/custom-validation.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
