Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 18 additions & 20 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions packages/compass-preferences-model/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,12 @@
"@mongodb-js/compass-logging": "^1.1.6",
"@mongodb-js/compass-utils": "^0.3.1",
"ampersand-collection-filterable": "^0.3.0",
"ampersand-model": "^8.0.1",
"ampersand-rest-collection": "^6.0.0",
"ampersand-state": "5.0.3",
"bson": "^5.2.0",
"joi": "^17.9.2",
"js-yaml": "^4.1.0",
"lodash": "^4.17.21",
"storage-mixin": "^5.1.5",
"yargs-parser": "^21.1.1"
},
"devDependencies": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ forceConnectionOptions:
global: {},
cli: {},
preferenceParseErrors: [
`Type for option "enableMaps" mismatches: expected boolean, received string (while validating preferences from: Global config file: ${file})`,
`enableMaps: "value" must be a boolean (while validating preferences from: Global config file: ${file})`,
],
});
});
Expand Down
36 changes: 11 additions & 25 deletions packages/compass-preferences-model/src/global-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import yaml from 'js-yaml';
import type { Options as YargsOptions } from 'yargs-parser';
import yargsParser from 'yargs-parser';
import { kebabCase } from 'lodash';
import type { AmpersandType, AllPreferences } from './preferences';
import type { AllPreferences } from './preferences';
import { allPreferencesProps } from './preferences';
import type { Types as JoiTypes } from 'joi';

import { createLoggerAndTelemetry } from '@mongodb-js/compass-logging';
const { log, mongoLogId } = createLoggerAndTelemetry('COMPASS-PREFERENCES');
Expand Down Expand Up @@ -91,11 +92,11 @@ async function loadGlobalPreferences(
const cliProps = Object.entries(allPreferencesProps).filter(
([, definition]) => definition.cli
);
function getCliPropNamesByType(type: AmpersandType<any>): string[] {
function getCliPropNamesByType(type: JoiTypes): string[] {
return [
...new Set(
cliProps
.filter(([, definition]) => definition.type === type)
.filter(([, definition]) => definition.validator.type === type)
.flatMap(([key]) => [key, kebabCase(key)])
),
];
Expand Down Expand Up @@ -170,30 +171,15 @@ function validatePreferences(
// as an option value, e.g. an object into an array of key-value pairs
const process = allPreferencesProps[key].customPostProcess;
const value = process ? process(rawValue, error) : rawValue;
// `typeof` + `isArray` is good enough for everything we need right now, but we can of course expand this check over time
if (
(Array.isArray(value) ? 'array' : typeof value) !==
allPreferencesProps[key].type
) {
error(
`Type for option "${key}" mismatches: expected ${
allPreferencesProps[key].type
}, received ${typeof value}`
);
continue;
}
if (
allPreferencesProps[key].values &&
!(allPreferencesProps[key].values as unknown[])?.includes(value)
) {
error(
`Value for option "${key}" is not allowed: expected one of [${String(
allPreferencesProps[key].values?.join(', ')
)}], received ${String(value)}`
);

const validationResults =
allPreferencesProps[key].validator.validate(value);
if (validationResults.error) {
error(`${key}: ${validationResults.error.message}`);
continue;
}
obj[key] = value as any;

obj[key] = validationResults.value as any;
}
return [obj, errors];
}
Expand Down
Loading