UNPKG

@unito/integration-debugger

Version:

The Unito Integration Debugger

77 lines (76 loc) 3.62 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const integration_api_1 = require("@unito/integration-api"); const crawler_1 = require("../crawler"); /** * Check: Validate classic comments * * We validate that any relation with the semantic "comment" is compliant to the classic comments schema. * * A classic comment must have: * - a field of type html/markdown/string with the semantic "description" * - a field of type datetime with the semantic "createdAt" (not required) * - a field of type reference with the semantic "user" (not required) * */ const check = { label: 'Classic Comments', prepareOnPreparedSteps: false, validateOnError: false, activatedByDefault: true, prepare: async (_stepResult, _crawlerDriver) => { return []; }, validate: (step, _crawlerDriver) => { if (step.operation !== crawler_1.Operation.GetItem || !step.payloadOut) { return; } const payload = step.payloadOut; const commentRelations = payload.relations?.filter(relation => relation.semantic === integration_api_1.RelationSemantics.COMMENTS); if (!commentRelations.length) { return; } for (const commentRelation of commentRelations) { const descriptionField = commentRelation.schema.fields.find(field => field.semantic === integration_api_1.Semantics.DESCRIPTION); if (!descriptionField) { step.errors.push({ keyword: 'unito', message: `The relation is missing a field.`, detailedMessage: `The relation with semantic '${integration_api_1.RelationSemantics.COMMENTS}' must have a field with the semantic '${integration_api_1.Semantics.DESCRIPTION}'.`, instancePath: step.path, schemaPath: step.schemaPath ?? '', params: { code: 'DESCRIPTION_NOT_FOUND', }, }); } const createdAtField = commentRelation.schema.fields.find(field => field.semantic === integration_api_1.Semantics.CREATED_AT); if (!createdAtField) { step.warnings.push({ keyword: 'unito', message: `The relation is missing a field.`, detailedMessage: `The relation with semantic '${integration_api_1.RelationSemantics.COMMENTS}' should have a field with the semantic '${integration_api_1.Semantics.CREATED_AT}' to enable comments's decoration.`, instancePath: step.path, schemaPath: step.schemaPath ?? '', params: { code: 'CREATED_AT_NOT_FOUND', }, }); } const userField = commentRelation.schema.fields.find(field => field.semantic === integration_api_1.Semantics.USER); if (!userField) { step.warnings.push({ keyword: 'unito', message: `The relation is missing a field.`, detailedMessage: `The relation with semantic '${integration_api_1.RelationSemantics.COMMENTS}' should have a field with the semantic '${integration_api_1.Semantics.USER}' to enable comments's decoration.`, instancePath: step.path, schemaPath: step.schemaPath ?? '', params: { code: 'USER_NOT_FOUND', }, }); } } }, }; exports.default = check;