@ontrails/config 1.0.0-beta.12

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.
Files changed (159) hide show
  1. package/.turbo/turbo-build.log +1 -0
  2. package/.turbo/turbo-lint.log +3 -0
  3. package/.turbo/turbo-typecheck.log +1 -0
  4. package/CHANGELOG.md +19 -0
  5. package/dist/app-config.d.ts +65 -0
  6. package/dist/app-config.d.ts.map +1 -0
  7. package/dist/app-config.js +172 -0
  8. package/dist/app-config.js.map +1 -0
  9. package/dist/collect.d.ts +11 -0
  10. package/dist/collect.d.ts.map +1 -0
  11. package/dist/collect.js +81 -0
  12. package/dist/collect.js.map +1 -0
  13. package/dist/compose.d.ts +26 -0
  14. package/dist/compose.d.ts.map +1 -0
  15. package/dist/compose.js +19 -0
  16. package/dist/compose.js.map +1 -0
  17. package/dist/config-layer.d.ts +11 -0
  18. package/dist/config-layer.d.ts.map +1 -0
  19. package/dist/config-layer.js +6 -0
  20. package/dist/config-layer.js.map +1 -0
  21. package/dist/config-service.d.ts +3 -0
  22. package/dist/config-service.d.ts.map +1 -0
  23. package/dist/config-service.js +26 -0
  24. package/dist/config-service.js.map +1 -0
  25. package/dist/define-config.d.ts +61 -0
  26. package/dist/define-config.d.ts.map +1 -0
  27. package/dist/define-config.js +90 -0
  28. package/dist/define-config.js.map +1 -0
  29. package/dist/describe.d.ts +25 -0
  30. package/dist/describe.d.ts.map +1 -0
  31. package/dist/describe.js +147 -0
  32. package/dist/describe.js.map +1 -0
  33. package/dist/doctor.d.ts +27 -0
  34. package/dist/doctor.d.ts.map +1 -0
  35. package/dist/doctor.js +167 -0
  36. package/dist/doctor.js.map +1 -0
  37. package/dist/explain.d.ts +30 -0
  38. package/dist/explain.d.ts.map +1 -0
  39. package/dist/explain.js +114 -0
  40. package/dist/explain.js.map +1 -0
  41. package/dist/extensions.d.ts +38 -0
  42. package/dist/extensions.d.ts.map +1 -0
  43. package/dist/extensions.js +35 -0
  44. package/dist/extensions.js.map +1 -0
  45. package/dist/generate/env.d.ts +15 -0
  46. package/dist/generate/env.d.ts.map +1 -0
  47. package/dist/generate/env.js +65 -0
  48. package/dist/generate/env.js.map +1 -0
  49. package/dist/generate/example.d.ts +16 -0
  50. package/dist/generate/example.d.ts.map +1 -0
  51. package/dist/generate/example.js +136 -0
  52. package/dist/generate/example.js.map +1 -0
  53. package/dist/generate/helpers.d.ts +35 -0
  54. package/dist/generate/helpers.d.ts.map +1 -0
  55. package/dist/generate/helpers.js +116 -0
  56. package/dist/generate/helpers.js.map +1 -0
  57. package/dist/generate/index.d.ts +4 -0
  58. package/dist/generate/index.d.ts.map +1 -0
  59. package/dist/generate/index.js +4 -0
  60. package/dist/generate/index.js.map +1 -0
  61. package/dist/generate/json-schema.d.ts +18 -0
  62. package/dist/generate/json-schema.d.ts.map +1 -0
  63. package/dist/generate/json-schema.js +97 -0
  64. package/dist/generate/json-schema.js.map +1 -0
  65. package/dist/index.d.ts +21 -0
  66. package/dist/index.d.ts.map +1 -0
  67. package/dist/index.js +21 -0
  68. package/dist/index.js.map +1 -0
  69. package/dist/merge.d.ts +16 -0
  70. package/dist/merge.d.ts.map +1 -0
  71. package/dist/merge.js +34 -0
  72. package/dist/merge.js.map +1 -0
  73. package/dist/ref.d.ts +24 -0
  74. package/dist/ref.d.ts.map +1 -0
  75. package/dist/ref.js +25 -0
  76. package/dist/ref.js.map +1 -0
  77. package/dist/registry.d.ts +24 -0
  78. package/dist/registry.d.ts.map +1 -0
  79. package/dist/registry.js +12 -0
  80. package/dist/registry.js.map +1 -0
  81. package/dist/resolve.d.ts +21 -0
  82. package/dist/resolve.d.ts.map +1 -0
  83. package/dist/resolve.js +174 -0
  84. package/dist/resolve.js.map +1 -0
  85. package/dist/secret-heuristics.d.ts +10 -0
  86. package/dist/secret-heuristics.d.ts.map +1 -0
  87. package/dist/secret-heuristics.js +11 -0
  88. package/dist/secret-heuristics.js.map +1 -0
  89. package/dist/trails/config-check.d.ts +11 -0
  90. package/dist/trails/config-check.d.ts.map +1 -0
  91. package/dist/trails/config-check.js +53 -0
  92. package/dist/trails/config-check.js.map +1 -0
  93. package/dist/trails/config-describe.d.ts +12 -0
  94. package/dist/trails/config-describe.d.ts.map +1 -0
  95. package/dist/trails/config-describe.js +41 -0
  96. package/dist/trails/config-describe.js.map +1 -0
  97. package/dist/trails/config-explain.d.ts +8 -0
  98. package/dist/trails/config-explain.d.ts.map +1 -0
  99. package/dist/trails/config-explain.js +74 -0
  100. package/dist/trails/config-explain.js.map +1 -0
  101. package/dist/trails/config-init.d.ts +9 -0
  102. package/dist/trails/config-init.d.ts.map +1 -0
  103. package/dist/trails/config-init.js +78 -0
  104. package/dist/trails/config-init.js.map +1 -0
  105. package/dist/workspace.d.ts +9 -0
  106. package/dist/workspace.d.ts.map +1 -0
  107. package/dist/workspace.js +44 -0
  108. package/dist/workspace.js.map +1 -0
  109. package/dist/zod-utils.d.ts +14 -0
  110. package/dist/zod-utils.d.ts.map +1 -0
  111. package/dist/zod-utils.js +41 -0
  112. package/dist/zod-utils.js.map +1 -0
  113. package/package.json +20 -0
  114. package/src/__tests__/app-config.test.ts +329 -0
  115. package/src/__tests__/compose.test.ts +59 -0
  116. package/src/__tests__/config-check.test.ts +171 -0
  117. package/src/__tests__/config-describe.test.ts +154 -0
  118. package/src/__tests__/config-explain.test.ts +167 -0
  119. package/src/__tests__/config-init.test.ts +210 -0
  120. package/src/__tests__/config-layer.test.ts +53 -0
  121. package/src/__tests__/config-service.test.ts +87 -0
  122. package/src/__tests__/define-config.test.ts +263 -0
  123. package/src/__tests__/describe.test.ts +158 -0
  124. package/src/__tests__/doctor.test.ts +172 -0
  125. package/src/__tests__/explain.test.ts +139 -0
  126. package/src/__tests__/extensions.test.ts +134 -0
  127. package/src/__tests__/generate.test.ts +269 -0
  128. package/src/__tests__/ref.test.ts +35 -0
  129. package/src/__tests__/resolve.test.ts +246 -0
  130. package/src/__tests__/workspace.test.ts +64 -0
  131. package/src/app-config.ts +307 -0
  132. package/src/collect.ts +118 -0
  133. package/src/compose.ts +46 -0
  134. package/src/config-layer.ts +15 -0
  135. package/src/config-service.ts +32 -0
  136. package/src/define-config.ts +134 -0
  137. package/src/describe.ts +252 -0
  138. package/src/doctor.ts +219 -0
  139. package/src/explain.ts +176 -0
  140. package/src/extensions.ts +51 -0
  141. package/src/generate/env.ts +104 -0
  142. package/src/generate/example.ts +222 -0
  143. package/src/generate/helpers.ts +158 -0
  144. package/src/generate/index.ts +3 -0
  145. package/src/generate/json-schema.ts +137 -0
  146. package/src/index.ts +44 -0
  147. package/src/merge.ts +43 -0
  148. package/src/ref.ts +38 -0
  149. package/src/registry.ts +33 -0
  150. package/src/resolve.ts +279 -0
  151. package/src/secret-heuristics.ts +13 -0
  152. package/src/trails/config-check.ts +60 -0
  153. package/src/trails/config-describe.ts +44 -0
  154. package/src/trails/config-explain.ts +93 -0
  155. package/src/trails/config-init.ts +96 -0
  156. package/src/workspace.ts +51 -0
  157. package/src/zod-utils.ts +53 -0
  158. package/tsconfig.json +9 -0
  159. package/tsconfig.tsbuildinfo +1 -0
