Provides a function to validate the content of a Rich Text element to ensure it's suitable for updating via the Kontent Management API.
npm install @kentico-ericd/kontent-richtext-validationnpm i @kentico-ericd/kontent-richtext-validation --save
js
// Typescript + ES
import { KontentRichText } from '@kentico-ericd/kontent-richtext-validation'
`
`js
// CommonJS
const { KontentRichText } = require('@kentico-ericd/kontent-richtext-validation');
`
The KontentRichText class provides a validate() function, which returns the following object:
`js
{
success: ,
message:
}
`
In the event that there is an error parsing the input, success will be _false_ and message will contain the error and (in most cases) the exact position of the faulty text. On success, the message should contain the exact text that was passed to the function.
For example:
`js
KontentRichText.validate('')
`
`js
{
success: false,
message: "The A element must have a 'href' attribute or a 'data-email-address' attribute or a data attribute that identifies the link
target ('data-item-id', 'data-item-external-id', 'data-asset-id' or 'data-asset-external-id'). (1, 30)"
}
`
We can see that the error occurred on line 1, col 30 which is the closing double-quote of the _href_ attribute.
Example
This package could be integrated into the automatic translation webhook of the sample ExpressJS application. Perhaps we want to _always_ return a 200 (success) response to Kontent when the webhook triggers, so that the webhook doesn't receive errors and stop processing. Or, we could validate the text and if there is some error, we can skip that item and continue with the rest of them.
Specifically, it could be added to this line where we get translated text from an external service. Maybe the external service accidentally changed some of the HTML? Let's check:
`js
if (match.length > 0) {
e.value = text.replace(/
/g, '
');
const validationResult = KontentRichText.validate(e.value);
if(!validationResult.success) {
const error = Validation failed for item ${updatedVariant.data.item.id},
+ lang ${targetLangCode}, element ${e.id}: ${validationResult.message};
// We can log the error
console.log(error);
// We could write to some physical log file too
writeToLogFile(error);
// Send an email to devs?
sendEmail('devs@mysite.com', 'Webhook error', error);
// Skip this variant but continue with others
stopProcessing = true;
// or, maybe throw an error
throw new Error(error);
}
}
``