@@ -5,8 +5,9 @@ import yaml from 'js-yaml';
5
5
import type { Options as YargsOptions } from 'yargs-parser' ;
6
6
import yargsParser from 'yargs-parser' ;
7
7
import { kebabCase } from 'lodash' ;
8
- import type { AmpersandType , AllPreferences } from './preferences' ;
8
+ import type { AllPreferences } from './preferences' ;
9
9
import { allPreferencesProps } from './preferences' ;
10
+ import type { Types as JoiTypes } from 'joi' ;
10
11
11
12
import { createLoggerAndTelemetry } from '@mongodb-js/compass-logging' ;
12
13
const { log, mongoLogId } = createLoggerAndTelemetry ( 'COMPASS-PREFERENCES' ) ;
@@ -91,11 +92,11 @@ async function loadGlobalPreferences(
91
92
const cliProps = Object . entries ( allPreferencesProps ) . filter (
92
93
( [ , definition ] ) => definition . cli
93
94
) ;
94
- function getCliPropNamesByType ( type : AmpersandType < any > ) : string [ ] {
95
+ function getCliPropNamesByType ( type : JoiTypes ) : string [ ] {
95
96
return [
96
97
...new Set (
97
98
cliProps
98
- . filter ( ( [ , definition ] ) => definition . type === type )
99
+ . filter ( ( [ , definition ] ) => definition . validator . type === type )
99
100
. flatMap ( ( [ key ] ) => [ key , kebabCase ( key ) ] )
100
101
) ,
101
102
] ;
@@ -170,30 +171,15 @@ function validatePreferences(
170
171
// as an option value, e.g. an object into an array of key-value pairs
171
172
const process = allPreferencesProps [ key ] . customPostProcess ;
172
173
const value = process ? process ( rawValue , error ) : rawValue ;
173
- // `typeof` + `isArray` is good enough for everything we need right now, but we can of course expand this check over time
174
- if (
175
- ( Array . isArray ( value ) ? 'array' : typeof value ) !==
176
- allPreferencesProps [ key ] . type
177
- ) {
178
- error (
179
- `Type for option "${ key } " mismatches: expected ${
180
- allPreferencesProps [ key ] . type
181
- } , received ${ typeof value } `
182
- ) ;
183
- continue ;
184
- }
185
- if (
186
- allPreferencesProps [ key ] . values &&
187
- ! ( allPreferencesProps [ key ] . values as unknown [ ] ) ?. includes ( value )
188
- ) {
189
- error (
190
- `Value for option "${ key } " is not allowed: expected one of [${ String (
191
- allPreferencesProps [ key ] . values ?. join ( ', ' )
192
- ) } ], received ${ String ( value ) } `
193
- ) ;
174
+
175
+ const validationResults =
176
+ allPreferencesProps [ key ] . validator . validate ( value ) ;
177
+ if ( validationResults . error ) {
178
+ error ( `${ key } : ${ validationResults . error . message } ` ) ;
194
179
continue ;
195
180
}
196
- obj [ key ] = value as any ;
181
+
182
+ obj [ key ] = validationResults . value as any ;
197
183
}
198
184
return [ obj , errors ] ;
199
185
}
0 commit comments