UNPKG

@atlasrender/render-plugin

Version:

Atlas Render Farm Manager plugin system.

139 lines 5.11 kB
"use strict"; /* * Copyright (c) 2020. This code created and belongs to Pathfinder render manager project. * Owner and project architect: Danil Andreev | danssg08@gmail.com | https://github.com/DanilAndreev * File creator: Danil Andreev * Project: atlas-render-plugin * File last modified: 11/12/20, 5:19 PM * All rights reserved. */ Object.defineProperty(exports, "__esModule", { value: true }); const ValidationError_1 = require("../errors/ValidationError"); /** * PluginSetting - base class for plugin settings. * @class * @author Danil Andreev */ class PluginSetting { /** * Creates an instance of PluginSetting * @constructor * @param type - Type of the plugin setting. * @param setting - Setting object. * @throws ValidationError * @throws TypeError * @author Danil Andreev */ constructor(type, setting) { if ((setting === null || setting === void 0 ? void 0 : setting.id) && !(typeof (setting === null || setting === void 0 ? void 0 : setting.id) === "number" || typeof (setting === null || setting === void 0 ? void 0 : setting.id) == "string")) throw new TypeError(`Incorrect type of id setting, expected "string |number | undefined", got "${typeof (setting === null || setting === void 0 ? void 0 : setting.id)}"`); if ((setting === null || setting === void 0 ? void 0 : setting.id) && typeof (setting === null || setting === void 0 ? void 0 : setting.id) === "string" && (setting === null || setting === void 0 ? void 0 : setting.id.length) > 50) throw new TypeError(`id is too long. Expected string less than 50 characters.`); this.validation = new ValidationError_1.default("Validation error", undefined, { id: setting === null || setting === void 0 ? void 0 : setting.id }); if (typeof setting !== "object" || Array.isArray(setting)) throw new ValidationError_1.default("Fatal validation error: incorrect token.", [], { isFatal: true }); const { name, label, nullable } = setting; if (!PluginSetting.types.includes(type)) throw new ValidationError_1.default("Incorrect setting type.", undefined, { isFatal: true }); if (typeof name !== "string") this.validation.reject("name", "string", { got: typeof name }); else if (name.length > 20) this.validation.reject("name", "string", { message: "Name is too long.", status: 413 }); else if (!PluginSetting.validateName(name)) this.validation.reject("name", "string", { message: "Name contains restricted characters.", status: 414, got: name, }); if (typeof label !== "string") this.validation.reject("label", "string", { got: typeof name }); else if (label.length > 25) this.validation.reject("label", "string", { message: "Label is too long", status: 413, got: label }); this.setType(type); this.name = name; this.label = label; this.nullable = !!nullable; this.id = setting.id; } /** * validateName - function, designed to validate setting name. * @method * @param input - Input data. Is should be correct string to get true. * @author Danil Andreev */ static validateName(input) { if (typeof input !== "string") return false; return PluginSetting.nameRegExp.test(input); } /** * setType - sets the type of the field. * @method * @author Danil Andreev */ setType(type) { this.type = type; } /** * getType - returns a type of the field. * @method * @author Danil Andreev */ getType() { return this.type; } /** * isValid - if validation has no errors will return true, else - false. * @method * @author Danil Andreev */ isValid() { return !this.validation.hasErrors(); } /** * getValidation - returns validation error object. * @method * @author Danil Andreev */ getValidation() { return this.validation; } /** * validatePayload - validates payload. * If it is OK - function will return payload. * If not - throw a Validation Error. * @method * @param payload - Any payload. * @throws ReferenceError * @throws ValidationError * @author Danil Andreev */ validatePayload(payload) { throw new ReferenceError(`This method must be overridden by ral field!`); } getJSON() { return { type: this.type, name: this.name, label: this.label, id: this.id, }; } } exports.default = PluginSetting; /** * types - available setting types. */ PluginSetting.types = [ "float", "integer", "string", "boolean", "group", "separator", ]; /** * nameRegExp - regular expression for setting name correctness checks. */ PluginSetting.nameRegExp = new RegExp("^[a-zA-Z_$][a-zA-Z_$0-9]*$"); //# sourceMappingURL=PluginSetting.js.map