@malloydata/malloy 0.0.365 → 0.0.366

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.
@@ -1,6 +1,7 @@
1
1
  import type { URLReader } from '../../runtime_types';
2
2
  import type { Connection, LookupConnection } from '../../connection/types';
3
3
  import type { ConnectionConfigEntry } from '../../connection/registry';
4
+ import type { LogMessage } from '../../lang/parse-log';
4
5
  import type { BuildID, BuildManifest, BuildManifestEntry, VirtualMap } from '../../model/malloy_types';
5
6
  /**
6
7
  * The parsed contents of a malloy-config.json file.
@@ -93,6 +94,7 @@ export declare class MalloyConfig {
93
94
  private readonly _urlReader?;
94
95
  private readonly _configURL?;
95
96
  private _data;
97
+ private _log;
96
98
  private _connectionMap;
97
99
  private readonly _manifest;
98
100
  private _managedLookup;
@@ -157,4 +159,11 @@ export declare class MalloyConfig {
157
159
  * The VirtualMap parsed from config, if present.
158
160
  */
159
161
  get virtualMap(): VirtualMap | undefined;
162
+ /**
163
+ * Errors and warnings from parsing and validating the config.
164
+ * Includes JSON parse errors (severity 'error') and schema validation
165
+ * warnings (severity 'warn') such as unknown keys, unknown connection
166
+ * types, wrong value types, and missing environment variables.
167
+ */
168
+ get log(): LogMessage[];
160
169
  }
@@ -123,23 +123,12 @@ class Manifest {
123
123
  }
124
124
  // New format: {entries: {...}, strict?: boolean}
125
125
  // Old format: {buildId: {tableName}, ...} (flat record, no "entries" key)
126
- let entries;
127
- if (parsed['entries'] && typeof parsed['entries'] === 'object') {
128
- entries = parsed['entries'];
129
- }
130
- else {
131
- // Treat as old flat-record format: every key with a tableName is an entry
132
- entries = {};
133
- for (const [key, val] of Object.entries(parsed)) {
134
- if (key !== 'strict' &&
135
- val &&
136
- typeof val === 'object' &&
137
- 'tableName' in val) {
138
- entries[key] = val;
139
- }
126
+ const rawEntries = isRecord(parsed['entries']) ? parsed['entries'] : parsed;
127
+ for (const [key, val] of Object.entries(rawEntries)) {
128
+ if (key !== 'strict' && isBuildManifestEntry(val)) {
129
+ this._manifest.entries[key] = val;
140
130
  }
141
131
  }
142
- Object.assign(this._manifest.entries, entries);
143
132
  }
144
133
  }
145
134
  exports.Manifest = Manifest;
@@ -163,8 +152,11 @@ exports.Manifest = Manifest;
163
152
  */
