@@ -5,8 +5,9 @@ import yaml from 'js-yaml';
55import type { Options as YargsOptions } from 'yargs-parser' ;
66import yargsParser from 'yargs-parser' ;
77import { kebabCase } from 'lodash' ;
8- import type { AmpersandType , AllPreferences } from './preferences' ;
8+ import type { AllPreferences } from './preferences' ;
99import { allPreferencesProps } from './preferences' ;
10+ import type { Types as JoiTypes } from 'joi' ;
1011
1112import { createLoggerAndTelemetry } from '@mongodb-js/compass-logging' ;
1213const { log, mongoLogId } = createLoggerAndTelemetry ( 'COMPASS-PREFERENCES' ) ;
@@ -91,11 +92,11 @@ async function loadGlobalPreferences(
9192const cliProps = Object . entries ( allPreferencesProps ) . filter (
9293 ( [ , definition ] ) => definition . cli
9394) ;
94- function getCliPropNamesByType ( type : AmpersandType < any > ) : string [ ] {
95+ function getCliPropNamesByType ( type : JoiTypes ) : string [ ] {
9596 return [
9697 ...new Set (
9798 cliProps
98- . filter ( ( [ , definition ] ) => definition . type === type )
99+ . filter ( ( [ , definition ] ) => definition . validator . type === type )
99100 . flatMap ( ( [ key ] ) => [ key , kebabCase ( key ) ] )
100101 ) ,
101102 ] ;
@@ -170,30 +171,15 @@ function validatePreferences(
170171 // as an option value, e.g. an object into an array of key-value pairs
171172 const process = allPreferencesProps [ key ] . customPostProcess ;
172173 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 } ` ) ;
194179 continue ;
195180 }
196- obj [ key ] = value as any ;
181+
182+ obj [ key ] = validationResults . value as any ;
197183 }
198184 return [ obj , errors ] ;
199185}
0 commit comments