UNPKG

@atlasrender/render-plugin

Version:

Atlas Render Farm Manager plugin system.

164 lines 5.91 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:42 PM * All rights reserved. */ Object.defineProperty(exports, "__esModule", { value: true }); const Validator_1 = require("./Validator"); const _ = require("lodash"); /** * ValidationError - validation error for plugin setting. * @class * @author Danil Andreev */ class ValidationError extends TypeError { /** * Creates an instance of SettingsValidationError * @param message - String message. * @param validation - Validation map object. * @param options - Validation error additional options. * @author Danil Andreev */ constructor(message, validation = [], options = {}) { super(message); this.validation = validation; this.fatalError = !!options.isFatal; this.nested = []; this.id = options.id; } /** * createValidationError - creates ValidationError instance from input structure. * @method * @param input - Any input data. Will be checked and validated. * @throws TypeError * @author Danil Andreev */ static createValidationError(input) { if (typeof input !== "object") throw new TypeError(`Invalid 'input' type, expected "object", got "${typeof input}".`); if (typeof input.message !== "string") throw new TypeError(`Invalid 'input.message' type, expected "string", got "${typeof input.message}".`); if (input.fatalError && typeof input.fatalError !== "boolean") throw new TypeError(`Invalid 'input.fatalError' type, expected "boolean", got "${typeof input.fatalError}".'`); if (input.validation && !Array.isArray(input.validation)) throw new TypeError(`Invalid 'input.validation' type, expected "Validator[]", got "${typeof input.validation}".'`); if (input.nested && !Array.isArray(input.nested)) throw new TypeError(`Invalid 'input.nested' type, expected "Validator[]", got "${typeof input.nested}".'`); let validation = []; if (input.validation) validation = input.validation.map(item => Validator_1.default.createValidator(item)); let nested = []; if (input.nested) nested = input.nested.map(item => ValidationError.createValidationError(item)); const result = new ValidationError(input.message, validation, input.fatalError || false); result.addNested(nested); return result; } /** * getValidation - method for getting validation map from error. * @method * @author Danil Andreev */ getValidation() { return this.validation; } /** * getNested - method for getting nested errors. * @method * @author Danil Andreev */ getNested() { return this.nested; } /** * failValidation - function, designed to fail validation with fatal error. * @method * @author Danil Andreev */ failValidation() { this.fatalError = true; return this; } /** * isFatal - if validation failed with fatal error - returns true, else - false. * @method * @author Danil Andreev */ isFatal() { return this.fatalError; } /** * hasErrors - method, designed to check if this error has validators. * @method * @author Danil Andreev */ hasErrors() { return !!this.validation.length || !!this.fatalError || !!this.nested.length; } /** * errorOn - returns error if it is error on selected field or undefined if not. * @method * @param key - Key of the field. * @author Danil Andreev */ errorOn(key) { return this.validation.find((candidate) => candidate.key === key); } /** * reject - method, designed to allow setting element rejection. * @method * @param name - The key of an element in setting. * @param expected - Expected value of an element. * @param options - Options for more detailed setup. * @author Danil Andreev */ reject(name, expected, options) { if (!this.validation.some((candidate) => candidate.key === name)) this.validation.push(new Validator_1.default(name, expected, options)); return this; } addNested(error) { if (Array.isArray(error)) this.nested = this.nested.concat(error); else this.nested.push(error); return this; } getJSON() { return { id: this.id, message: this.message, fatalError: this.fatalError, nested: this.nested.map(item => item.getJSON()), validation: this.validation.map(item => item.getJSON()), }; } /** * getFlatErrorsList - returns flatten errors list with all nested errors and errors nested in validators. * @method * @author Danil Andreev */ getFlatErrorsList() { let errors = []; errors = errors.concat(this.nested); errors = errors.concat(_.flatten(this.nested.map((item) => item.getFlatErrorsList()))); errors.concat(_.flatten(this.validation.map((validator) => (validator.getNested().map((item) => item.getFlatErrorsList()))))); return errors; } /** * getErrorOnId - returns error with custom id that matches input id. * If not found - returns false. * @method * @param id - Target id. * @author Danil Adnreev */ getErrorOnId(id) { return this.getFlatErrorsList().find((item) => item.id === id); } } exports.default = ValidationError; //# sourceMappingURL=ValidationError.js.map