164
153
  class MalloyConfig {
165
154
  constructor(urlReaderOrText, configURL) {
155
+ this._log = [];
166
156
  if (typeof urlReaderOrText === 'string') {
167
- this._data = parseConfigText(urlReaderOrText);
157
+ const { config, log } = parseConfigText(urlReaderOrText);
158
+ this._data = config;
159
+ this._log = log;
168
160
  this._connectionMap = this._data.connections
169
161
  ? { ...this._data.connections }
170
162
  : undefined;
@@ -198,7 +190,9 @@ class MalloyConfig {
198
190
  return;
199
191
  const result = await this._urlReader.readURL(new URL(this._configURL));
200
192
  const contents = typeof result === 'string' ? result : result.contents;
201
- this._data = parseConfigText(contents);
193
+ const parsed = parseConfigText(contents);
194
+ this._data = parsed.config;
195
+ this._log = parsed.log;
202
196
  this._connectionMap = this._data.connections
203
197
  ? { ...this._data.connections }
204
198
  : undefined;
@@ -285,22 +279,61 @@ class MalloyConfig {
285
279
  var _a;
286
280
  return (_a = this._data) === null || _a === void 0 ? void 0 : _a.virtualMap;
287
281
  }
282
+ /**
283
+ * Errors and warnings from parsing and validating the config.
284
+ * Includes JSON parse errors (severity 'error') and schema validation
285
+ * warnings (severity 'warn') such as unknown keys, unknown connection
286
+ * types, wrong value types, and missing environment variables.
287
+ */
288
+ get log() {
289
+ return this._log;
290
+ }
288
291
  }
289
292
  exports.MalloyConfig = MalloyConfig;
290
293
  /**
291
294
  * Parse a config JSON string into a MalloyProjectConfig.
292
295
  * Invalid connection entries (missing `is`) are silently dropped.
296
+ * Returns the processed config and validation log.
293
297
  */
294
298
  function parseConfigText(jsonText) {
295
- var _a;
296
- const parsed = JSON.parse(jsonText);
297
- const connections = Object.fromEntries(Object.entries((_a = parsed.connections) !== null && _a !== void 0 ? _a : {}).filter(([, v]) => (0, registry_1.isConnectionConfigEntry)(v)));
299
+ let parsed;
300
+ try {
301
+ parsed = JSON.parse(jsonText);
302
+ }
303
+ catch (e) {
304
+ return {
305
+ config: {},
306
+ log: [
307
+ {
308
+ message: `Invalid JSON: ${e instanceof Error ? e.message : String(e)}`,
309
+ severity: 'error',
310
+ code: 'config-validation',
311
+ },
312
+ ],
313
+ };
314
+ }
315
+ if (!isRecord(parsed)) {
316
+ return {
317
+ config: {},
318
+ log: [
319
+ {
320
+ message: 'config is not a JSON object',
321
+ severity: 'error',
322
+ code: 'config-validation',
323
+ },
324
+ ],
325
+ };
326
+ }
327
+ const rawConnections = parsed['connections'];
328
+ const connectionEntries = Object.entries(isRecord(rawConnections) ? rawConnections : {}).filter((entry) => (0, registry_1.isConnectionConfigEntry)(entry[1]));
329
+ const connections = Object.fromEntries(connectionEntries);
298
330
  const result = { ...parsed, connections };
299
- if (parsed.virtualMap &&
300
- typeof parsed.virtualMap === 'object' &&
301
- !Array.isArray(parsed.virtualMap)) {
331
+ const virtualMap = parsed['virtualMap'];
332
+ if (virtualMap &&
333
+ typeof virtualMap === 'object' &&
334
+ !Array.isArray(virtualMap)) {
302
335
  const outer = new Map();
303
- for (const [connName, inner] of Object.entries(parsed.virtualMap)) {
336
+ for (const [connName, inner] of Object.entries(virtualMap)) {
304
337
  if (inner && typeof inner === 'object' && !Array.isArray(inner)) {
305
338
  const innerMap = new Map();
306
339
  for (const [virtualName, tablePath] of Object.entries(inner)) {
@@ -313,6 +346,156 @@ function parseConfigText(jsonText) {
313
346
  }
314
347
  result.virtualMap = outer;
315
348
  }
316
- return result;
349
+ return { config: result, log: validateConfig(parsed) };
350
+ }
351
+ const KNOWN_TOP_LEVEL_KEYS = new Set([
352
+ 'connections',
353
+ 'manifestPath',
354
+ 'virtualMap',
355
+ ]);
356
+ function isRecord(value) {
357
+ return typeof value === 'object' && value !== null && !Array.isArray(value);
358
+ }
359
+ function isBuildManifestEntry(value) {
360
+ return isRecord(value) && typeof value['tableName'] === 'string';
361
+ }
362
+ function makeWarning(path, message) {
363
+ return {
364
+ message: `${path}: ${message}`,
365
+ severity: 'warn',
366
+ code: 'config-validation',
367
+ };
368
+ }
369
+ /**
370
+ * Validate a parsed config object against the connection type registry.
371
+ * Returns LogMessage[] — does not throw.
372
+ */
373
+ function validateConfig(data) {
374
+ const log = [];
375
+ for (const key of Object.keys(data)) {
376
+ if (!KNOWN_TOP_LEVEL_KEYS.has(key)) {
377
+ const suggestion = closestMatch(key, [...KNOWN_TOP_LEVEL_KEYS]);
378
+ const hint = suggestion ? `. Did you mean "${suggestion}"?` : '';
379
+ log.push(makeWarning(key, `unknown config key "${key}"${hint}`));
380
+ }
381
+ }
382
+ if (data['manifestPath'] !== undefined &&
383
+ typeof data['manifestPath'] !== 'string') {
384
+ log.push(makeWarning('manifestPath', '"manifestPath" should be a string'));
385
+ }
386
+ const connections = data['connections'];
387
+ if (connections === undefined)
388
+ return log;
389
+ if (!isRecord(connections)) {
390
+ log.push(makeWarning('connections', '"connections" should be an object'));
391
+ return log;
392
+ }
393
+ const registeredTypes = new Set((0, registry_1.getRegisteredConnectionTypes)());
394
+ for (const [name, rawEntry] of Object.entries(connections)) {
395
+ const prefix = `connections.${name}`;
396
+ if (!isRecord(rawEntry)) {
397
+ log.push(makeWarning(prefix, 'should be an object'));
398
+ continue;
399
+ }
400
+ if (!rawEntry['is']) {
401
+ log.push(makeWarning(prefix, 'missing required "is" field (connection type)'));
402
+ continue;
403
+ }
404
+ if (typeof rawEntry['is'] !== 'string') {
405
+ log.push(makeWarning(`${prefix}.is`, '"is" should be a string'));
406
+ continue;
407
+ }
408
+ const typeName = rawEntry['is'];
409
+ if (!registeredTypes.has(typeName)) {
410
+ const suggestion = closestMatch(typeName, [...registeredTypes]);
411
+ const hint = suggestion ? ` Did you mean "${suggestion}"?` : '';
412
+ log.push(makeWarning(`${prefix}.is`, `unknown connection type "${typeName}".${hint} Available types: ${[...registeredTypes].join(', ')}`));
413
+ continue;
414
+ }
415
+ const props = (0, registry_1.getConnectionProperties)(typeName);
416
+ if (!props)
417
+ continue;
418
+ const knownProps = new Map(props.map(p => [p.name, p]));
419
+ for (const [key, value] of Object.entries(rawEntry)) {
420
+ if (key === 'is')
421
+ continue;
422
+ const propDef = knownProps.get(key);
423
+ if (!propDef) {
424
+ const suggestion = closestMatch(key, [...knownProps.keys()]);
425
+ const hint = suggestion ? `. Did you mean "${suggestion}"?` : '';
426
+ log.push(makeWarning(`${prefix}.${key}`, `unknown property "${key}" for connection type "${typeName}"${hint}`));
427
+ continue;
428
+ }
429
+ // For env var references on non-json props, check the variable exists.
430
+ if (propDef.type !== 'json' && (0, registry_1.isValueRef)(value)) {
431
+ const env = value.env;
432
+ if (process.env[env] === undefined) {
433
+ log.push(makeWarning(`${prefix}.${key}`, `environment variable "${env}" is not set`));
434
+ }
435
+ continue;
436
+ }
437
+ const typeError = checkValueType(value, propDef.type);
438
+ if (typeError) {
439
+ log.push(makeWarning(`${prefix}.${key}`, `${typeError} (expected ${propDef.type})`));
440
+ }
441
+ }
442
+ }
443
+ return log;
444
+ }
445
+ function levenshtein(a, b) {
446
+ const m = a.length;
447
+ const n = b.length;
448
+ const dp = Array.from({ length: m + 1 }, () => Array(n + 1).fill(0));
449
+ for (let i = 0; i <= m; i++)
450
+ dp[i][0] = i;
451
+ for (let j = 0; j <= n; j++)
452
+ dp[0][j] = j;
453
+ for (let i = 1; i <= m; i++) {
454
+ for (let j = 1; j <= n; j++) {
455
+ const cost = a[i - 1] === b[j - 1] ? 0 : 1;
456
+ dp[i][j] = Math.min(dp[i - 1][j] + 1, dp[i][j - 1] + 1, dp[i - 1][j - 1] + cost);
457
+ }
458
+ }
459
+ return dp[m][n];
460
+ }
461
+ function closestMatch(input, candidates) {
462
+ if (candidates.length === 0)
463
+ return undefined;
464
+ let best = candidates[0];
465
+ let bestDist = Infinity;
466
+ for (const c of candidates) {
467
+ const dist = levenshtein(input.toLowerCase(), c.toLowerCase());
468
+ if (dist < bestDist) {
469
+ bestDist = dist;
470
+ best = c;
471
+ }
472
+ }
473
+ const maxDist = Math.max(1, Math.floor(Math.max(input.length, best.length) / 3));
474
+ return bestDist <= maxDist ? best : undefined;
475
+ }
476
+ function checkValueType(value, expectedType) {
477
+ if (value === undefined || value === null)
478
+ return undefined;
479
+ switch (expectedType) {
480
+ case 'number':
481
+ if (typeof value !== 'number')
482
+ return `should be a number, got ${typeof value}`;
483
+ break;
484
+ case 'boolean':
485
+ if (typeof value !== 'boolean')
486
+ return `should be a boolean, got ${typeof value}`;
487
+ break;
488
+ case 'string':
489
+ case 'password':
490
+ case 'secret':
491
+ case 'file':
492
+ case 'text':
493
+ if (typeof value !== 'string')
494
+ return `should be a string, got ${typeof value}`;
495
+ break;
496
+ case 'json':
497
+ break;
498
+ }
499
+ return undefined;
317
500
  }
318
501
  //# sourceMappingURL=config.js.map
@@ -48,7 +48,7 @@ export type ConfigValue = string | number | boolean | ValueRef | JsonConfigValue
48
48
  /**
49
49
  * Type guard for ValueRef.
50
50
  */
51
- export declare function isValueRef(value: ConfigValue): value is ValueRef;
51
+ export declare function isValueRef(value: unknown): value is ValueRef;
52
52
  /**
53
53
  * Resolve a ValueRef to a string by looking up the environment variable.
54
54
  * Returns undefined if the env var is not set.
@@ -22,7 +22,8 @@ function isValueRef(value) {
22
22
  value !== null &&
23
23
  !Array.isArray(value) &&
24
24
  Object.keys(value).length === 1 &&
25
- 'env' in value);
25
+ 'env' in value &&
26
+ typeof value.env === 'string');
26
27
  }
27
28
  /**
28
29
  * Resolve a ValueRef to a string by looking up the environment variable.
package/dist/version.d.ts CHANGED
@@ -1 +1 @@
1
- export declare const MALLOY_VERSION = "0.0.365";
1
+ export declare const MALLOY_VERSION = "0.0.366";
package/dist/version.js CHANGED
@@ -2,5 +2,5 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.MALLOY_VERSION = void 0;
4
4
  // generated with 'generate-version-file' script; do not edit manually
5
- exports.MALLOY_VERSION = '0.0.365';
5
+ exports.MALLOY_VERSION = '0.0.366';
6
6
  //# sourceMappingURL=version.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@malloydata/malloy",
3
- "version": "0.0.365",
3
+ "version": "0.0.366",
4
4
  "license": "MIT",
5
5
  "exports": {
6
6
  ".": "./dist/index.js",
@@ -47,9 +47,9 @@
47
47
  "generate-version-file": "VERSION=$(npm pkg get version --workspaces=false | tr -d \\\")\necho \"// generated with 'generate-version-file' script; do not edit manually\\nexport const MALLOY_VERSION = '$VERSION';\" > src/version.ts"
48
48
  },
49
49
  "dependencies": {
50
- "@malloydata/malloy-filter": "0.0.365",
51
- "@malloydata/malloy-interfaces": "0.0.365",
52
- "@malloydata/malloy-tag": "0.0.365",
50
+ "@malloydata/malloy-filter": "0.0.366",
51
+ "@malloydata/malloy-interfaces": "0.0.366",
52
+ "@malloydata/malloy-tag": "0.0.366",
53
53
  "@noble/hashes": "^1.8.0",
54
54
  "antlr4ts": "^0.5.0-alpha.4",
55
55
  "assert": "^2.0.0",