@contentful/rich-text-types
Version:
Type definitions and constants for the Contentful rich text field type.
123 lines • 5.43 kB
JavaScript
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.assertLink = exports.assert = exports.HyperLinkAssertion = exports.EntityLinkAssertion = exports.NodeAssertion = exports.VOID_CONTENT = void 0;
var assert_1 = require("./assert");
exports.VOID_CONTENT = [];
var NodeAssertion = /** @class */ (function () {
function NodeAssertion(contentRule, validateData) {
this.contentRule = contentRule;
this.validateData = validateData;
}
NodeAssertion.prototype.assert = function (node, path) {
var _a, _b;
var $ = new assert_1.ObjectAssertion(node, path);
if (!$.object()) {
return $.errors;
}
$.noAdditionalProperties(['nodeType', 'data', 'content']);
var _c = Array.isArray(this.contentRule)
? {
nodeTypes: this.contentRule,
}
: this.contentRule(node, path), nodeTypes = _c.nodeTypes, _d = _c.min, min = _d === void 0 ? 0 : _d;
if (nodeTypes.length === 0 && min > 0) {
throw new Error("Invalid content rule. Cannot have enforce a 'min' of ".concat(min, " with no nodeTypes"));
}
$.minLength('content', min);
// Is void
if (nodeTypes.length === 0) {
$.empty('content');
}
// Ensure content nodes have valid nodeTypes without validating the full
// shape which is something that's only done later if the current node is
// valid.
else {
$.each('content', function (item, path) {
var item$ = new assert_1.ObjectAssertion(item, path);
if (!item$.object()) {
return item$.errors;
}
item$.enum('nodeType', nodeTypes);
return item$.errors;
});
}
if ($.object('data')) {
var dataErrors = (_b = (_a = this.validateData) === null || _a === void 0 ? void 0 : _a.call(this, node.data, path.of('data'))) !== null && _b !== void 0 ? _b : [];
$.catch.apply($, dataErrors);
}
return $.errors;
};
return NodeAssertion;
}());
exports.NodeAssertion = NodeAssertion;
var EntityLinkAssertion = /** @class */ (function (_super) {
__extends(EntityLinkAssertion, _super);
function EntityLinkAssertion(linkType, contentNodeTypes) {
var _this = _super.call(this, contentNodeTypes, function (data, path) { return _this.assertLink(data, path); }) || this;
_this.linkType = linkType;
_this.assertLink = function (data, path) {
var $ = new assert_1.ObjectAssertion(data, path);
if ($.object('target')) {
var sys$ = new assert_1.ObjectAssertion(data.target.sys, path.of('target').of('sys'));
if (sys$.object()) {
sys$.enum('type', [_this.type]);
sys$.enum('linkType', [_this.linkType]);
if (_this.type === 'Link') {
sys$.string('id');
sys$.noAdditionalProperties(['type', 'linkType', 'id']);
}
else if (_this.type === 'ResourceLink') {
sys$.string('urn');
sys$.noAdditionalProperties(['type', 'linkType', 'urn']);
}
}
$.catch.apply($, sys$.errors);
}
$.noAdditionalProperties(['target']);
return $.errors;
};
_this.type = _this.linkType.startsWith('Contentful:') ? 'ResourceLink' : 'Link';
return _this;
}
return EntityLinkAssertion;
}(NodeAssertion));
exports.EntityLinkAssertion = EntityLinkAssertion;
var HyperLinkAssertion = /** @class */ (function (_super) {
__extends(HyperLinkAssertion, _super);
function HyperLinkAssertion() {
var _this = _super.call(this, ['text'], function (data, path) { return _this.assertLink(data, path); }) || this;
_this.assertLink = function (data, path) {
var $ = new assert_1.ObjectAssertion(data, path);
$.string('uri');
$.noAdditionalProperties(['uri']);
return $.errors;
};
return _this;
}
return HyperLinkAssertion;
}(NodeAssertion));
exports.HyperLinkAssertion = HyperLinkAssertion;
var assert = function (contentRule, validateData) {
return new NodeAssertion(contentRule, validateData);
};
exports.assert = assert;
var assertLink = function (linkType, contentRule) {
return new EntityLinkAssertion(linkType, contentRule);
};
exports.assertLink = assertLink;
//# sourceMappingURL=node.js.map
;