@compas/cli
Version:
CLI containing utilities and simple script runner
1,364 lines (1,323 loc) • 49.8 kB
JavaScript
// Generated by @compas/code-gen
/**
* @template T, E
* @typedef {{ value: T, error?: never}|{ value?: never, error: E }} Either
*/
/**
* @typedef {Record<string, any|undefined>} ValidatorErrorMap
*/
// eslint-disable-next-line no-unused-vars
const isRecord = (v) => !!v && typeof v === "object" && !Array.isArray(v);
/**
* @param {import("../common/types.js").CliCommandDefinitionInput|any} value
* @returns {Either<import("../common/types.js").CliCommandDefinition, ValidatorErrorMap>}
*/
export function validateCliCommandDefinition(value) {
/** @type {ValidatorErrorMap} */
const errorMap = {};
/** @type {any} */
let result = undefined;
if (value === null || value === undefined) {
errorMap[`$`] = {
key: "validator.undefined",
};
}
else {
if (!isRecord(value)) {
errorMap[`$`] = {
key: "validator.object",
value: value,
foundType: typeof value,
};
}
else {
/** @type {Set<string>} */
const knownKeys0 = new Set([
"name",
"shortDescription",
"longDescription",
"modifiers",
"dynamicValue",
"watchSettings",
"subCommands",
"flags",
"executor",
]);
for (const key of Object.keys(value)) {
if (!knownKeys0.has(key) && value[key] !== null && value[key] !== undefined) {
const expectedKeys = [...knownKeys0];
const foundKeys = Object.keys(value);
const unknownKeys = foundKeys.filter(it => !knownKeys0.has(it));
errorMap[`$`] = {
key: "validator.keys",
unknownKeys,
expectedKeys,
foundKeys,
};
break;
}
}
result = { "name": undefined, "shortDescription": undefined, "longDescription": undefined, "modifiers": undefined, "dynamicValue": undefined, "watchSettings": undefined, "subCommands": undefined, "flags": undefined, "executor": undefined,};
if (value["name"] === null || value["name"] === undefined) {
errorMap[`$.name`] = {
key: "validator.undefined",
};
}
else {
/** @type {string} */
let convertedString1 = value["name"];
if (typeof convertedString1 !== "string") {
errorMap[`$.name`] = {
key: "validator.string",
};
}
else {
if (convertedString1.length < 1) {
errorMap[`$.name`] = {
key: "validator.length",
minLength: 1
};
} else if (!/^[a-z-]+$/g.test(convertedString1)) {
errorMap[`$.name`] = {
key: "validator.pattern",
};
} else {
result["name"] = convertedString1;
}
}
}
if (value["shortDescription"] === null || value["shortDescription"] === undefined) {
errorMap[`$.shortDescription`] = {
key: "validator.undefined",
};
}
else {
/** @type {string} */
let convertedString2 = value["shortDescription"];
if (typeof convertedString2 !== "string") {
errorMap[`$.shortDescription`] = {
key: "validator.string",
};
}
else {
if (convertedString2.length < 1) {
errorMap[`$.shortDescription`] = {
key: "validator.length",
minLength: 1
};
} else if (!/^[^\n]+$/g.test(convertedString2)) {
errorMap[`$.shortDescription`] = {
key: "validator.pattern",
};
} else {
result["shortDescription"] = convertedString2;
}
}
}
if (value["longDescription"] === null || value["longDescription"] === undefined) {
result["longDescription"] = undefined;
}
else {
/** @type {string} */
let convertedString3 = value["longDescription"];
if (typeof convertedString3 !== "string") {
errorMap[`$.longDescription`] = {
key: "validator.string",
};
}
else {
if (convertedString3.length === 0) {
result["longDescription"] = undefined;
}
else {
if (convertedString3.length < 1) {
errorMap[`$.longDescription`] = {
key: "validator.length",
minLength: 1
};
} else {
result["longDescription"] = convertedString3;
}
}
}
}
if (value["modifiers"] === null || value["modifiers"] === undefined) {
result["modifiers"] = {"isDynamic":false,"isCosmetic":false,"isWatchable":false};
}
else {
if (!isRecord(value["modifiers"])) {
errorMap[`$.modifiers`] = {
key: "validator.object",
value: value["modifiers"],
foundType: typeof value["modifiers"],
};
}
else {
/** @type {Set<string>} */
const knownKeys4 = new Set([
"isDynamic",
"isCosmetic",
"isWatchable",
]);
for (const key of Object.keys(value["modifiers"])) {
if (!knownKeys4.has(key) && value["modifiers"][key] !== null && value["modifiers"][key] !== undefined) {
const expectedKeys = [...knownKeys4];
const foundKeys = Object.keys(value["modifiers"]);
const unknownKeys = foundKeys.filter(it => !knownKeys4.has(it));
errorMap[`$.modifiers`] = {
key: "validator.keys",
unknownKeys,
expectedKeys,
foundKeys,
};
break;
}
}
result["modifiers"] = { "isDynamic": undefined, "isCosmetic": undefined, "isWatchable": undefined,};
if (value["modifiers"]["isDynamic"] === null || value["modifiers"]["isDynamic"] === undefined) {
result["modifiers"]["isDynamic"] = false;
}
else {
if (value["modifiers"]["isDynamic"] === true || value["modifiers"]["isDynamic"] === "true" || value["modifiers"]["isDynamic"] === 1 || value["modifiers"]["isDynamic"] === "1") {
result["modifiers"]["isDynamic"] = true;
}
else if (value["modifiers"]["isDynamic"] === false || value["modifiers"]["isDynamic"] === "false" || value["modifiers"]["isDynamic"] === 0 || value["modifiers"]["isDynamic"] === "0") {
result["modifiers"]["isDynamic"] = false;
}
else {
errorMap[`$.modifiers.isDynamic`] = {
key: "validator.type",
expectedType: "boolean",
};
}
}
if (value["modifiers"]["isCosmetic"] === null || value["modifiers"]["isCosmetic"] === undefined) {
result["modifiers"]["isCosmetic"] = false;
}
else {
if (value["modifiers"]["isCosmetic"] === true || value["modifiers"]["isCosmetic"] === "true" || value["modifiers"]["isCosmetic"] === 1 || value["modifiers"]["isCosmetic"] === "1") {
result["modifiers"]["isCosmetic"] = true;
}
else if (value["modifiers"]["isCosmetic"] === false || value["modifiers"]["isCosmetic"] === "false" || value["modifiers"]["isCosmetic"] === 0 || value["modifiers"]["isCosmetic"] === "0") {
result["modifiers"]["isCosmetic"] = false;
}
else {
errorMap[`$.modifiers.isCosmetic`] = {
key: "validator.type",
expectedType: "boolean",
};
}
}
if (value["modifiers"]["isWatchable"] === null || value["modifiers"]["isWatchable"] === undefined) {
result["modifiers"]["isWatchable"] = false;
}
else {
if (value["modifiers"]["isWatchable"] === true || value["modifiers"]["isWatchable"] === "true" || value["modifiers"]["isWatchable"] === 1 || value["modifiers"]["isWatchable"] === "1") {
result["modifiers"]["isWatchable"] = true;
}
else if (value["modifiers"]["isWatchable"] === false || value["modifiers"]["isWatchable"] === "false" || value["modifiers"]["isWatchable"] === 0 || value["modifiers"]["isWatchable"] === "0") {
result["modifiers"]["isWatchable"] = false;
}
else {
errorMap[`$.modifiers.isWatchable`] = {
key: "validator.type",
expectedType: "boolean",
};
}
}
}
}
if (value["dynamicValue"] === null || value["dynamicValue"] === undefined) {
result["dynamicValue"] = {};
}
else {
if (!isRecord(value["dynamicValue"])) {
errorMap[`$.dynamicValue`] = {
key: "validator.object",
value: value["dynamicValue"],
foundType: typeof value["dynamicValue"],
};
}
else {
/** @type {Set<string>} */
const knownKeys5 = new Set([
"validator",
"completions",
]);
for (const key of Object.keys(value["dynamicValue"])) {
if (!knownKeys5.has(key) && value["dynamicValue"][key] !== null && value["dynamicValue"][key] !== undefined) {
const expectedKeys = [...knownKeys5];
const foundKeys = Object.keys(value["dynamicValue"]);
const unknownKeys = foundKeys.filter(it => !knownKeys5.has(it));
errorMap[`$.dynamicValue`] = {
key: "validator.keys",
unknownKeys,
expectedKeys,
foundKeys,
};
break;
}
}
result["dynamicValue"] = { "validator": undefined, "completions": undefined,};
if (value["dynamicValue"]["validator"] === null || value["dynamicValue"]["validator"] === undefined) {
result["dynamicValue"]["validator"] = undefined;
}
else {
if (typeof value["dynamicValue"]["validator"] === "function") {
result["dynamicValue"]["validator"] = value["dynamicValue"]["validator"];
}
else {
errorMap[`$.dynamicValue.validator`] = {
key: "validator.any",
message: "Custom validator error. See the input type for more information.",
};
}
}
if (value["dynamicValue"]["completions"] === null || value["dynamicValue"]["completions"] === undefined) {
result["dynamicValue"]["completions"] = undefined;
}
else {
if (typeof value["dynamicValue"]["completions"] === "function") {
result["dynamicValue"]["completions"] = value["dynamicValue"]["completions"];
}
else {
errorMap[`$.dynamicValue.completions`] = {
key: "validator.any",
message: "Custom validator error. See the input type for more information.",
};
}
}
}
}
if (value["watchSettings"] === null || value["watchSettings"] === undefined) {
result["watchSettings"] = {"extensions":["js","json"],"ignorePatterns":[".cache","coverage","node_modules"]};
}
else {
if (!isRecord(value["watchSettings"])) {
errorMap[`$.watchSettings`] = {
key: "validator.object",
value: value["watchSettings"],
foundType: typeof value["watchSettings"],
};
}
else {
/** @type {Set<string>} */
const knownKeys6 = new Set([
"extensions",
"ignorePatterns",
]);
for (const key of Object.keys(value["watchSettings"])) {
if (!knownKeys6.has(key) && value["watchSettings"][key] !== null && value["watchSettings"][key] !== undefined) {
const expectedKeys = [...knownKeys6];
const foundKeys = Object.keys(value["watchSettings"]);
const unknownKeys = foundKeys.filter(it => !knownKeys6.has(it));
errorMap[`$.watchSettings`] = {
key: "validator.keys",
unknownKeys,
expectedKeys,
foundKeys,
};
break;
}
}
result["watchSettings"] = { "extensions": undefined, "ignorePatterns": undefined,};
if (value["watchSettings"]["extensions"] === null || value["watchSettings"]["extensions"] === undefined) {
result["watchSettings"]["extensions"] = ["js", "json"];
}
else {
/** @type {ValidatorErrorMap} */
const intermediateErrorMap8 = {};
/** @type {any[]} */
let intermediateResult8 = [];
/** @type {any|any[]} */
const intermediateValue8 = value["watchSettings"]["extensions"];
if (!Array.isArray(intermediateValue8)) {
errorMap[`$.watchSettings.extensions`] = {
key: "validator.array",
value: intermediateValue8,
};
}
else {
result["watchSettings"]["extensions"] = [];
for (let i8 = 0; i8 < intermediateValue8.length; ++i8) {
if (intermediateValue8[i8] === null || intermediateValue8[i8] === undefined) {
intermediateErrorMap8[`$.${i8}`] = {
key: "validator.undefined",
};
}
else {
/** @type {string} */
let convertedString8 = intermediateValue8[i8];
if (typeof convertedString8 !== "string") {
intermediateErrorMap8[`$.${i8}`] = {
key: "validator.string",
};
}
else {
if (convertedString8.length < 1) {
intermediateErrorMap8[`$.${i8}`] = {
key: "validator.length",
minLength: 1
};
} else {
intermediateResult8[i8] = convertedString8;
}
}
}
}
}
if (Object.keys(intermediateErrorMap8).length) {
for (const errorKey of Object.keys(intermediateErrorMap8)) {
errorMap[`$.watchSettings.extensions${errorKey.substring(1)}`] = intermediateErrorMap8[errorKey];
}
}
else {
result["watchSettings"]["extensions"] = intermediateResult8;
}
}
if (value["watchSettings"]["ignorePatterns"] === null || value["watchSettings"]["ignorePatterns"] === undefined) {
result["watchSettings"]["ignorePatterns"] = [".cache", "coverage", "node_modules"];
}
else {
/** @type {ValidatorErrorMap} */
const intermediateErrorMap9 = {};
/** @type {any[]} */
let intermediateResult9 = [];
/** @type {any|any[]} */
const intermediateValue9 = value["watchSettings"]["ignorePatterns"];
if (!Array.isArray(intermediateValue9)) {
errorMap[`$.watchSettings.ignorePatterns`] = {
key: "validator.array",
value: intermediateValue9,
};
}
else {
result["watchSettings"]["ignorePatterns"] = [];
for (let i9 = 0; i9 < intermediateValue9.length; ++i9) {
if (intermediateValue9[i9] === null || intermediateValue9[i9] === undefined) {
intermediateErrorMap9[`$.${i9}`] = {
key: "validator.undefined",
};
}
else {
/** @type {string} */
let convertedString9 = intermediateValue9[i9];
if (typeof convertedString9 !== "string") {
intermediateErrorMap9[`$.${i9}`] = {
key: "validator.string",
};
}
else {
if (convertedString9.length < 1) {
intermediateErrorMap9[`$.${i9}`] = {
key: "validator.length",
minLength: 1
};
} else {
intermediateResult9[i9] = convertedString9;
}
}
}
}
}
if (Object.keys(intermediateErrorMap9).length) {
for (const errorKey of Object.keys(intermediateErrorMap9)) {
errorMap[`$.watchSettings.ignorePatterns${errorKey.substring(1)}`] = intermediateErrorMap9[errorKey];
}
}
else {
result["watchSettings"]["ignorePatterns"] = intermediateResult9;
}
}
}
}
if (value["subCommands"] === null || value["subCommands"] === undefined) {
result["subCommands"] = [];
}
else {
/** @type {ValidatorErrorMap} */
const intermediateErrorMap8 = {};
/** @type {any[]} */
let intermediateResult8 = [];
/** @type {any|any[]} */
const intermediateValue8 = value["subCommands"];
if (!Array.isArray(intermediateValue8)) {
errorMap[`$.subCommands`] = {
key: "validator.array",
value: intermediateValue8,
};
}
else {
result["subCommands"] = [];
for (let i8 = 0; i8 < intermediateValue8.length; ++i8) {
if (intermediateValue8[i8] === null || intermediateValue8[i8] === undefined) {
intermediateErrorMap8[`$.${i8}`] = {
key: "validator.undefined",
};
}
else {
const refResult8 = validateCliCommandDefinition(intermediateValue8[i8]);
if (refResult8.error) {
for (const errorKey of Object.keys(refResult8.error)) {
intermediateErrorMap8[`$.${i8}${errorKey.substring(1)}`] = refResult8.error[errorKey];
}
}
intermediateResult8[i8] = refResult8.value;
}
}
}
if (Object.keys(intermediateErrorMap8).length) {
for (const errorKey of Object.keys(intermediateErrorMap8)) {
errorMap[`$.subCommands${errorKey.substring(1)}`] = intermediateErrorMap8[errorKey];
}
}
else {
result["subCommands"] = intermediateResult8;
}
}
if (value["flags"] === null || value["flags"] === undefined) {
result["flags"] = [];
}
else {
/** @type {ValidatorErrorMap} */
const intermediateErrorMap9 = {};
/** @type {any[]} */
let intermediateResult9 = [];
/** @type {any|any[]} */
const intermediateValue9 = value["flags"];
if (!Array.isArray(intermediateValue9)) {
errorMap[`$.flags`] = {
key: "validator.array",
value: intermediateValue9,
};
}
else {
result["flags"] = [];
for (let i9 = 0; i9 < intermediateValue9.length; ++i9) {
if (intermediateValue9[i9] === null || intermediateValue9[i9] === undefined) {
intermediateErrorMap9[`$.${i9}`] = {
key: "validator.undefined",
};
}
else {
const refResult9 = validateCliFlagDefinition(intermediateValue9[i9]);
if (refResult9.error) {
for (const errorKey of Object.keys(refResult9.error)) {
intermediateErrorMap9[`$.${i9}${errorKey.substring(1)}`] = refResult9.error[errorKey];
}
}
intermediateResult9[i9] = refResult9.value;
}
}
}
if (Object.keys(intermediateErrorMap9).length) {
for (const errorKey of Object.keys(intermediateErrorMap9)) {
errorMap[`$.flags${errorKey.substring(1)}`] = intermediateErrorMap9[errorKey];
}
}
else {
result["flags"] = intermediateResult9;
}
}
if (value["executor"] === null || value["executor"] === undefined) {
result["executor"] = undefined;
}
else {
if (typeof value["executor"] === "function") {
result["executor"] = value["executor"];
}
else {
errorMap[`$.executor`] = {
key: "validator.any",
message: "Custom validator error. See the input type for more information.",
};
}
}
}
}
if (Object.keys(errorMap).length > 0) {
return { error: errorMap };
}
return { value: result }
}
/**
* @param {import("../common/types.js").CliFlagDefinitionInput|any} value
* @returns {Either<import("../common/types.js").CliFlagDefinition, ValidatorErrorMap>}
*/
export function validateCliFlagDefinition(value) {
/** @type {ValidatorErrorMap} */
const errorMap = {};
/** @type {any} */
let result = undefined;
if (value === null || value === undefined) {
errorMap[`$`] = {
key: "validator.undefined",
};
}
else {
if (!isRecord(value)) {
errorMap[`$`] = {
key: "validator.object",
value: value,
foundType: typeof value,
};
}
else {
/** @type {Set<string>} */
const knownKeys0 = new Set([
"name",
"rawName",
"description",
"modifiers",
"value",
]);
for (const key of Object.keys(value)) {
if (!knownKeys0.has(key) && value[key] !== null && value[key] !== undefined) {
const expectedKeys = [...knownKeys0];
const foundKeys = Object.keys(value);
const unknownKeys = foundKeys.filter(it => !knownKeys0.has(it));
errorMap[`$`] = {
key: "validator.keys",
unknownKeys,
expectedKeys,
foundKeys,
};
break;
}
}
result = { "name": undefined, "rawName": undefined, "description": undefined, "modifiers": undefined, "value": undefined,};
if (value["name"] === null || value["name"] === undefined) {
errorMap[`$.name`] = {
key: "validator.undefined",
};
}
else {
/** @type {string} */
let convertedString1 = value["name"];
if (typeof convertedString1 !== "string") {
errorMap[`$.name`] = {
key: "validator.string",
};
}
else {
if (convertedString1.length < 1) {
errorMap[`$.name`] = {
key: "validator.length",
minLength: 1
};
} else {
result["name"] = convertedString1;
}
}
}
if (value["rawName"] === null || value["rawName"] === undefined) {
errorMap[`$.rawName`] = {
key: "validator.undefined",
};
}
else {
/** @type {string} */
let convertedString2 = value["rawName"];
if (typeof convertedString2 !== "string") {
errorMap[`$.rawName`] = {
key: "validator.string",
};
}
else {
convertedString2 = convertedString2.toLowerCase();
if (convertedString2.length < 1) {
errorMap[`$.rawName`] = {
key: "validator.length",
minLength: 1
};
} else if (!/^--\w/g.test(convertedString2)) {
errorMap[`$.rawName`] = {
key: "validator.pattern",
};
} else {
result["rawName"] = convertedString2;
}
}
}
if (value["description"] === null || value["description"] === undefined) {
result["description"] = undefined;
}
else {
/** @type {string} */
let convertedString3 = value["description"];
if (typeof convertedString3 !== "string") {
errorMap[`$.description`] = {
key: "validator.string",
};
}
else {
if (convertedString3.length === 0) {
result["description"] = undefined;
}
else {
if (convertedString3.length < 1) {
errorMap[`$.description`] = {
key: "validator.length",
minLength: 1
};
} else if (!/^[^\n]+$/g.test(convertedString3)) {
errorMap[`$.description`] = {
key: "validator.pattern",
};
} else {
result["description"] = convertedString3;
}
}
}
}
if (value["modifiers"] === null || value["modifiers"] === undefined) {
result["modifiers"] = {"isRepeatable":false,"isRequired":false,"isInternal":false};
}
else {
if (!isRecord(value["modifiers"])) {
errorMap[`$.modifiers`] = {
key: "validator.object",
value: value["modifiers"],
foundType: typeof value["modifiers"],
};
}
else {
/** @type {Set<string>} */
const knownKeys4 = new Set([
"isRepeatable",
"isRequired",
"isInternal",
]);
for (const key of Object.keys(value["modifiers"])) {
if (!knownKeys4.has(key) && value["modifiers"][key] !== null && value["modifiers"][key] !== undefined) {
const expectedKeys = [...knownKeys4];
const foundKeys = Object.keys(value["modifiers"]);
const unknownKeys = foundKeys.filter(it => !knownKeys4.has(it));
errorMap[`$.modifiers`] = {
key: "validator.keys",
unknownKeys,
expectedKeys,
foundKeys,
};
break;
}
}
result["modifiers"] = { "isRepeatable": undefined, "isRequired": undefined, "isInternal": undefined,};
if (value["modifiers"]["isRepeatable"] === null || value["modifiers"]["isRepeatable"] === undefined) {
result["modifiers"]["isRepeatable"] = false;
}
else {
if (value["modifiers"]["isRepeatable"] === true || value["modifiers"]["isRepeatable"] === "true" || value["modifiers"]["isRepeatable"] === 1 || value["modifiers"]["isRepeatable"] === "1") {
result["modifiers"]["isRepeatable"] = true;
}
else if (value["modifiers"]["isRepeatable"] === false || value["modifiers"]["isRepeatable"] === "false" || value["modifiers"]["isRepeatable"] === 0 || value["modifiers"]["isRepeatable"] === "0") {
result["modifiers"]["isRepeatable"] = false;
}
else {
errorMap[`$.modifiers.isRepeatable`] = {
key: "validator.type",
expectedType: "boolean",
};
}
}
if (value["modifiers"]["isRequired"] === null || value["modifiers"]["isRequired"] === undefined) {
result["modifiers"]["isRequired"] = false;
}
else {
if (value["modifiers"]["isRequired"] === true || value["modifiers"]["isRequired"] === "true" || value["modifiers"]["isRequired"] === 1 || value["modifiers"]["isRequired"] === "1") {
result["modifiers"]["isRequired"] = true;
}
else if (value["modifiers"]["isRequired"] === false || value["modifiers"]["isRequired"] === "false" || value["modifiers"]["isRequired"] === 0 || value["modifiers"]["isRequired"] === "0") {
result["modifiers"]["isRequired"] = false;
}
else {
errorMap[`$.modifiers.isRequired`] = {
key: "validator.type",
expectedType: "boolean",
};
}
}
if (value["modifiers"]["isInternal"] === null || value["modifiers"]["isInternal"] === undefined) {
result["modifiers"]["isInternal"] = false;
}
else {
if (value["modifiers"]["isInternal"] === true || value["modifiers"]["isInternal"] === "true" || value["modifiers"]["isInternal"] === 1 || value["modifiers"]["isInternal"] === "1") {
result["modifiers"]["isInternal"] = true;
}
else if (value["modifiers"]["isInternal"] === false || value["modifiers"]["isInternal"] === "false" || value["modifiers"]["isInternal"] === 0 || value["modifiers"]["isInternal"] === "0") {
result["modifiers"]["isInternal"] = false;
}
else {
errorMap[`$.modifiers.isInternal`] = {
key: "validator.type",
expectedType: "boolean",
};
}
}
}
}
if (value["value"] === null || value["value"] === undefined) {
result["value"] = {"specification":"boolean"};
}
else {
if (!isRecord(value["value"])) {
errorMap[`$.value`] = {
key: "validator.object",
value: value["value"],
foundType: typeof value["value"],
};
}
else {
/** @type {Set<string>} */
const knownKeys5 = new Set([
"specification",
"validator",
"completions",
]);
for (const key of Object.keys(value["value"])) {
if (!knownKeys5.has(key) && value["value"][key] !== null && value["value"][key] !== undefined) {
const expectedKeys = [...knownKeys5];
const foundKeys = Object.keys(value["value"]);
const unknownKeys = foundKeys.filter(it => !knownKeys5.has(it));
errorMap[`$.value`] = {
key: "validator.keys",
unknownKeys,
expectedKeys,
foundKeys,
};
break;
}
}
result["value"] = { "specification": undefined, "validator": undefined, "completions": undefined,};
if (value["value"]["specification"] === null || value["value"]["specification"] === undefined) {
result["value"]["specification"] = "boolean";
}
else {
/** @type {string} */
let convertedString6 = value["value"]["specification"];
if (typeof convertedString6 !== "string") {
errorMap[`$.value.specification`] = {
key: "validator.string",
};
}
else {
if (convertedString6.length === 0) {
result["value"]["specification"] = "boolean";
}
else {
if (convertedString6.length < 1) {
errorMap[`$.value.specification`] = {
key: "validator.length",
minLength: 1
};
} else if (convertedString6 !== "boolean" && convertedString6 !== "number" && convertedString6 !== "string" && convertedString6 !== "booleanOrString") {
errorMap[`$.value.specification`] = {
key: "validator.oneOf",
allowedValues: ["boolean","number","string","booleanOrString"],
foundValue: convertedString6,
};
} else {
result["value"]["specification"] = convertedString6;
}
}
}
}
if (value["value"]["validator"] === null || value["value"]["validator"] === undefined) {
result["value"]["validator"] = undefined;
}
else {
if (typeof value["value"]["validator"] === "function") {
result["value"]["validator"] = value["value"]["validator"];
}
else {
errorMap[`$.value.validator`] = {
key: "validator.any",
message: "Custom validator error. See the input type for more information.",
};
}
}
if (value["value"]["completions"] === null || value["value"]["completions"] === undefined) {
result["value"]["completions"] = undefined;
}
else {
if (typeof value["value"]["completions"] === "function") {
result["value"]["completions"] = value["value"]["completions"];
}
else {
errorMap[`$.value.completions`] = {
key: "validator.any",
message: "Custom validator error. See the input type for more information.",
};
}
}
}
}
}
}
if (Object.keys(errorMap).length > 0) {
return { error: errorMap };
}
return { value: result }
}
/**
* @param {import("../common/types.js").CliCompletion|any} value
* @returns {Either<import("../common/types.js").CliCompletion, ValidatorErrorMap>}
*/
export function validateCliCompletion(value) {
/** @type {ValidatorErrorMap} */
const errorMap = {};
/** @type {any} */
let result = undefined;
if (value === null || value === undefined) {
errorMap[`$`] = {
key: "validator.undefined",
};
}
else {
let hasAnyOfMatch0 = false;
errorMap[`$`] = {
key: "validator.anyOf",
errors: [],
};
if (!hasAnyOfMatch0) {
/** @type {ValidatorErrorMap} */
const intermediateErrorMap2 = {};
/** @type {any} */
let intermediateResult2 = undefined;
/** @type {any} */
let intermediateValue2 = value;
if (intermediateValue2 === null || intermediateValue2 === undefined) {
intermediateErrorMap2[`$`] = {
key: "validator.undefined",
};
}
else {
if (!isRecord(intermediateValue2)) {
intermediateErrorMap2[`$`] = {
key: "validator.object",
value: intermediateValue2,
foundType: typeof intermediateValue2,
};
}
else {
/** @type {Set<string>} */
const knownKeys2 = new Set([
"type",
]);
for (const key of Object.keys(intermediateValue2)) {
if (!knownKeys2.has(key) && intermediateValue2[key] !== null && intermediateValue2[key] !== undefined) {
const expectedKeys = [...knownKeys2];
const foundKeys = Object.keys(intermediateValue2);
const unknownKeys = foundKeys.filter(it => !knownKeys2.has(it));
intermediateErrorMap2[`$`] = {
key: "validator.keys",
unknownKeys,
expectedKeys,
foundKeys,
};
break;
}
}
intermediateResult2 = { "type": undefined,};
if (intermediateValue2["type"] === null || intermediateValue2["type"] === undefined) {
intermediateErrorMap2[`$.type`] = {
key: "validator.undefined",
};
}
else {
/** @type {string} */
let convertedString3 = intermediateValue2["type"];
if (typeof convertedString3 !== "string") {
intermediateErrorMap2[`$.type`] = {
key: "validator.string",
};
}
else {
if (convertedString3.length < 1) {
intermediateErrorMap2[`$.type`] = {
key: "validator.length",
minLength: 1
};
} else if (convertedString3 !== "directory") {
intermediateErrorMap2[`$.type`] = {
key: "validator.oneOf",
allowedValues: ["directory"],
foundValue: convertedString3,
};
} else {
intermediateResult2["type"] = convertedString3;
}
}
}
}
}
if (Object.keys(intermediateErrorMap2).length > 0) {
errorMap[`$`].errors.push(intermediateErrorMap2);
}
else {
hasAnyOfMatch0 = true;
delete errorMap[`$`];
result = intermediateResult2;
}
}
if (!hasAnyOfMatch0) {
/** @type {ValidatorErrorMap} */
const intermediateErrorMap2 = {};
/** @type {any} */
let intermediateResult2 = undefined;
/** @type {any} */
let intermediateValue2 = value;
if (intermediateValue2 === null || intermediateValue2 === undefined) {
intermediateErrorMap2[`$`] = {
key: "validator.undefined",
};
}
else {
if (!isRecord(intermediateValue2)) {
intermediateErrorMap2[`$`] = {
key: "validator.object",
value: intermediateValue2,
foundType: typeof intermediateValue2,
};
}
else {
/** @type {Set<string>} */
const knownKeys2 = new Set([
"type",
]);
for (const key of Object.keys(intermediateValue2)) {
if (!knownKeys2.has(key) && intermediateValue2[key] !== null && intermediateValue2[key] !== undefined) {
const expectedKeys = [...knownKeys2];
const foundKeys = Object.keys(intermediateValue2);
const unknownKeys = foundKeys.filter(it => !knownKeys2.has(it));
intermediateErrorMap2[`$`] = {
key: "validator.keys",
unknownKeys,
expectedKeys,
foundKeys,
};
break;
}
}
intermediateResult2 = { "type": undefined,};
if (intermediateValue2["type"] === null || intermediateValue2["type"] === undefined) {
intermediateErrorMap2[`$.type`] = {
key: "validator.undefined",
};
}
else {
/** @type {string} */
let convertedString3 = intermediateValue2["type"];
if (typeof convertedString3 !== "string") {
intermediateErrorMap2[`$.type`] = {
key: "validator.string",
};
}
else {
if (convertedString3.length < 1) {
intermediateErrorMap2[`$.type`] = {
key: "validator.length",
minLength: 1
};
} else if (convertedString3 !== "file") {
intermediateErrorMap2[`$.type`] = {
key: "validator.oneOf",
allowedValues: ["file"],
foundValue: convertedString3,
};
} else {
intermediateResult2["type"] = convertedString3;
}
}
}
}
}
if (Object.keys(intermediateErrorMap2).length > 0) {
errorMap[`$`].errors.push(intermediateErrorMap2);
}
else {
hasAnyOfMatch0 = true;
delete errorMap[`$`];
result = intermediateResult2;
}
}
if (!hasAnyOfMatch0) {
/** @type {ValidatorErrorMap} */
const intermediateErrorMap2 = {};
/** @type {any} */
let intermediateResult2 = undefined;
/** @type {any} */
let intermediateValue2 = value;
if (intermediateValue2 === null || intermediateValue2 === undefined) {
intermediateErrorMap2[`$`] = {
key: "validator.undefined",
};
}
else {
if (!isRecord(intermediateValue2)) {
intermediateErrorMap2[`$`] = {
key: "validator.object",
value: intermediateValue2,
foundType: typeof intermediateValue2,
};
}
else {
/** @type {Set<string>} */
const knownKeys2 = new Set([
"type",
"name",
"description",
]);
for (const key of Object.keys(intermediateValue2)) {
if (!knownKeys2.has(key) && intermediateValue2[key] !== null && intermediateValue2[key] !== undefined) {
const expectedKeys = [...knownKeys2];
const foundKeys = Object.keys(intermediateValue2);
const unknownKeys = foundKeys.filter(it => !knownKeys2.has(it));
intermediateErrorMap2[`$`] = {
key: "validator.keys",
unknownKeys,
expectedKeys,
foundKeys,
};
break;
}
}
intermediateResult2 = { "type": undefined, "name": undefined, "description": undefined,};
if (intermediateValue2["type"] === null || intermediateValue2["type"] === undefined) {
intermediateErrorMap2[`$.type`] = {
key: "validator.undefined",
};
}
else {
/** @type {string} */
let convertedString3 = intermediateValue2["type"];
if (typeof convertedString3 !== "string") {
intermediateErrorMap2[`$.type`] = {
key: "validator.string",
};
}
else {
if (convertedString3.length < 1) {
intermediateErrorMap2[`$.type`] = {
key: "validator.length",
minLength: 1
};
} else if (convertedString3 !== "completion") {
intermediateErrorMap2[`$.type`] = {
key: "validator.oneOf",
allowedValues: ["completion"],
foundValue: convertedString3,
};
} else {
intermediateResult2["type"] = convertedString3;
}
}
}
if (intermediateValue2["name"] === null || intermediateValue2["name"] === undefined) {
intermediateErrorMap2[`$.name`] = {
key: "validator.undefined",
};
}
else {
/** @type {string} */
let convertedString4 = intermediateValue2["name"];
if (typeof convertedString4 !== "string") {
intermediateErrorMap2[`$.name`] = {
key: "validator.string",
};
}
else {
if (convertedString4.length < 1) {
intermediateErrorMap2[`$.name`] = {
key: "validator.length",
minLength: 1
};
} else {
intermediateResult2["name"] = convertedString4;
}
}
}
if (intermediateValue2["description"] === null || intermediateValue2["description"] === undefined) {
intermediateResult2["description"] = undefined;
}
else {
/** @type {string} */
let convertedString5 = intermediateValue2["description"];
if (typeof convertedString5 !== "string") {
intermediateErrorMap2[`$.description`] = {
key: "validator.string",
};
}
else {
if (convertedString5.length === 0) {
intermediateResult2["description"] = undefined;
}
else {
if (convertedString5.length < 1) {
intermediateErrorMap2[`$.description`] = {
key: "validator.length",
minLength: 1
};
} else {
intermediateResult2["description"] = convertedString5;
}
}
}
}
}
}
if (Object.keys(intermediateErrorMap2).length > 0) {
errorMap[`$`].errors.push(intermediateErrorMap2);
}
else {
hasAnyOfMatch0 = true;
delete errorMap[`$`];
result = intermediateResult2;
}
}
if (!hasAnyOfMatch0) {
/** @type {ValidatorErrorMap} */
const intermediateErrorMap2 = {};
/** @type {any} */
let intermediateResult2 = undefined;
/** @type {any} */
let intermediateValue2 = value;
if (intermediateValue2 === null || intermediateValue2 === undefined) {
intermediateErrorMap2[`$`] = {
key: "validator.undefined",
};
}
else {
if (!isRecord(intermediateValue2)) {
intermediateErrorMap2[`$`] = {
key: "validator.object",
value: intermediateValue2,
foundType: typeof intermediateValue2,
};
}
else {
/** @type {Set<string>} */
const knownKeys2 = new Set([
"type",
"specification",
"description",
]);
for (const key of Object.keys(intermediateValue2)) {
if (!knownKeys2.has(key) && intermediateValue2[key] !== null && intermediateValue2[key] !== undefined) {
const expectedKeys = [...knownKeys2];
const foundKeys = Object.keys(intermediateValue2);
const unknownKeys = foundKeys.filter(it => !knownKeys2.has(it));
intermediateErrorMap2[`$`] = {
key: "validator.keys",
unknownKeys,
expectedKeys,
foundKeys,
};
break;
}
}
intermediateResult2 = { "type": undefined, "specification": undefined, "description": undefined,};
if (intermediateValue2["type"] === null || intermediateValue2["type"] === undefined) {
intermediateErrorMap2[`$.type`] = {
key: "validator.undefined",
};
}
else {
/** @type {string} */
let convertedString3 = intermediateValue2["type"];
if (typeof convertedString3 !== "string") {
intermediateErrorMap2[`$.type`] = {
key: "validator.string",
};
}
else {
if (convertedString3.length < 1) {
intermediateErrorMap2[`$.type`] = {
key: "validator.length",
minLength: 1
};
} else if (convertedString3 !== "value") {
intermediateErrorMap2[`$.type`] = {
key: "validator.oneOf",
allowedValues: ["value"],
foundValue: convertedString3,
};
} else {
intermediateResult2["type"] = convertedString3;
}
}
}
if (intermediateValue2["specification"] === null || intermediateValue2["specification"] === undefined) {
intermediateErrorMap2[`$.specification`] = {
key: "validator.undefined",
};
}
else {
/** @type {string} */
let convertedString4 = intermediateValue2["specification"];
if (typeof convertedString4 !== "string") {
intermediateErrorMap2[`$.specification`] = {
key: "validator.string",
};
}
else {
if (convertedString4.length < 1) {
intermediateErrorMap2[`$.specification`] = {
key: "validator.length",
minLength: 1
};
} else if (convertedString4 !== "boolean" && convertedString4 !== "number" && convertedString4 !== "string" && convertedString4 !== "booleanOrString") {
intermediateErrorMap2[`$.specification`] = {
key: "validator.oneOf",
allowedValues: ["boolean","number","string","booleanOrString"],
foundValue: convertedString4,
};
} else {
intermediateResult2["specification"] = convertedString4;
}
}
}
if (intermediateValue2["description"] === null || intermediateValue2["description"] === undefined) {
intermediateResult2["description"] = undefined;
}
else {
/** @type {string} */
let convertedString5 = intermediateValue2["description"];
if (typeof convertedString5 !== "string") {
intermediateErrorMap2[`$.description`] = {
key: "validator.string",
};
}
else {
if (convertedString5.length === 0) {
intermediateResult2["description"] = undefined;
}
else {
if (convertedString5.length < 1) {
intermediateErrorMap2[`$.description`] = {
key: "validator.length",
minLength: 1
};
} else {
intermediateResult2["description"] = convertedString5;
}
}
}
}
}
}
if (Object.keys(intermediateErrorMap2).length > 0) {
errorMap[`$`].errors.push(intermediateErrorMap2);
}
else {
hasAnyOfMatch0 = true;
delete errorMap[`$`];
result = intermediateResult2;
}
}
}
if (Object.keys(errorMap).length > 0) {
return { error: errorMap };
}
return { value: result }
}