@contentful/rich-text-types
Version:
Type definitions and constants for the Contentful rich text field type.
280 lines (279 loc) • 9.97 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
Object.defineProperty(exports, "ObjectAssertion", {
enumerable: true,
get: function() {
return ObjectAssertion;
}
});
var _isplainobj = /*#__PURE__*/ _interop_require_default(require("is-plain-obj"));
var _errors = require("./errors.js");
function _array_like_to_array(arr, len) {
if (len == null || len > arr.length) len = arr.length;
for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
return arr2;
}
function _array_without_holes(arr) {
if (Array.isArray(arr)) return _array_like_to_array(arr);
}
function _class_call_check(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a function");
}
}
function _defineProperties(target, props) {
for(var i = 0; i < props.length; i++){
var descriptor = props[i];
descriptor.enumerable = descriptor.enumerable || false;
descriptor.configurable = true;
if ("value" in descriptor) descriptor.writable = true;
Object.defineProperty(target, descriptor.key, descriptor);
}
}
function _create_class(Constructor, protoProps, staticProps) {
if (protoProps) _defineProperties(Constructor.prototype, protoProps);
if (staticProps) _defineProperties(Constructor, staticProps);
return Constructor;
}
function _define_property(obj, key, value) {
if (key in obj) {
Object.defineProperty(obj, key, {
value: value,
enumerable: true,
configurable: true,
writable: true
});
} else {
obj[key] = value;
}
return obj;
}
function _interop_require_default(obj) {
return obj && obj.__esModule ? obj : {
default: obj
};
}
function _iterable_to_array(iter) {
if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
}
function _non_iterable_spread() {
throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
}
function _to_consumable_array(arr) {
return _array_without_holes(arr) || _iterable_to_array(arr) || _unsupported_iterable_to_array(arr) || _non_iterable_spread();
}
function _unsupported_iterable_to_array(o, minLen) {
if (!o) return;
if (typeof o === "string") return _array_like_to_array(o, minLen);
var n = Object.prototype.toString.call(o).slice(8, -1);
if (n === "Object" && o.constructor) n = o.constructor.name;
if (n === "Map" || n === "Set") return Array.from(n);
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen);
}
var ObjectAssertion = /*#__PURE__*/ function() {
"use strict";
function ObjectAssertion(obj, path) {
var _this = this;
var _this1 = this;
_class_call_check(this, ObjectAssertion);
_define_property(this, "obj", void 0);
_define_property(this, "path", void 0);
_define_property(this, "_errors", void 0);
_define_property(this, "catch", void 0);
_define_property(this, "exists", void 0);
_define_property(this, "object", void 0);
_define_property(this, "string", void 0);
_define_property(this, "number", void 0);
_define_property(this, "array", void 0);
_define_property(this, "enum", void 0);
_define_property(this, "empty", void 0);
_define_property(this, "minLength", void 0);
_define_property(this, "noAdditionalProperties", void 0);
_define_property(this, "each", void 0);
this.obj = obj;
this.path = path;
this._errors = [];
this.catch = function() {
for(var _len = arguments.length, errors = new Array(_len), _key = 0; _key < _len; _key++){
errors[_key] = arguments[_key];
}
var _this__errors;
(_this__errors = _this1._errors).push.apply(_this__errors, _to_consumable_array(errors));
};
this.exists = function(key) {
if (key in _this.obj) {
return true;
}
_this.catch((0, _errors.requiredPropertyError)({
property: key,
path: _this.path.of(key)
}));
return false;
};
this.object = function(key) {
var value = key ? _this.obj[key] : _this.obj;
if (key) {
if (!_this.exists(key)) {
return false;
}
}
if ((0, _isplainobj.default)(value)) {
return true;
}
var _$path = key ? _this.path.of(key) : _this.path;
var _ref;
var property = (_ref = key !== null && key !== void 0 ? key : _this.path.last()) !== null && _ref !== void 0 ? _ref : 'value';
_this.catch((0, _errors.typeMismatchError)({
typeName: 'Object',
property: property,
path: _$path,
value: value
}));
return false;
};
this.string = function(key) {
var value = _this.obj[key];
if (key && !_this.exists(key)) {
return false;
}
if (typeof value === 'string') {
return true;
}
_this.catch((0, _errors.typeMismatchError)({
typeName: 'String',
property: key,
path: _this.path.of(key),
value: value
}));
return false;
};
this.number = function(key, optional) {
var value = _this.obj[key];
if (optional && !(key in _this.obj)) {
return true;
}
if (!_this.exists(key)) {
return false;
}
if (typeof value === 'number' && !Number.isNaN(value)) {
return true;
}
_this.catch((0, _errors.typeMismatchError)({
typeName: 'Number',
property: key,
path: _this.path.of(key),
value: value
}));
return false;
};
this.array = function(key) {
var value = _this.obj[key];
if (key && !_this.exists(key)) {
return false;
}
if (Array.isArray(value)) {
return true;
}
_this.catch((0, _errors.typeMismatchError)({
typeName: 'Array',
property: key,
path: _this.path.of(key),
value: value
}));
return false;
};
this.enum = function(key, expected) {
var value = _this.obj[key];
if (typeof value === 'string' && expected.includes(value)) {
return true;
}
_this.catch((0, _errors.enumError)({
expected: expected,
value: value,
path: _this.path.of(key)
}));
return false;
};
this.empty = function(key) {
if (!_this.array(key)) {
return false;
}
var value = _this.obj[key];
if (value.length === 0) {
return true;
}
_this.catch((0, _errors.maxSizeError)({
max: 0,
value: value,
path: _this.path.of(key)
}));
return false;
};
this.minLength = function(key, min) {
if (!_this.array(key)) {
return false;
}
var value = _this.obj[key];
if (value.length >= min) {
return true;
}
_this.catch((0, _errors.minSizeError)({
min: min,
value: value,
path: _this.path.of(key)
}));
return false;
};
this.noAdditionalProperties = function(properties) {
var unknowns = Object.keys(_this.obj).sort().filter(function(key) {
return !properties.includes(key);
});
unknowns.forEach(function(property) {
return _this.catch((0, _errors.unknownPropertyError)({
property: property,
path: _this.path.of(property)
}));
});
return unknowns.length === 0;
};
this.each = function(key, assert) {
if (!_this.array(key)) {
return;
}
var value = _this.obj[key];
var foundErrors = false;
value.forEach(function(item, index) {
if (foundErrors) {
return;
}
var errors = assert(item, _this.path.of(key).of(index));
if (errors.length > 0) {
foundErrors = true;
}
_this.catch.apply(_this, _to_consumable_array(errors));
});
};
}
_create_class(ObjectAssertion, [
{
key: "errors",
get: function get() {
var _this = this;
var serializeError = function(error) {
return JSON.stringify({
details: error.details,
path: error.path
});
};
return this._errors.filter(function(error, index) {
return _this._errors.findIndex(function(step) {
return serializeError(error) === serializeError(step);
}) === index;
});
}
}
]);
return ObjectAssertion;
}();