1- import { ValueModel , ValueModelFactoryFromModel , ValidationResult , createValidationSingleError } from '../../model' ;
1+ import { ValueModel , ValidationResult , createValidationSingleError , ValueModelFactory } from '../../model' ;
22import { Path } from '../../core/path' ;
33import { ValueContext } from '../../context' ;
44
5- export interface ChoiceValueModelConfiguration {
6- choices : string [ ] ;
7- defaultValue ?: string ;
5+ export interface ChoiceValueModelConfiguration < TValue extends string = string > {
6+ choices : TValue [ ] ;
7+ defaultValue ?: TValue ;
88}
99
10- export type ChoiceValueModel = ValueModel < string , ChoiceValueModelConfiguration > ;
10+ export type ChoiceValueModel < TValue extends string = string > = ValueModel < TValue , ChoiceValueModelConfiguration < TValue > > ;
1111
1212export const choiceValueModelId = 'choice' ;
1313
14- export function createChoiceValueModel ( configuration : ChoiceValueModelConfiguration ) : ValueModelFactoryFromModel < ChoiceValueModel > {
14+ export function createChoiceValueModel < TValue extends string > (
15+ configuration : ChoiceValueModelConfiguration < TValue >
16+ ) : ValueModelFactory < TValue , ChoiceValueModelConfiguration < TValue > > {
1517 if ( configuration . choices . length < 1 ) {
1618 throw new Error ( 'At least one choice must be provided.' ) ;
1719 }
@@ -25,14 +27,14 @@ export function createChoiceValueModel(configuration: ChoiceValueModelConfigurat
2527 getDefaultValue ( ) {
2628 if ( configuration . defaultValue ) {
2729 if ( ! configuration . choices . includes ( configuration . defaultValue ) ) {
28- throw new Error ( ' Default value does not match any of the choices.' ) ;
30+ throw new Error ( ` Default value " ${ configuration . defaultValue } " does not match any of the choices.` ) ;
2931 }
3032 return configuration . defaultValue ;
3133 }
3234 return configuration . choices [ 0 ] ;
3335 } ,
3436 getVariableDefinitions : ( ) => null ,
35- validate ( context : ValueContext < ChoiceValueModel > ) : ValidationResult {
37+ validate ( context : ValueContext < ChoiceValueModel < TValue > > ) : ValidationResult {
3638 const value = context . getValue ( ) ;
3739 if ( ! configuration . choices . includes ( value ) ) {
3840 return createValidationSingleError ( 'Choice is not supported.' ) ;
0 commit comments