@serfab/config-check 1.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,64 @@
1
+ /**
2
+ * The entry point: check a whole config tree and either return it typed or throw one `Error`
3
+ * listing every problem — an operator fixing a hand-edited file should not have to restart once
4
+ * per typo. Each line names the offending key and its source: the config file, or the
5
+ * environment variable whose write covers that key.
6
+ */
7
+
8
+ import { type Checker, ValidationContext, isPathPrefix, isPlainObject } from './checkers.js';
9
+
10
+ export interface ValidateOptions {
11
+ /** Named in every problem the environment did not cause. */
12
+ configPath: string;
13
+ /**
14
+ * Dotted path the environment wrote → the variable that wrote it (`applyEnvOverrides` builds
15
+ * it). A problem at a key is attributed to the variable whose written path is the longest
16
+ * prefix of that key, and to `configPath` when none is.
17
+ */
18
+ provenance?: ReadonlyMap<string, string>;
19
+ /** Variables that could supply a missing key, for "is required" messages. */
20
+ suppliersOf?: (keyPath: string) => readonly string[];
21
+ /**
22
+ * Key paths whose whole subtree holds hand-written secrets (cadre-provider's `push` and
23
+ * `billing`): a rejected value at or under one is described by kind only, never echoed.
24
+ */
25
+ concealUnder?: readonly string[];
26
+ }
27
+
28
+ /**
29
+ * Check `tree` with `root`. An empty file (`undefined`/`null`) is an empty mapping; any other
30
+ * non-mapping top level is one problem. Throws one `Error` whose message lists every problem,
31
+ * one per line, each prefixed `Environment variable X:` or `Config <path>:`.
32
+ */
33
+ export function validateTree<T>(tree: unknown, root: Checker<T>, options: ValidateOptions): T {
34
+ const ctx = new ValidationContext(options.suppliersOf, options.concealUnder);
35
+ const checked = checkRoot(tree, root, ctx);
36
+ if (checked === undefined || ctx.problems.length > 0) {
37
+ const provenance = options.provenance ?? new Map<string, string>();
38
+ const lines = ctx.problems.map((p) => `${sourceOf(p.keyPath, provenance, options.configPath)}: ${p.text}`);
39
+ throw new Error(lines.join('\n'));
40
+ }
41
+ return checked;
42
+ }
43
+
44
+ function checkRoot<T>(tree: unknown, root: Checker<T>, ctx: ValidationContext): T | undefined {
45
+ // An empty file parses to undefined/null: an empty mapping, for the environment to fill.
46
+ const value = tree ?? {};
47
+ if (!isPlainObject(value)) {
48
+ return ctx.fail('', `the top level must be a mapping of keys, got ${ctx.describe('', value)}`);
49
+ }
50
+ return root(value, '', ctx);
51
+ }
52
+
53
+ /** The variable that wrote the longest prefix of `keyPath`, else the file. */
54
+ function sourceOf(keyPath: string, provenance: ReadonlyMap<string, string>, configPath: string): string {
55
+ let longest = -1;
56
+ let envVar: string | undefined;
57
+ for (const [written, variable] of provenance) {
58
+ if (isPathPrefix(written, keyPath) && written.length > longest) {
59
+ longest = written.length;
60
+ envVar = variable;
61
+ }
62
+ }
63
+ return envVar !== undefined ? `Environment variable ${envVar}` : `Config ${configPath}`;
64
+ }