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
You can use this validator to check if there are issues when indexing the content with Episerver Find.
Read my blog here
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]; | |
} | |
} |