Skip to content

Commit 022f468

Browse files
committed
refactor: change buildApiFromDirectiveArguments return type to string[]
1 parent 2fb1e1b commit 022f468

File tree

2 files changed

+16
-17
lines changed

2 files changed

+16
-17
lines changed

src/directive.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ export function buildApi(config: FormattedDirectiveConfig, directives: ReadonlyA
115115
.map((directive) => {
116116
const directiveName = directive.name.value;
117117
const argsConfig = config[directiveName];
118-
return buildApiFromDirectiveArguments(argsConfig, directive.arguments ?? []);
118+
return buildApiFromDirectiveArguments(argsConfig, directive.arguments ?? []).join('');
119119
})
120120
.join('')
121121
}
@@ -132,7 +132,7 @@ function buildApiSchema(validationSchema: string[] | undefined, argValue: ConstV
132132
return `.${schemaApi}(${schemaApiArgs.join(', ')})`;
133133
}
134134

135-
function buildApiFromDirectiveArguments(config: FormattedDirectiveArguments, args: ReadonlyArray<ConstArgumentNode>): string {
135+
function buildApiFromDirectiveArguments(config: FormattedDirectiveArguments, args: ReadonlyArray<ConstArgumentNode>): string[] {
136136
return args
137137
.map((arg) => {
138138
const argName = arg.name.value;
@@ -142,7 +142,6 @@ function buildApiFromDirectiveArguments(config: FormattedDirectiveArguments, arg
142142

143143
return buildApiSchema(validationSchema, arg.value);
144144
})
145-
.join('');
146145
}
147146

148147
function buildApiFromDirectiveObjectArguments(config: FormattedDirectiveObjectArguments, argValue: ConstValueNode): string {

tests/directive.spec.ts

+14-14
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ describe('format directive config', () => {
352352
config: FormattedDirectiveArguments
353353
args: ReadonlyArray<ConstArgumentNode>
354354
}
355-
want: string
355+
want: string[]
356356
}[] = [
357357
{
358358
name: 'string',
@@ -364,7 +364,7 @@ describe('format directive config', () => {
364364
msg: `"hello"`,
365365
}),
366366
},
367-
want: `.required("hello")`,
367+
want: [`.required("hello")`],
368368
},
369369
{
370370
name: 'string with additional stuff',
@@ -376,7 +376,7 @@ describe('format directive config', () => {
376376
startWith: `"hello"`,
377377
}),
378378
},
379-
want: `.matched("^hello")`,
379+
want: [`.matched("^hello")`],
380380
},
381381
{
382382
name: 'number',
@@ -388,7 +388,7 @@ describe('format directive config', () => {
388388
minLength: `1`,
389389
}),
390390
},
391-
want: `.min(1)`,
391+
want: [`.min(1)`],
392392
},
393393
{
394394
name: 'boolean',
@@ -401,7 +401,7 @@ describe('format directive config', () => {
401401
enabled: `true`,
402402
}),
403403
},
404-
want: `.strict(true)`,
404+
want: [`.strict(true)`],
405405
},
406406
{
407407
name: 'list',
@@ -413,7 +413,7 @@ describe('format directive config', () => {
413413
minLength: `[1, "message"]`,
414414
}),
415415
},
416-
want: `.min(1, "message")`,
416+
want: [`.min(1, "message")`],
417417
},
418418
{
419419
name: 'object in list',
@@ -425,7 +425,7 @@ describe('format directive config', () => {
425425
matches: `["hello", {message:"message", excludeEmptyString:true}]`,
426426
}),
427427
},
428-
want: `.matches("hello", {"message":"message","excludeEmptyString":true})`,
428+
want: [`.matches("hello", {"message":"message","excludeEmptyString":true})`],
429429
},
430430
{
431431
name: 'two arguments but matched to first argument',
@@ -438,7 +438,7 @@ describe('format directive config', () => {
438438
msg2: `"world"`,
439439
}),
440440
},
441-
want: `.required("hello")`,
441+
want: [`.required("hello")`, ``],
442442
},
443443
{
444444
name: 'two arguments but matched to second argument',
@@ -451,7 +451,7 @@ describe('format directive config', () => {
451451
msg2: `"world"`,
452452
}),
453453
},
454-
want: `.required("world")`,
454+
want: [``, `.required("world")`],
455455
},
456456
{
457457
name: 'two arguments matched all',
@@ -465,7 +465,7 @@ describe('format directive config', () => {
465465
minLength: `1`,
466466
}),
467467
},
468-
want: `.required("message").min(1)`,
468+
want: [`.required("message")`, `.min(1)`],
469469
},
470470
{
471471
name: 'argument matches validation schema api',
@@ -479,7 +479,7 @@ describe('format directive config', () => {
479479
format: `"uri"`,
480480
}),
481481
},
482-
want: `.url()`,
482+
want: [`.url()`],
483483
},
484484
{
485485
name: 'argument matched argument but doesn\'t match api',
@@ -493,7 +493,7 @@ describe('format directive config', () => {
493493
format: `"uuid"`,
494494
}),
495495
},
496-
want: ``,
496+
want: [``],
497497
},
498498
{
499499
name: 'complex',
@@ -509,7 +509,7 @@ describe('format directive config', () => {
509509
format: `"uri"`,
510510
}),
511511
},
512-
want: `.required("message").url()`,
512+
want: [`.required("message")`, `.url()`],
513513
},
514514
{
515515
name: 'complex 2',
@@ -525,7 +525,7 @@ describe('format directive config', () => {
525525
format: `"uuid"`,
526526
}),
527527
},
528-
want: `.required("message")`,
528+
want: [`.required("message")`, ``],
529529
},
530530
];
531531
for (const tc of cases) {

0 commit comments

Comments
 (0)