Skip to content

Commit 6ad3698

Browse files
committed
refactor(kotlin): improve validation annotation structure with typed interfaces
- Add DirectiveArgument interface for better type safety - Add ValidationAnnotation interface to represent annotation objects - Refactor parseDirectiveArgs to return structured objects instead of strings - Update extractValidationAnnotations methods to use typed interfaces - Improve formatValidationAnnotations to handle structured annotation data
1 parent b2e0c15 commit 6ad3698

2 files changed

Lines changed: 45 additions & 18 deletions

File tree

packages/plugins/java/kotlin/src/directive-mapping.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
* GraphQL validation directives to Jakarta Validation annotations mapping
33
*/
44

5+
export interface DirectiveArgument {
6+
name: string;
7+
value: string;
8+
}
9+
510
export const VALIDATION_DIRECTIVES: Record<string, string> = {
611
'@notBlank': 'NotBlank',
712
'@size': 'Size',
@@ -66,13 +71,16 @@ export const VALIDATION_PARAM_MAPPING: Record<string, Record<string, string>> =
6671
export function parseDirectiveArgs(
6772
directiveName: string,
6873
args: any[]
69-
): string[] {
74+
): DirectiveArgument[] {
7075
const paramMapping = VALIDATION_PARAM_MAPPING[directiveName] || {};
7176
return args.map(arg => {
7277
const argName = arg.name.value;
7378
const mappedArgName = paramMapping[argName] || argName;
7479
const value = formatArgValue(arg.value);
75-
return `${mappedArgName} = ${value}`;
80+
return {
81+
name: mappedArgName,
82+
value: value
83+
};
7684
});
7785
}
7886

packages/plugins/java/kotlin/src/visitor.ts

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,14 @@ import { KotlinResolversPluginRawConfig } from './config.js';
2929
import {
3030
VALIDATION_DIRECTIVES,
3131
parseDirectiveArgs,
32+
DirectiveArgument,
3233
} from './directive-mapping.js';
3334

35+
export interface ValidationAnnotation {
36+
name: string;
37+
params?: DirectiveArgument[];
38+
}
39+
3440
export const KOTLIN_SCALARS = {
3541
ID: 'Any',
3642
String: 'String',
@@ -186,12 +192,12 @@ ${enumValues}
186192
/**
187193
* Extract validation annotations from field directives
188194
*/
189-
private extractValidationAnnotations(field: InputValueDefinitionNode): string[] {
195+
private extractValidationAnnotations(field: InputValueDefinitionNode): ValidationAnnotation[] {
190196
if (!field.directives || field.directives.length === 0) {
191197
return [];
192198
}
193199

194-
const annotations: string[] = [];
200+
const annotations: ValidationAnnotation[] = [];
195201

196202
for (const directive of field.directives) {
197203
const directiveName = `@${directive.name.value}`;
@@ -201,13 +207,15 @@ ${enumValues}
201207
const annotationName = VALIDATION_DIRECTIVES[directiveName];
202208

203209
// Parse directive arguments
204-
let annotationParams = '';
210+
let annotationParams: DirectiveArgument[] | undefined;
205211
if (directive.arguments && directive.arguments.length > 0) {
206-
const params = parseDirectiveArgs(directiveName, Array.from(directive.arguments));
207-
annotationParams = `(${params.join(', ')})`;
212+
annotationParams = parseDirectiveArgs(directiveName, Array.from(directive.arguments));
208213
}
209214

210-
annotations.push(`${annotationName}${annotationParams}`);
215+
annotations.push({
216+
name: annotationName,
217+
params: annotationParams
218+
});
211219
}
212220
}
213221

@@ -217,10 +225,15 @@ ${enumValues}
217225
/**
218226
* Format validation annotations
219227
*/
220-
private formatValidationAnnotations(annotations: string[]): string[] {
228+
private formatValidationAnnotations(annotations: ValidationAnnotation[]): string[] {
221229
// All validation annotations need @field: prefix because they are field annotations, not class annotations
222230
const prefix = '@field:';
223-
return annotations.map(annotation => `${prefix}${annotation}`);
231+
return annotations.map(annotation => {
232+
const annotationString = annotation.params
233+
? `${annotation.name}(${annotation.params.map(param => `${param.name} = ${param.value}`).join(', ')})`
234+
: annotation.name;
235+
return `${prefix}${annotationString}`;
236+
});
224237
}
225238

226239
/**
@@ -242,12 +255,12 @@ ${enumValues}
242255
/**
243256
* Extract validation annotations from object type field directives
244257
*/
245-
private extractValidationAnnotationsForField(field: FieldDefinitionNode): string[] {
258+
private extractValidationAnnotationsForField(field: FieldDefinitionNode): ValidationAnnotation[] {
246259
if (!field.directives || field.directives.length === 0) {
247260
return [];
248261
}
249262

250-
const annotations: string[] = [];
263+
const annotations: ValidationAnnotation[] = [];
251264

252265
for (const directive of field.directives) {
253266
const directiveName = `@${directive.name.value}`;
@@ -257,13 +270,15 @@ ${enumValues}
257270
const annotationName = VALIDATION_DIRECTIVES[directiveName];
258271

259272
// Parse directive arguments
260-
let annotationParams = '';
273+
let annotationParams: DirectiveArgument[] | undefined;
261274
if (directive.arguments && directive.arguments.length > 0) {
262-
const params = parseDirectiveArgs(directiveName, Array.from(directive.arguments));
263-
annotationParams = `(${params.join(', ')})`;
275+
annotationParams = parseDirectiveArgs(directiveName, Array.from(directive.arguments));
264276
}
265277

266-
annotations.push(`${annotationName}${annotationParams}`);
278+
annotations.push({
279+
name: annotationName,
280+
params: annotationParams
281+
});
267282
}
268283
}
269284

@@ -283,8 +298,12 @@ ${enumValues}
283298
return [];
284299
}
285300

286-
// For object type fields, annotations are added directly to constructor parameters
287-
return annotations;
301+
// For object type fields, format annotations without @field: prefix since they're constructor parameters
302+
return annotations.map(annotation => {
303+
return annotation.params
304+
? `@${annotation.name}(${annotation.params.map(param => `${param.name} = ${param.value}`).join(', ')})`
305+
: `@${annotation.name}`;
306+
});
288307
}
289308

290309
protected buildInputTransfomer(

0 commit comments

Comments
 (0)