@@ -0,0 +1,252 @@
1
+ /**
2
+ * Config introspection — describe all fields in a schema without values.
3
+ *
4
+ * Returns a structured catalog suitable for CLI rendering or agent inspection.
5
+ */
6
+
7
+ import { globalRegistry } from 'zod';
8
+ import type { z } from 'zod';
9
+
10
+ import { collectConfigMeta } from './collect.js';
11
+ import { isZodObject, unwrapToBase, zodDef } from './zod-utils.js';
12
+
13
+ // ---------------------------------------------------------------------------
14
+ // Types
15
+ // ---------------------------------------------------------------------------
16
+
17
+ /** Description of a single config field. */
18
+ export interface FieldDescription {
19
+ readonly path: string;
20
+ readonly type: string;
21
+ readonly description?: string;
22
+ readonly default?: unknown;
23
+ readonly required: boolean;
24
+ readonly env?: string;
25
+ readonly secret?: boolean;
26
+ readonly deprecated?: string;
27
+ readonly constraints?: Record<string, unknown>;
28
+ }
29
+
30
+ /** Accumulated state while unwrapping Zod wrappers. */
31
+ interface UnwrapState {
32
+ hasDefault: boolean;
33
+ defaultValue: unknown;
34
+ isOptional: boolean;
35
+ }
36
+
37
+ /** Unwrap result carrying both base schema and accumulated metadata. */
38
+ interface UnwrapResult {
39
+ readonly base: z.ZodType;
40
+ readonly hasDefault: boolean;
41
+ readonly defaultValue: unknown;
42
+ readonly isOptional: boolean;
43
+ }
44
+
45
+ // ---------------------------------------------------------------------------
46
+ // Helpers (defined before consumers — satisfies no-use-before-define)
47
+ // ---------------------------------------------------------------------------
48
+
49
+ /** Read the description from the Zod global registry. */
50
+ const getDescription = (schema: z.ZodType): string | undefined => {
51
+ const meta = globalRegistry.get(schema);
52
+ return meta?.description as string | undefined;
53
+ };
54
+
55
+ /** Handle a single unwrap step; returns updated inner type and state, or null to stop. */
56
+ const unwrapStep = (
57
+ def: Record<string, unknown>,
58
+ state: UnwrapState
59
+ ): { inner: z.ZodType; state: UnwrapState } | null => {
60
+ const typeName = def['type'] as string;
61
+
62
+ if (typeName === 'default') {
63
+ return {
64
+ inner: def['innerType'] as z.ZodType,
65
+ state: { ...state, defaultValue: def['defaultValue'], hasDefault: true },
66
+ };
67
+ }
68
+
69
+ if (typeName === 'optional') {
70
+ return {
71
+ inner: def['innerType'] as z.ZodType,
72
+ state: { ...state, isOptional: true },
73
+ };
74
+ }
75
+
76
+ if (typeName === 'nullable') {
77
+ return { inner: def['innerType'] as z.ZodType, state };
78
+ }
79
+
80
+ return null;
81
+ };
82
+
83
+ /** Unwrap through default/optional/nullable wrappers to find the base type. */
84
+ const unwrapSchema = (schema: z.ZodType): UnwrapResult => {
85
+ let current = schema;
86
+ let state: UnwrapState = {
87
+ defaultValue: undefined,
88
+ hasDefault: false,
89
+ isOptional: false,
90
+ };
91
+
92
+ for (let depth = 0; depth < 10; depth += 1) {
93
+ const result = unwrapStep(zodDef(current), state);
94
+ if (!result) {
95
+ break;
96
+ }
97
+ current = result.inner;
98
+ ({ state } = result);
99
+ }
100
+
101
+ return { base: current, ...state };
102
+ };
103
+
104
+ /** Resolve the user-facing type name from a base Zod schema. */
105
+ const resolveTypeName = (schema: z.ZodType): string => {
106
+ const typeName = zodDef(schema)['type'] as string;
107
+ const typeMap: Record<string, string> = {
108
+ boolean: 'boolean',
109
+ enum: 'enum',
110
+ number: 'number',
111
+ string: 'string',
112
+ };
113
+ return typeMap[typeName] ?? typeName;
114
+ };
115
+
116
+ /** Extract enum values from an enum def. */
117
+ const extractEnumConstraints = (
118
+ def: Record<string, unknown>
119
+ ): Record<string, unknown> | undefined => {
120
+ const entries = def['entries'] as Record<string, string> | undefined;
121
+ if (!entries) {
122
+ return undefined;
123
+ }
124
+ return { values: Object.values(entries) };
125
+ };
126
+
127
+ /** Extract min/max from a number schema's properties. */
128
+ const extractNumberConstraints = (
129
+ schema: z.ZodType
130
+ ): Record<string, unknown> | undefined => {
131
+ const result: Record<string, unknown> = {};
132
+ const numSchema = schema as unknown as {
133
+ minValue?: number;
134
+ maxValue?: number;
135
+ };
136
+
137
+ if (numSchema.minValue !== undefined && numSchema.minValue !== null) {
138
+ result['min'] = numSchema.minValue;
139
+ }
140
+ if (numSchema.maxValue !== undefined && numSchema.maxValue !== null) {
141
+ result['max'] = numSchema.maxValue;
142
+ }
143
+
144
+ return Object.keys(result).length > 0 ? result : undefined;
145
+ };
146
+
147
+ /** Extract constraints from a base schema (min, max, enum values). */
148
+ const extractConstraints = (
149
+ schema: z.ZodType
150
+ ): Record<string, unknown> | undefined => {
151
+ const def = zodDef(schema);
152
+ const typeName = def['type'] as string;
153
+
154
+ if (typeName === 'enum') {
155
+ return extractEnumConstraints(def);
156
+ }
157
+ if (typeName === 'number') {
158
+ return extractNumberConstraints(schema);
159
+ }
160
+ return undefined;
161
+ };
162
+
163
+ // ---------------------------------------------------------------------------
164
+ // Schema walking
165
+ // ---------------------------------------------------------------------------
166
+
167
+ /** Entry for iterative schema walk. */
168
+ interface WalkEntry {
169
+ readonly schema: z.ZodType;
170
+ readonly prefix: string;
171
+ }
172
+
173
+ /** Build a single FieldDescription from a leaf schema and its metadata. */
174
+ const buildFieldDescription = (
175
+ path: string,
176
+ fieldSchema: z.ZodType,
177
+ configMeta: Map<
178
+ string,
179
+ { env?: string; secret?: boolean; deprecated?: string }
180
+ >
181
+ ): FieldDescription => {
182
+ const { base, defaultValue, hasDefault, isOptional } =
183
+ unwrapSchema(fieldSchema);
184
+ const meta = configMeta.get(path);
185
+ const description = getDescription(base) ?? getDescription(fieldSchema);
186
+ const constraints = extractConstraints(base);
187
+
188
+ return {
189
+ ...(constraints ? { constraints } : {}),
190
+ ...(hasDefault ? { default: defaultValue } : {}),
191
+ ...(meta?.deprecated ? { deprecated: meta.deprecated } : {}),
192
+ ...(description ? { description } : {}),
193
+ ...(meta?.env ? { env: meta.env } : {}),
194
+ path,
195
+ required: !hasDefault && !isOptional,
196
+ ...(meta?.secret ? { secret: meta.secret } : {}),
197
+ type: resolveTypeName(base),
198
+ };
199
+ };
200
+
201
+ /** Walk one level of an object shape, collecting leaves and queuing nested objects. */
202
+ const walkShapeLevel = (
203
+ shape: Record<string, z.ZodType>,
204
+ prefix: string,
205
+ configMeta: Map<
206
+ string,
207
+ { env?: string; secret?: boolean; deprecated?: string }
208
+ >,
209
+ results: FieldDescription[],
210
+ queue: WalkEntry[]
211
+ ): void => {
212
+ for (const [key, fieldSchema] of Object.entries(shape)) {
213
+ const path = prefix ? `${prefix}.${key}` : key;
214
+ if (isZodObject(fieldSchema)) {
215
+ queue.push({ prefix: path, schema: unwrapToBase(fieldSchema) });
216
+ } else {
217
+ results.push(buildFieldDescription(path, fieldSchema, configMeta));
218
+ }
219
+ }
220
+ };
221
+
222
+ // ---------------------------------------------------------------------------
223
+ // Public API
224
+ // ---------------------------------------------------------------------------
225
+
226
+ /**
227
+ * Describe all fields in a schema without needing a config file.
228
+ *
229
+ * Returns a structured catalog suitable for CLI rendering or agent inspection.
230
+ */
231
+ export const describeConfig = (
232
+ schema: z.ZodObject<Record<string, z.ZodType>>
233
+ ): readonly FieldDescription[] => {
234
+ const configMeta = collectConfigMeta(schema);
235
+ const queue: WalkEntry[] = [];
236
+ const results: FieldDescription[] = [];
237
+
238
+ walkShapeLevel(
239
+ schema.shape as Record<string, z.ZodType>,
240
+ '',
241
+ configMeta,
242
+ results,
243
+ queue
244
+ );
245
+
246
+ for (let entry = queue.pop(); entry; entry = queue.pop()) {
247
+ const nested = zodDef(entry.schema)['shape'] as Record<string, z.ZodType>;
248
+ walkShapeLevel(nested, entry.prefix, configMeta, results, queue);
249
+ }
250
+
251
+ return results;
252
+ };
package/src/doctor.ts ADDED
@@ -0,0 +1,219 @@
1
+ /**
2
+ * Config doctor — structured diagnostics for a config object against a schema.
3
+ *
4
+ * Reports which fields are valid, missing, using defaults, deprecated, or invalid.
5
+ */
6
+
7
+ import type { z } from 'zod';
8
+
9
+ import { collectConfigMeta } from './collect.js';
10
+ import { getAtPath, isZodObject, unwrapToBase, zodDef } from './zod-utils.js';
11
+
12
+ // ---------------------------------------------------------------------------
13
+ // Types
14
+ // ---------------------------------------------------------------------------
15
+
16
+ /** Diagnostic status for a single config field. */
17
+ export interface ConfigDiagnostic {
18
+ readonly path: string;
19
+ readonly status: 'valid' | 'missing' | 'invalid' | 'deprecated' | 'default';
20
+ readonly message: string;
21
+ readonly value?: unknown;
22
+ }
23
+
24
+ /** Aggregated result from checking config against a schema. */
25
+ export interface CheckResult {
26
+ readonly diagnostics: readonly ConfigDiagnostic[];
27
+ readonly valid: boolean;
28
+ }
29
+
30
+ // ---------------------------------------------------------------------------
31
+ // Helpers (defined before consumers)
32
+ // ---------------------------------------------------------------------------
33
+
34
+ /** Check if a schema wraps a ZodDefault. */
35
+ const isDefaultWrapper = (schema: z.ZodType): boolean =>
36
+ zodDef(schema)['type'] === 'default';
37
+
38
+ /** Check if a schema wraps a ZodOptional. */
39
+ const isOptionalWrapper = (schema: z.ZodType): boolean =>
40
+ zodDef(schema)['type'] === 'optional';
41
+
42
+ /** Get the default value from a ZodDefault wrapper. */
43
+ const getDefaultValue = (schema: z.ZodType): unknown =>
44
+ zodDef(schema)['defaultValue'];
45
+
46
+ /** Set a value at a dot-separated path, creating intermediate objects. */
47
+ const setAtPath = (
48
+ obj: Record<string, unknown>,
49
+ path: string,
50
+ value: unknown
51
+ ): void => {
52
+ const parts = path.split('.');
53
+ let current: Record<string, unknown> = obj;
54
+ for (let i = 0; i < parts.length - 1; i += 1) {
55
+ const part = parts[i] as string;
56
+ const next = current[part];
57
+ const nested =
58
+ typeof next === 'object' && next !== null
59
+ ? (next as Record<string, unknown>)
60
+ : {};
61
+ current[part] = nested;
62
+ current = nested;
63
+ }
64
+ const lastPart = parts.at(-1) as string;
65
+ current[lastPart] = value;
66
+ };
67
+
68
+ /** Build values object with env overrides applied. */
69
+ const applyEnvToValues = (
70
+ values: Record<string, unknown>,
71
+ schema: z.ZodObject<Record<string, z.ZodType>>,
72
+ envVars: Record<string, string | undefined>
73
+ ): Record<string, unknown> => {
74
+ const meta = collectConfigMeta(schema);
75
+ const result = structuredClone(values) as Record<string, unknown>;
76
+ for (const [path, fieldMeta] of meta) {
77
+ if (fieldMeta.env && envVars[fieldMeta.env] !== undefined) {
78
+ setAtPath(result, path, envVars[fieldMeta.env]);
79
+ }
80
+ }
81
+ return result;
82
+ };
83
+
84
+ // ---------------------------------------------------------------------------
85
+ // Schema walking
86
+ // ---------------------------------------------------------------------------
87
+
88
+ /** Entry for the iterative schema walk queue. */
89
+ interface WalkEntry {
90
+ readonly schema: z.ZodType;
91
+ readonly path: string;
92
+ }
93
+
94
+ /** Validate a single field value against its schema. */
95
+ const validateFieldValue = (
96
+ path: string,
97
+ fieldSchema: z.ZodType,
98
+ value: unknown
99
+ ): ConfigDiagnostic => {
100
+ const result = fieldSchema.safeParse(value);
101
+ if (result.success) {
102
+ return { message: 'OK', path, status: 'valid', value };
103
+ }
104
+ const issue = result.error?.issues?.[0];
105
+ const msg = issue ? issue.message : 'Invalid value';
106
+ return { message: msg, path, status: 'invalid', value };
107
+ };
108
+
109
+ /** Classify a single field and produce a diagnostic. */
110
+ const classifyField = (
111
+ path: string,
112
+ fieldSchema: z.ZodType,
113
+ values: Record<string, unknown>,
114
+ deprecatedMeta: Map<string, string>
115
+ ): ConfigDiagnostic => {
116
+ const value = getAtPath(values, path);
117
+ const deprecationMsg = deprecatedMeta.get(path);
118
+
119
+ if (deprecationMsg && value !== undefined) {
120
+ return { message: deprecationMsg, path, status: 'deprecated', value };
121
+ }
122
+
123
+ if (value === undefined && isDefaultWrapper(fieldSchema)) {
124
+ return {
125
+ message: 'Using default value',
126
+ path,
127
+ status: 'default',
128
+ value: getDefaultValue(fieldSchema),
129
+ };
130
+ }
131
+
132
+ if (value === undefined && !isOptionalWrapper(fieldSchema)) {
133
+ return {
134
+ message: `Required field "${path}" is missing`,
135
+ path,
136
+ status: 'missing',
137
+ };
138
+ }
139
+
140
+ return validateFieldValue(path, fieldSchema, value);
141
+ };
142
+
143
+ /** Collect deprecated metadata paths from config meta. */
144
+ const collectDeprecatedPaths = (
145
+ schema: z.ZodObject<Record<string, z.ZodType>>
146
+ ): Map<string, string> => {
147
+ const meta = collectConfigMeta(schema);
148
+ const result = new Map<string, string>();
149
+ for (const [path, fieldMeta] of meta) {
150
+ if (fieldMeta.deprecated) {
151
+ result.set(path, fieldMeta.deprecated);
152
+ }
153
+ }
154
+ return result;
155
+ };
156
+
157
+ /** Walk an object shape and enqueue leaf fields or nested objects. */
158
+ const walkShape = (
159
+ schema: z.ZodType,
160
+ prefix: string,
161
+ queue: WalkEntry[],
162
+ leaves: WalkEntry[]
163
+ ): void => {
164
+ const shape = zodDef(schema)['shape'] as Record<string, z.ZodType>;
165
+ for (const [key, fieldSchema] of Object.entries(shape)) {
166
+ const path = prefix ? `${prefix}.${key}` : key;
167
+ if (isZodObject(fieldSchema)) {
168
+ queue.push({ path, schema: unwrapToBase(fieldSchema) });
169
+ } else {
170
+ leaves.push({ path, schema: fieldSchema });
171
+ }
172
+ }
173
+ };
174
+
175
+ /** Collect all leaf fields from a schema, walking nested objects iteratively. */
176
+ const collectLeaves = (schema: z.ZodType): WalkEntry[] => {
177
+ const queue: WalkEntry[] = [];
178
+ const leaves: WalkEntry[] = [];
179
+ walkShape(schema, '', queue, leaves);
180
+
181
+ for (let entry = queue.pop(); entry; entry = queue.pop()) {
182
+ walkShape(entry.schema, entry.path, queue, leaves);
183
+ }
184
+
185
+ return leaves;
186
+ };
187
+
188
+ // ---------------------------------------------------------------------------
189
+ // Public API
190
+ // ---------------------------------------------------------------------------
191
+
192
+ /**
193
+ * Check a config object against a schema and return structured diagnostics.
194
+ *
195
+ * Reports which fields are valid, missing, using defaults, deprecated, or invalid.
196
+ */
197
+ export const checkConfig = <T extends z.ZodType>(
198
+ schema: T,
199
+ values: Record<string, unknown>,
200
+ options?: { readonly env?: Record<string, string | undefined> }
201
+ ): CheckResult => {
202
+ const objSchema = schema as unknown as z.ZodObject<Record<string, z.ZodType>>;
203
+ const effectiveValues = options?.env
204
+ ? applyEnvToValues(values, objSchema, options.env)
205
+ : values;
206
+
207
+ const deprecatedMeta = collectDeprecatedPaths(objSchema);
208
+ const leaves = collectLeaves(objSchema);
209
+
210
+ const diagnostics = leaves.map((leaf) =>
211
+ classifyField(leaf.path, leaf.schema, effectiveValues, deprecatedMeta)
212
+ );
213
+
214
+ const valid = diagnostics.every(
215
+ (d) => d.status !== 'missing' && d.status !== 'invalid'
216
+ );
217
+
218
+ return { diagnostics, valid };
219
+ };
package/src/explain.ts ADDED
@@ -0,0 +1,176 @@
1
+ /**
2
+ * Config provenance — show which source won for each config field.
3
+ *
4
+ * Used for debugging: answers "where did this value come from?"
5
+ */
6
+
7
+ import type { z } from 'zod';
8
+
9
+ import { collectConfigMeta } from './collect.js';
10
+ import { isLikelySecret } from './secret-heuristics.js';
11
+ import { getAtPath, isZodObject, unwrapToBase, zodDef } from './zod-utils.js';
12
+
13
+ // ---------------------------------------------------------------------------
14
+ // Types
15
+ // ---------------------------------------------------------------------------
16
+
17
+ /** Provenance entry describing the source of a resolved config value. */
18
+ export interface ProvenanceEntry {
19
+ readonly path: string;
20
+ readonly value: unknown;
21
+ readonly source: 'default' | 'base' | 'loadout' | 'local' | 'env';
22
+ readonly redacted: boolean;
23
+ }
24
+
25
+ /** Options for explaining config provenance. */
26
+ export interface ExplainConfigOptions<T extends z.ZodType> {
27
+ readonly schema: T;
28
+ readonly base?: Record<string, unknown>;
29
+ readonly loadout?: Record<string, unknown>;
30
+ readonly local?: Record<string, unknown>;
31
+ readonly env?: Record<string, string | undefined>;
32
+ readonly resolved: Record<string, unknown>;
33
+ }
34
+
35
+ // ---------------------------------------------------------------------------
36
+ // Helpers (defined before consumers)
37
+ // ---------------------------------------------------------------------------
38
+
39
+ /** Build a map of path → env var name from config metadata. */
40
+ const buildEnvMap = (
41
+ schema: z.ZodObject<Record<string, z.ZodType>>
42
+ ): Map<string, string> => {
43
+ const meta = collectConfigMeta(schema);
44
+ const result = new Map<string, string>();
45
+ for (const [path, fieldMeta] of meta) {
46
+ if (fieldMeta.env) {
47
+ result.set(path, fieldMeta.env);
48
+ }
49
+ }
50
+ return result;
51
+ };
52
+
53
+ /** Build a set of paths marked as secret from config metadata. */
54
+ const buildSecretSet = (
55
+ schema: z.ZodObject<Record<string, z.ZodType>>
56
+ ): Set<string> => {
57
+ const meta = collectConfigMeta(schema);
58
+ const result = new Set<string>();
59
+ for (const [path, fieldMeta] of meta) {
60
+ if (fieldMeta.secret) {
61
+ result.add(path);
62
+ }
63
+ }
64
+ return result;
65
+ };
66
+
67
+ /** Source layers in reverse precedence order for winner detection. */
68
+ type SourceLayer = readonly [
69
+ name: ProvenanceEntry['source'],
70
+ values: Record<string, unknown> | undefined,
71
+ ];
72
+
73
+ /** Determine which source provided the winning value for a given path. */
74
+ const determineSource = (
75
+ path: string,
76
+ resolved: Record<string, unknown>,
77
+ layers: readonly SourceLayer[],
78
+ envMap: Map<string, string>,
79
+ envVars: Record<string, string | undefined> | undefined
80
+ ): ProvenanceEntry['source'] => {
81
+ if (envVars && envMap.has(path)) {
82
+ const envVar = envMap.get(path);
83
+ if (envVar && envVars[envVar] !== undefined) {
84
+ return 'env';
85
+ }
86
+ }
87
+
88
+ const resolvedValue = getAtPath(resolved, path);
89
+ for (const [name, values] of layers) {
90
+ if (values && getAtPath(values, path) === resolvedValue) {
91
+ return name;
92
+ }
93
+ }
94
+
95
+ return 'default';
96
+ };
97
+
98
+ // ---------------------------------------------------------------------------
99
+ // Schema walking
100
+ // ---------------------------------------------------------------------------
101
+
102
+ /** Entry for iterative schema walk. */
103
+ interface WalkEntry {
104
+ readonly schema: z.ZodType;
105
+ readonly prefix: string;
106
+ }
107
+
108
+ /** Collect all leaf field paths from an object schema. */
109
+ const collectLeafPaths = (schema: z.ZodType, prefix: string): string[] => {
110
+ const paths: string[] = [];
111
+ const queue: WalkEntry[] = [{ prefix, schema }];
112
+
113
+ for (let entry = queue.pop(); entry; entry = queue.pop()) {
114
+ const shape = zodDef(entry.schema)['shape'] as Record<string, z.ZodType>;
115
+ for (const [key, fieldSchema] of Object.entries(shape)) {
116
+ const path = entry.prefix ? `${entry.prefix}.${key}` : key;
117
+ if (isZodObject(fieldSchema)) {
118
+ queue.push({ prefix: path, schema: unwrapToBase(fieldSchema) });
119
+ } else {
120
+ paths.push(path);
121
+ }
122
+ }
123
+ }
124
+
125
+ return paths;
126
+ };
127
+
128
+ // ---------------------------------------------------------------------------
129
+ // Public API
130
+ // ---------------------------------------------------------------------------
131
+
132
+ /**
133
+ * Show which source won for each config field.
134
+ *
135
+ * Used for debugging — answers "where did this value come from?"
136
+ *
137
+ */
138
+ export const explainConfig = <T extends z.ZodType>(
139
+ options: ExplainConfigOptions<T>
140
+ ): readonly ProvenanceEntry[] => {
141
+ const objSchema = options.schema as unknown as z.ZodObject<
142
+ Record<string, z.ZodType>
143
+ >;
144
+ const envMap = buildEnvMap(objSchema);
145
+ const secretSet = buildSecretSet(objSchema);
146
+
147
+ const layers: readonly SourceLayer[] = [
148
+ ['local', options.local],
149
+ ['loadout', options.loadout],
150
+ ['base', options.base],
151
+ ];
152
+
153
+ const paths = collectLeafPaths(objSchema, '');
154
+
155
+ return paths.map((path) => {
156
+ const source = determineSource(
157
+ path,
158
+ options.resolved,
159
+ layers,
160
+ envMap,
161
+ options.env
162
+ );
163
+ const envVarName = envMap.get(path);
164
+ const isSecret =
165
+ secretSet.has(path) ||
166
+ (envVarName !== undefined && isLikelySecret(envVarName));
167
+ const rawValue = getAtPath(options.resolved, path);
168
+
169
+ return {
170
+ path,
171
+ redacted: isSecret,
172
+ source,
173
+ value: isSecret ? '[REDACTED]' : rawValue,
174
+ };
175
+ });
176
+ };
@@ -0,0 +1,51 @@
1
+ import type { z } from 'zod';
2
+
3
+ /** Metadata shape stored on Zod schemas via `.meta()`. */
4
+ export interface ConfigFieldMeta {
5
+ readonly env?: string;
6
+ readonly secret?: boolean;
7
+ readonly deprecated?: string;
8
+ }
9
+
10
+ /**
11
+ * Bind a schema field to an environment variable.
12
+ *
13
+ * Must be called BEFORE `.default()`, `.optional()`, or other transforms
14
+ * so that the metadata lives on the inner type where `collectConfigMeta`
15
+ * can find it by unwrapping wrappers.
16
+ */
17
+ export const env = <T extends z.ZodType>(schema: T, varName: string): T =>
18
+ schema.meta({ ...schema.meta(), env: varName }) as T;
19
+
20
+ /**
21
+ * Mark a schema field as sensitive. Redacted in survey, explain, and logs.
22
+ *
23
+ * Must be called BEFORE `.default()`, `.optional()`, or other transforms.
24
+ */
25
+ export const secret = <T extends z.ZodType>(schema: T): T =>
26
+ schema.meta({ ...schema.meta(), secret: true }) as T;
27
+
28
+ /**
29
+ * Mark a schema field as deprecated with migration guidance.
30
+ *
31
+ * Stores **two** meta keys: `deprecated: true` and `deprecationMessage: string`.
32
+ * This indirection exists because Zod 4's `GlobalMeta` types `deprecated` as
33
+ * `boolean | undefined` — there is no way to attach a migration message to the
34
+ * standard key. We set `deprecated: true` so Zod-native tooling (schema
35
+ * serializers, OpenAPI generators) recognises the field as deprecated, and store
36
+ * the human-readable message under `deprecationMessage` for our own
37
+ * `collectConfigMeta` / survey / explain surfaces.
38
+ *
39
+ * Must be called BEFORE `.default()`, `.optional()`, or other transforms
40
+ * so that the metadata lives on the inner type where `collectConfigMeta`
41
+ * can find it by unwrapping wrappers.
42
+ */
43
+ export const deprecated = <T extends z.ZodType>(
44
+ schema: T,
45
+ message: string
46
+ ): T =>
47
+ schema.meta({
48
+ ...schema.meta(),
49
+ deprecated: true,
50
+ deprecationMessage: message,
51
+ }) as T;