119 lines
3.6 KiB
TypeScript
119 lines
3.6 KiB
TypeScript
import { envVariableDefinitions } from "./catalog.js";
|
|
import type { EnvClassification, SecretValidationDiagnostic, SecretValidationResult, SecretUrlValidationOptions } from "./types.js";
|
|
|
|
function getSecretClassification(name: string): EnvClassification | undefined {
|
|
const definition = envVariableDefinitions.find((candidate) => {
|
|
if (candidate.name === name) {
|
|
return true;
|
|
}
|
|
return "dynamicPrefix" in candidate && candidate.dynamicPrefix ? name.startsWith(candidate.dynamicPrefix) : false;
|
|
});
|
|
|
|
return definition?.classification;
|
|
}
|
|
|
|
function createDiagnostic(code: SecretValidationDiagnostic["code"], name: string, message: string): SecretValidationDiagnostic {
|
|
const classification = getSecretClassification(name);
|
|
|
|
return {
|
|
code,
|
|
name,
|
|
...(classification ? { classification } : {}),
|
|
message,
|
|
};
|
|
}
|
|
|
|
function ok<T>(value: T, configured: boolean): SecretValidationResult<T> {
|
|
return {
|
|
ok: true,
|
|
configured,
|
|
value,
|
|
diagnostics: [],
|
|
};
|
|
}
|
|
|
|
function failed<T>(diagnostic: SecretValidationDiagnostic, configured: boolean): SecretValidationResult<T> {
|
|
return {
|
|
ok: false,
|
|
configured,
|
|
diagnostics: [diagnostic],
|
|
};
|
|
}
|
|
|
|
type NormalizedOptionalStringValue =
|
|
| { configured: false; value?: undefined }
|
|
| { configured: true; value: string };
|
|
|
|
function normalizeOptionalStringValue(value: string | undefined): NormalizedOptionalStringValue {
|
|
const normalized = value?.trim();
|
|
|
|
if (!normalized) {
|
|
return { configured: false };
|
|
}
|
|
|
|
return { configured: true, value: normalized };
|
|
}
|
|
|
|
export function validateRequiredSecretString(name: string, value: string | undefined): SecretValidationResult<string> {
|
|
const normalized = normalizeOptionalStringValue(value);
|
|
|
|
if (!normalized.configured) {
|
|
return failed(createDiagnostic("missing-required", name, `${name} is required.`), false);
|
|
}
|
|
|
|
return ok(normalized.value, true);
|
|
}
|
|
|
|
export function validateOptionalSecretString(name: string, value: string | undefined): SecretValidationResult<string | undefined> {
|
|
const normalized = normalizeOptionalStringValue(value);
|
|
|
|
return ok(normalized.value, normalized.configured);
|
|
}
|
|
|
|
export function validateOptionalSecretUrl(name: string, value: string | undefined, options: SecretUrlValidationOptions = {}): SecretValidationResult<string | undefined> {
|
|
const normalized = normalizeOptionalStringValue(value);
|
|
|
|
if (!normalized.configured) {
|
|
return ok(undefined, false);
|
|
}
|
|
|
|
let parsed: URL;
|
|
try {
|
|
parsed = new URL(normalized.value);
|
|
} catch {
|
|
return failed(createDiagnostic("invalid-url", name, `${name} must be a valid URL.`), true);
|
|
}
|
|
|
|
if (options.allowedProtocols?.length) {
|
|
const allowedProtocols = options.allowedProtocols.map((protocol) => (protocol.endsWith(":") ? protocol : `${protocol}:`));
|
|
if (!allowedProtocols.includes(parsed.protocol)) {
|
|
return failed(createDiagnostic("invalid-url-protocol", name, `${name} must use an allowed URL protocol.`), true);
|
|
}
|
|
}
|
|
|
|
return ok(normalized.value, true);
|
|
}
|
|
|
|
export function validateBooleanFlag(name: string, value: string | undefined, defaultValue = false): SecretValidationResult<boolean> {
|
|
const normalized = normalizeOptionalStringValue(value);
|
|
|
|
if (!normalized.configured) {
|
|
return ok(defaultValue, false);
|
|
}
|
|
|
|
switch (normalized.value.toLowerCase()) {
|
|
case "true":
|
|
case "1":
|
|
case "yes":
|
|
case "on":
|
|
return ok(true, true);
|
|
case "false":
|
|
case "0":
|
|
case "no":
|
|
case "off":
|
|
return ok(false, true);
|
|
default:
|
|
return failed(createDiagnostic("invalid-boolean", name, `${name} must be a boolean flag.`), true);
|
|
}
|
|
}
|