@atlasrender/render-plugin
Version:
Atlas Render Farm Manager plugin system.
136 lines • 4.86 kB
JavaScript
;
/*
* 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:47 PM
* All rights reserved.
*/
Object.defineProperty(exports, "__esModule", { value: true });
const ValidationError_1 = require("./ValidationError");
/**
* Validator - class for declaring plugin setting validation error token.
* @class
* @author Danil Andreev
*/
class Validator {
/**
* Creates an instance of Validator.
* @constructor
* @param key - The key of an element in setting.
* @param expected - Expected value of an element.
* @param options - Options for more detailed setup.
* @author Danil Andreev
*/
constructor(key, expected, options = {}) {
this.key = key;
this.expected = expected;
this.message = options.message;
this.status = options.status;
this.got = options.got;
this.nested = Array.isArray(options.nested) ? [...options.nested] : [];
}
/**
* compareCode - returns true if expected code is subset of current
* @param expected - Code for checking.
* @param current - Code from validation.
* @example
* Validator.compareCode(420, 421) === true
* Validator.compareCode(400, 421) === true
* Validator.compareCode(422, 421) === false
* Validator.compareCode(431, 421) === false
*/
static compareCode(expected, current) {
const expectedStr = String(expected);
const currentStr = String(current);
for (let i = 0; i < currentStr.length; i++) {
if (expectedStr[i] && expectedStr[i] !== "0" && expectedStr[i] !== currentStr[i])
return false;
}
return true;
}
/**
* createValidator - creates Validator instance from input structure.
* @method
* @param input - Any input data. Will be checked and validated.
* @throws TypeError
* @author Danil Andreev
*/
static createValidator(input) {
if (typeof input !== "object")
throw new TypeError(`Incorrect type of validator, expected "object", got "${typeof input}"`);
if (typeof input.key !== "string")
throw new TypeError(`Incorrect type of 'key' field, expected "string", got "${typeof input.key}"`);
if (typeof input.expected !== "string")
throw new TypeError(`Incorrect type of 'expected' field, expected "string", got "${typeof input.expected}"`);
if (input.message && typeof input.message !== "string")
throw new TypeError(`Incorrect type of 'message' field, expected "string | undefined", got "${typeof input.message}"`);
if (input.status && typeof input.status !== "number")
throw new TypeError(`Incorrect type of 'status' field, expected "number | undefined", got "${typeof input.status}"`);
if (input.nested && !Array.isArray(input.nested))
throw new TypeError(`Incorrect type of 'nested' field, expected "ValidationError[]", got "${typeof input.nested}"`);
let nested = [];
if (input.nested)
nested = input.nested.map(item => ValidationError_1.default.createValidationError(item));
const result = new Validator(input.key, input.expected, {
message: input.message,
status: input.status,
got: input.got,
nested: nested,
});
return result;
}
/**
* getNested - returns an array of nested errors.
* @method
* @author Danil Andreev
*/
getNested() {
return this.nested;
}
/**
* addNested - adds nested validation error to object.
* @param error - Error object.
*/
addNested(error) {
this.nested.push(error);
return this;
}
getJSON() {
return {
key: this.key,
expected: this.expected,
message: this.message,
status: this.status,
got: this.got,
nested: this.nested.map(item => item.getJSON()),
};
}
}
exports.default = Validator;
/**
* Codes - available error codes for validation.
*/
Validator.Codes = {
// 1XX Common errors
INVALID_PAYLOAD: 100,
// 2XX - Type errors
INVALID_TYPE: 200,
INVALID_NUMBER: 210,
INVALID_INTEGER: 211,
INVALID_BOOLEAN: 220,
INVALID_OBJECT: 230,
INVALID_KEY: 240,
INVALID_STRING: 250,
INVALID_ARRAY: 260,
// 3XX - Boundary errors
OUT_OF_BOUNDS: 300,
HIGHER_THAN_MAX: 310,
TOO_BIG_VALUE: 312,
LOWER_THAN_MIN: 320,
LOWER_THAN_ZERO: 321,
TOO_SMALL_VALUE: 322,
MIN_HIGHER_THAN_MAX: 330,
};
//# sourceMappingURL=Validator.js.map