Skip to content

Instantly share code, notes, and snippets.

@jstemerdink
Created September 12, 2018 15:58
Show Gist options
  • Save jstemerdink/d7553deefb4cae809bd4b47bcec0a673 to your computer and use it in GitHub Desktop.
Save jstemerdink/d7553deefb4cae809bd4b47bcec0a673 to your computer and use it in GitHub Desktop.

Validate your content on Episerver Find indexing errors

You can use this validator to check if there are issues when indexing the content with Episerver Find.

Read my blog here

Powered by ReSharper image

using EPiServer.Core;
using EPiServer.Find;
using EPiServer.Find.Cms;
using EPiServer.Find.Framework;
using EPiServer.Find.Helpers;
using EPiServer.Find.Json;
using EPiServer.Reference.Commerce.Site.Features.Product.Models;
using EPiServer.Validation;
using Newtonsoft.Json;
public class SerializationValidator : IValidate<IContent>
{
IEnumerable<ValidationError> IValidate<IContent>.Validate(IContent content)
{
JsonSerializer jsonSerializer = Serializer.CreateDefault();
Action<JsonSerializer> customizeSerializer = SearchClient.Instance.CustomizeSerializer;
customizeSerializer(obj: jsonSerializer);
IClientConventions defaultConventions = SearchClient.Instance.Conventions;
if (defaultConventions.ContractResolver.IsNotNull())
{
jsonSerializer.ContractResolver = defaultConventions.ContractResolver;
}
if (defaultConventions.CustomizeSerializer.IsNotNull())
{
defaultConventions.CustomizeSerializer(obj: jsonSerializer);
}
try
{
jsonSerializer.Serialize(value: content);
}
catch (SelfReferencingLoopException selfReferencingLoopException)
{
string message = selfReferencingLoopException.ExplainIndexException();
return new[]
{
new ValidationError
{
ErrorMessage = message,
PropertyName = selfReferencingLoopException.GetType().Name,
Severity = ValidationErrorSeverity.Warning,
ValidationType = ValidationErrorType.Unspecified
}
};
}
catch (MaxSerializationDepthExceededException maxSerializationDepthExceededException)
{
return new[]
{
new ValidationError
{
ErrorMessage =
maxSerializationDepthExceededException
.ExplainIndexException(),
PropertyName =
maxSerializationDepthExceededException.GetType().Name,
Severity = ValidationErrorSeverity.Warning,
ValidationType = ValidationErrorType.Unspecified
}
};
}
catch (Serializer.JsonItemSerializationException jsonItemSerializationException)
{
return new[]
{
new ValidationError
{
ErrorMessage =
jsonItemSerializationException.ExplainIndexException(),
PropertyName = jsonItemSerializationException.GetType().Name,
Severity = ValidationErrorSeverity.Warning,
ValidationType = ValidationErrorType.Unspecified
}
};
}
catch (JsonSerializationException jsonSerializationException)
{
return new[]
{
new ValidationError
{
ErrorMessage =
jsonSerializationException.ExplainIndexException(),
PropertyName = jsonSerializationException.GetType().Name,
Severity = ValidationErrorSeverity.Warning,
ValidationType = ValidationErrorType.Unspecified
}
};
}
return new ValidationError[0];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment