@codecovevienna/gittt-cli
Version:
Tracking time with CLI into a git repository
189 lines (188 loc) • 5.75 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ValidationHelper = void 0;
const fs_1 = __importDefault(require("fs"));
const fs_extra_1 = __importDefault(require("fs-extra"));
const _1 = require("./");
class ValidationHelper {
}
exports.ValidationHelper = ValidationHelper;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
ValidationHelper.validateNumber = (input, from, to) => {
if (!isNaN(input)) {
const inputNumber = parseInt(input, 10);
if (typeof from === "number" && typeof to === "number") {
if (inputNumber >= from && inputNumber <= to) {
return true;
}
else {
return false;
}
}
else {
return true;
}
}
else {
return false;
}
};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
ValidationHelper.validateYear = (input) => {
if (ValidationHelper.validateNumber(input)) {
return true;
}
else {
return "The year has to be a number";
}
};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
ValidationHelper.validateMonth = (input) => {
if (ValidationHelper.validateNumber(input, 1, 12)) {
return true;
}
else {
return "The month has to be a number between 1 and 12";
}
};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
ValidationHelper.validateDay = (input) => {
if (ValidationHelper.validateNumber(input, 1, 31)) {
return true;
}
else {
return "The day has to be a number between 1 and 31";
}
};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
ValidationHelper.validateHour = (input) => {
if (ValidationHelper.validateNumber(input, 0, 23)) {
return true;
}
else {
return "The hour has to be a number between 0 and 23";
}
};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
ValidationHelper.validateMinute = (input) => {
if (ValidationHelper.validateNumber(input, 0, 59)) {
return true;
}
else {
return "The minute has to be a number between 0 and 59";
}
};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
ValidationHelper.validateAmount = (input) => {
if (ValidationHelper.validateNumber(input)) {
return true;
}
else {
return "The amount has to be a number";
}
};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
ValidationHelper.validateGitUrl = (input) => {
try {
// Will throw if parsing fails
(0, _1.parseProjectNameFromGitUrl)(input);
return true;
}
catch (err) {
return "The url has to look like ssh://git@github.com:22/gittt/project.git";
}
};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
ValidationHelper.validateUsername = (input) => {
if (!input) {
return "No input provided";
}
const inputString = input;
if (new RegExp("^[a-z0-9_-]{3,64}$").test(inputString)) {
return true;
}
else {
return "The username has to be a 3 to 64 characters long";
}
};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
ValidationHelper.validatePassword = (input) => {
if (!input) {
return "No input provided";
}
const inputString = input;
if (new RegExp("^[a-z0-9_-]{3,64}$").test(inputString)) {
return true;
}
else {
return "The password has to be a 3 to 64 characters long";
}
};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
ValidationHelper.validateClientSecret = (input) => {
if (!input) {
return "No input provided";
}
const inputString = input;
if (new RegExp("^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$").test(inputString)) {
return true;
}
else {
return "The provided string does not seem to be a valid client secret";
}
};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
ValidationHelper.validateJiraEndpoint = (input) => {
// TODO improve
const inputString = input;
if (new RegExp("^(http://|https://).+").test(inputString)) {
return true;
}
else {
return "The endpoint has to be a valid url";
}
};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
ValidationHelper.validateJiraKey = (input) => {
const inputString = input;
if (inputString.length > 1) {
return true;
}
else {
return "The key has to be longer than one character";
}
};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
ValidationHelper.validateJiraIssueKey = (input) => {
const inputString = input;
if (inputString.length > 0) {
const issueRegex = new RegExp('[A-Z]+-[1-9]+');
if (issueRegex.test(inputString)) {
return true;
}
return "The issue key has to contain a dash ('-')";
}
else {
return true;
}
};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
ValidationHelper.validateFile = (input) => {
if (typeof input === 'string') {
try {
const inputFilePath = input;
const stats = fs_extra_1.default.statSync(inputFilePath);
if (stats.isFile()) {
fs_extra_1.default.accessSync(inputFilePath, fs_1.default.constants.R_OK | fs_1.default.constants.W_OK);
return true;
}
}
catch (err) {
return false;
}
}
return false;
};