Reorganize repo structure and standards references
This commit is contained in:
1 parent
3cb93a81be
commit
be2054274d
5885 files changed
+2588
-195
No files matched your search
@@ -0,0 +1,118 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user