contentful-migration
Version:
Migration tooling for contentful
62 lines • 3.13 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const __1 = require("../");
const resource_links_1 = require("../../utils/resource-links");
const errors_1 = __importDefault(require("./errors"));
class ResourceLinksValidator {
constructor() {
this.hooks = [__1.ApiHook.SaveContentType];
}
validate({ contentType }) {
var _a, _b;
const errors = [];
const fields = contentType.fields.toRaw();
const resourceLinkCount = fields.filter((field) => (0, resource_links_1.isResourceLink)(field) || (0, resource_links_1.getEnabledResourceLinkNodes)(field).length).length;
if (resourceLinkCount > resource_links_1.MAX_RESOURCE_LINKS) {
errors.push({
type: 'InvalidPayload',
message: errors_1.default.contentType.TOO_MANY_RESOURCE_LINK_FIELDS(contentType.id)
});
}
for (const field of fields) {
if ((0, resource_links_1.isResourceLink)(field) && !field.allowedResources) {
errors.push({
type: 'InvalidPayload',
message: errors_1.default.field.REQUIRED_DEPENDENT_PROPERTY('allowedResources', field.id, 'type', 'ResourceLink')
});
}
const enabledResourceLinkNodes = (0, resource_links_1.getEnabledResourceLinkNodes)(field);
if (enabledResourceLinkNodes.length > 0) {
const nodesValidation = (_a = field.validations) === null || _a === void 0 ? void 0 : _a.find(({ nodes }) => nodes && typeof nodes === 'object');
if (!nodesValidation) {
errors.push({
type: 'InvalidPayload',
message: errors_1.default.field.REQUIRED_DEPENDENT_PROPERTY('validations[].nodes', field.id, 'type', 'RichText')
});
}
else {
for (const nodeType of enabledResourceLinkNodes) {
if (!((_b = nodesValidation.nodes[nodeType]) === null || _b === void 0 ? void 0 : _b.allowedResources)) {
errors.push({
type: 'InvalidPayload',
message: errors_1.default.field.REQUIRED_DEPENDENT_PROPERTY(`validations[].nodes.${nodeType}.allowedResources`, field.id, 'validations[].enabledNodeTypes[]', nodeType)
});
}
}
}
}
if (!(0, resource_links_1.isResourceLink)(field) && field.allowedResources) {
errors.push({
type: 'InvalidPayload',
message: errors_1.default.field.FORBIDDEN_DEPENDENT_PROPERTY('allowedResources', field.id, 'type', 'ResourceLink')
});
}
}
return errors;
}
}
exports.default = ResourceLinksValidator;
//# sourceMappingURL=resource-links.js.map