@@ -20,6 +20,7 @@ import {
2020 FieldDefinitionNode ,
2121 InterfaceTypeDefinitionNode ,
2222 Kind ,
23+ ListValueNode ,
2324 ObjectTypeDefinitionNode ,
2425 StringValueNode ,
2526 TypeNode ,
@@ -82,11 +83,24 @@ const validateNotPrimaryKey = (field: FieldDefinitionNode): void => {
8283 }
8384} ;
8485
86+ const validateNotCompositeKeyMember = ( config : DefaultValueDirectiveConfiguration ) : void => {
87+ const objectDirectives = config . object . fields ?. flatMap ( ( f ) => f . directives ) ;
88+ const primaryKeyDirective = objectDirectives ?. find ( ( dir ) => dir ?. name . value === 'primaryKey' ) ;
89+ if ( primaryKeyDirective ) {
90+ const sortKeyFields = primaryKeyDirective . arguments ?. find ( ( arg ) => arg . name . value === 'sortKeyFields' ) ?. value as ListValueNode ;
91+ const sortKeys = sortKeyFields ?. values as StringValueNode [ ] ;
92+ if ( sortKeys ?. some ( ( sortKey ) => sortKey . value === config . field . name . value ) ) {
93+ throw new InvalidDirectiveError ( 'The @default directive may not be applied to composite key member fields.' ) ;
94+ }
95+ }
96+ } ;
97+
8598const validate = ( ctx : TransformerSchemaVisitStepContextProvider , config : DefaultValueDirectiveConfiguration ) : void => {
8699 validateModelDirective ( config ) ;
87100 validateFieldType ( ctx , config . field . type ) ;
88101 validateDirectiveArguments ( config . directive ) ;
89102 validateNotPrimaryKey ( config . field ) ;
103+ validateNotCompositeKeyMember ( config ) ;
90104
91105 // Validate the default values only for the DynamoDB datasource.
92106 // For SQL, the database determines and sets the default value. We will not validate the value in transformers.
0 commit comments