UNPKG

shyft

Version:

Model driven GraphQL API framework

129 lines (128 loc) 5.82 kB
"use strict"; 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 __()); }; })(); var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.buildListDataType = exports.isListDataType = exports.ListDataType = void 0; var util_1 = require("../util"); var Entity_1 = require("../entity/Entity"); var DataType_1 = require("./DataType"); var ComplexDataType_1 = require("./ComplexDataType"); var ListDataType = /** @class */ (function (_super) { __extends(ListDataType, _super); function ListDataType(setup) { if (setup === void 0) { setup = {}; } var _this = _super.call(this) || this; _this.validate = function (_a) { var value = _a.value; if (value) { if (!util_1.isArray(value)) { throw new Error("List data type '" + _this.name + "' expects an array of items"); } if (value.length < _this.minItems) { throw new Error("List data type '" + _this.name + "' requires a minimum of " + _this.minItems + " items"); } if (_this.maxItems !== 0 && value.length > _this.maxItems) { throw new Error("List data type '" + _this.name + "' requires a maximum of " + _this.maxItems + " items"); } } }; var name = setup.name, description = setup.description, itemType = setup.itemType, minItems = setup.minItems, maxItems = setup.maxItems; util_1.passOrThrow(name, function () { return 'Missing list data type name'; }); util_1.passOrThrow(description, function () { return "Missing description for list data type '" + name + "'"; }); util_1.passOrThrow(itemType, function () { return "Missing item type for list data type '" + name + "'"; }); util_1.passOrThrow(DataType_1.isDataType(itemType) || Entity_1.isEntity(itemType) || ComplexDataType_1.isComplexDataType(itemType) || util_1.isFunction(itemType), function () { return "List data type '" + name + "' has invalid item type '" + String(itemType) + "'"; }); var _minItems = minItems || 0; var _maxItems = maxItems || 0; if (_minItems) { util_1.passOrThrow(Number.isInteger(_minItems) && _minItems >= 0, function () { return "List data type '" + name + "' has invalid minItems setting '" + _minItems + "'"; }); } if (_maxItems) { util_1.passOrThrow(Number.isInteger(_maxItems) && _maxItems >= 0, function () { return "List data type '" + name + "' has invalid maxItems setting '" + _maxItems + "'"; }); } util_1.passOrThrow(_minItems <= _maxItems || _maxItems === 0, function () { return "List data type '" + name + "' has a bigger minItems than the maxItems setting"; }); _this.name = name; _this.description = description; _this.itemType = itemType; _this.minItems = _minItems; _this.maxItems = _maxItems; return _this; } ListDataType.prototype._processItemType = function () { var _this = this; if (util_1.isFunction(this.itemType)) { var itemTypeBuilder = this .itemType; var itemType_1 = itemTypeBuilder({ setup: { name: this.name, description: this.description, }, }); util_1.passOrThrow(DataType_1.isDataType(itemType_1) || Entity_1.isEntity(itemType_1) || ComplexDataType_1.isComplexDataType(itemType_1), function () { return "List data type '" + _this.name + "' has invalid dynamic item type '" + String(itemType_1) + "'"; }); return itemType_1; } return this.itemType; }; ListDataType.prototype.getItemType = function () { if (this._itemType) { return this._itemType; } var ret = (this._itemType = this._processItemType()); return ret; }; ListDataType.prototype.toString = function () { return this.name; }; return ListDataType; }(ComplexDataType_1.ComplexDataType)); exports.ListDataType = ListDataType; var isListDataType = function (obj) { return obj instanceof ListDataType; }; exports.isListDataType = isListDataType; var buildListDataType = function (obj) { return function (_a) { var _b = _a.setup, name = _b.name, description = _b.description; return new ListDataType(__assign(__assign({ description: description }, obj), { name: name })); }; }; exports.buildListDataType = buildListDataType;