@mdf.js/tasks
Version:
MMS - API Core - Tasks
220 lines • 8.9 kB
JavaScript
/**
* Copyright 2024 Mytra Control S.L. All rights reserved.
*
* Use of this source code is governed by an MIT-style license that can be found in the LICENSE file
* or at https://opensource.org/licenses/MIT.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.Validator = void 0;
const tslib_1 = require("tslib");
const crash_1 = require("@mdf.js/crash");
const ms_1 = tslib_1.__importDefault(require("ms"));
class Validator {
/**
* Validate the resources configuration
* @param resources - The resources configuration
*/
static validateResources(resources) {
if (!resources || typeof resources !== 'object' || Array.isArray(resources)) {
throw new crash_1.Crash(`The resources should be an object: ${JSON.stringify(resources, null, 2)}`);
}
for (const [resource, entry] of Object.entries(resources)) {
if (!entry || typeof entry !== 'object' || Array.isArray(entry)) {
throw new crash_1.Crash(`The resource entry should be an object: ${JSON.stringify(entry, null, 2)}`);
}
if (typeof resource !== 'string' || resource.length < 1) {
throw new crash_1.Crash(`The resource should be a non empty string: ${JSON.stringify(resource, null, 2)}`);
}
Validator.valideEntry(entry);
}
}
/**
* Check if the entry is valid
* @param entry - The entry to add to the scheduler
*/
static valideEntry(entry) {
for (const [polling, tasks] of Object.entries(entry.pollingGroups)) {
Validator.isValidPeriod(polling);
if (!Array.isArray(tasks)) {
throw new crash_1.Crash(`The tasks should be an array of tasks: ${JSON.stringify(tasks, null, 2)}`);
}
for (const task of tasks) {
Validator.isValidConfig(task);
}
}
}
/**
* Check if the task configuration is a single task configuration
* @param config - The task configuration
*/
static isSingleTaskConfig(config) {
try {
Validator.isValidConfig(config);
return 'task' in config;
}
catch {
return false;
}
}
/**
* Check if the task configuration is a group task configuration
* @param config - The task configuration
*/
static isGroupTaskConfig(config) {
try {
Validator.isValidConfig(config);
return 'tasks' in config;
}
catch {
return false;
}
}
/**
* Check if the task configuration is a sequence task configuration
* @param config - The task configuration
*/
static isSequenceTaskConfig(config) {
try {
Validator.isValidConfig(config);
return 'pattern' in config;
}
catch {
return false;
}
}
/** Check if the period is valid */
static isValidPeriod(period) {
if (typeof period !== 'string' || period.length < 2 || !Validator.isValidEndForPeriod(period)) {
throw new crash_1.Crash(`The period should be a string with the format <number><ms|s|m|h|d>`);
}
else {
try {
const value = (0, ms_1.default)(period);
if (typeof value !== 'number' || Number.isNaN(value) || value < 0) {
throw new crash_1.Crash(`Wrong period value [${period}]`);
}
}
catch (rawError) {
const error = crash_1.Crash.from(rawError);
throw new crash_1.Crash(`The period could not be parsed: ${error.message}, the period should be a string with the format <number><ms|s|m|h|d>`);
}
}
}
/**
* Check if the period is based on milliseconds, seconds, minutes, hours or days
* @param period - The period to check
* @returns - True if the period is valid
*/
static isValidEndForPeriod(period) {
return (period.endsWith('ms') ||
period.endsWith('s') ||
period.endsWith('m') ||
period.endsWith('h') ||
period.endsWith('d'));
}
/** Check if the configuration is valid */
static isValidConfig(config) {
if (!config || typeof config !== 'object' || Array.isArray(config)) {
throw new crash_1.Crash(`The task configuration should be an object`);
}
else if ('task' in config) {
Validator.isValidSingleTaskConfig(config);
}
else if ('tasks' in config) {
Validator.isValidGroupConfig(config);
}
else if ('pattern' in config) {
Validator.isValidSequenceConfig(config);
}
else {
throw new crash_1.Crash(`The task configuration should have a task, tasks or pattern property`);
}
}
/**
* Check if the task configuration is valid
* @param config - The task configuration
*/
static isValidSingleTaskConfig(config) {
if (typeof config.task !== 'function') {
throw new crash_1.Crash(`The task should be a function or a promise: ${JSON.stringify(config, null, 2)}`);
}
else if ('taskArgs' in config && !Array.isArray(config.taskArgs)) {
throw new crash_1.Crash(`The taskArgs should be an array: ${JSON.stringify(config, null, 2)}`);
}
else {
Validator.isValidTaskOptions(config.options);
}
}
/**
* Check if the task options are valid
* @param options - The task options
*/
static isValidTaskOptions(options) {
if (!options || typeof options !== 'object' || Array.isArray(options)) {
throw new crash_1.Crash(`The options should be an object: ${JSON.stringify(options, null, 2)}`);
}
else if (!('id' in options)) {
throw new crash_1.Crash(`The options should have an id property: ${JSON.stringify(options, null, 2)}`);
}
else if (typeof options.id !== 'string') {
throw new crash_1.Crash(`The id should be a string: ${JSON.stringify(options, null, 2)}`);
}
else if (options.id.length < 1) {
throw new crash_1.Crash(`The id should be a non empty string: ${JSON.stringify(options, null, 2)}`);
}
else if (options.id.length > 255) {
throw new crash_1.Crash(`The id should be a string with less than 255 characters: ${JSON.stringify(options, null, 2)}`);
}
}
/**
* Check if the group configuration is valid
* @param config - The group configuration
*/
static isValidGroupConfig(config) {
if (!Array.isArray(config.tasks)) {
throw new crash_1.Crash(`The tasks should be an array of tasks: ${JSON.stringify(config, null, 2)}`);
}
else {
for (const task of config.tasks) {
Validator.isValidSingleTaskConfig(task);
}
Validator.isValidTaskOptions(config.options);
}
}
/**
* Check if the sequence configuration is valid
* @param config - The sequence configuration
*/
static isValidSequenceConfig(config) {
// Helper to validate array properties
const validateArrayProperty = (property) => {
if (property in config.pattern && !Array.isArray(config.pattern[property])) {
throw new crash_1.Crash(`The ${property} property should be an array of tasks: ${JSON.stringify(config, null, 2)}`);
}
};
// Ensure `pattern` is a valid object with a `task` property
if (!config.pattern || typeof config.pattern !== 'object' || Array.isArray(config.pattern)) {
throw new crash_1.Crash(`Pattern should be an object with the task property: ${JSON.stringify(config, null, 2)}`);
}
if (!config.pattern.task ||
typeof config.pattern.task !== 'object' ||
Array.isArray(config.pattern.task)) {
throw new crash_1.Crash(`The sequence configuration should have a task property: ${JSON.stringify(config, null, 2)}`);
}
// Validate that `pre`, `post`, and `finally` properties (if present) are arrays
['pre', 'post', 'finally'].forEach(validateArrayProperty);
// Validate each array of tasks in `pre`, `post`, and `finally` if they exist
['pre', 'post', 'finally'].forEach(property => {
if (Array.isArray(config.pattern[property])) {
for (const task of config.pattern[property]) {
Validator.isValidSingleTaskConfig(task);
}
}
});
// Validate any additional task options
Validator.isValidTaskOptions(config.options);
}
}
exports.Validator = Validator;
//# sourceMappingURL=Validator.js.map
;