Skip to content

Commit b9cb35b

Browse files
committed
refactor: add _buildApiFromDirectiveArguments()
1 parent 75c4fbe commit b9cb35b

File tree

2 files changed

+21
-17
lines changed

2 files changed

+21
-17
lines changed

src/directive.ts

Lines changed: 7 additions & 3 deletions
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 ?? []).join('');
118+
return buildApiFromDirectiveArguments(argsConfig, directive.arguments ?? []);
119119
})
120120
.join('')
121121
}
@@ -148,7 +148,7 @@ export function buildApiForValibot(config: FormattedDirectiveConfig, directives:
148148
.map((directive) => {
149149
const directiveName = directive.name.value;
150150
const argsConfig = config[directiveName];
151-
const apis = buildApiFromDirectiveArguments(argsConfig, directive.arguments ?? []);
151+
const apis = _buildApiFromDirectiveArguments(argsConfig, directive.arguments ?? []);
152152
return apis.map(api => `v${api}`);
153153
}).flat()
154154
}
@@ -165,7 +165,11 @@ function buildApiSchema(validationSchema: string[] | undefined, argValue: ConstV
165165
return `.${schemaApi}(${schemaApiArgs.join(', ')})`;
166166
}
167167

168-
function buildApiFromDirectiveArguments(config: FormattedDirectiveArguments, args: ReadonlyArray<ConstArgumentNode>): string[] {
168+
function buildApiFromDirectiveArguments(config: FormattedDirectiveArguments, args: ReadonlyArray<ConstArgumentNode>): string {
169+
return _buildApiFromDirectiveArguments(config, args).join('');
170+
}
171+
172+
function _buildApiFromDirectiveArguments(config: FormattedDirectiveArguments, args: ReadonlyArray<ConstArgumentNode>): string[] {
169173
return args
170174
.map((arg) => {
171175
const argName = arg.name.value;

tests/directive.spec.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ describe('format directive config', () => {
353353
config: FormattedDirectiveArguments
354354
args: ReadonlyArray<ConstArgumentNode>
355355
}
356-
want: string[]
356+
want: string
357357
}[] = [
358358
{
359359
name: 'string',
@@ -365,7 +365,7 @@ describe('format directive config', () => {
365365
msg: `"hello"`,
366366
}),
367367
},
368-
want: [`.required("hello")`],
368+
want: `.required("hello")`,
369369
},
370370
{
371371
name: 'string with additional stuff',
@@ -377,7 +377,7 @@ describe('format directive config', () => {
377377
startWith: `"hello"`,
378378
}),
379379
},
380-
want: [`.matched("^hello")`],
380+
want: `.matched("^hello")`,
381381
},
382382
{
383383
name: 'number',
@@ -389,7 +389,7 @@ describe('format directive config', () => {
389389
minLength: `1`,
390390
}),
391391
},
392-
want: [`.min(1)`],
392+
want: `.min(1)`,
393393
},
394394
{
395395
name: 'boolean',
@@ -402,7 +402,7 @@ describe('format directive config', () => {
402402
enabled: `true`,
403403
}),
404404
},
405-
want: [`.strict(true)`],
405+
want: `.strict(true)`,
406406
},
407407
{
408408
name: 'list',
@@ -414,7 +414,7 @@ describe('format directive config', () => {
414414
minLength: `[1, "message"]`,
415415
}),
416416
},
417-
want: [`.min(1, "message")`],
417+
want: `.min(1, "message")`,
418418
},
419419
{
420420
name: 'object in list',
@@ -426,7 +426,7 @@ describe('format directive config', () => {
426426
matches: `["hello", {message:"message", excludeEmptyString:true}]`,
427427
}),
428428
},
429-
want: [`.matches("hello", {"message":"message","excludeEmptyString":true})`],
429+
want: `.matches("hello", {"message":"message","excludeEmptyString":true})`,
430430
},
431431
{
432432
name: 'two arguments but matched to first argument',
@@ -439,7 +439,7 @@ describe('format directive config', () => {
439439
msg2: `"world"`,
440440
}),
441441
},
442-
want: [`.required("hello")`, ``],
442+
want: `.required("hello")`,
443443
},
444444
{
445445
name: 'two arguments but matched to second argument',
@@ -452,7 +452,7 @@ describe('format directive config', () => {
452452
msg2: `"world"`,
453453
}),
454454
},
455-
want: [``, `.required("world")`],
455+
want: `.required("world")`,
456456
},
457457
{
458458
name: 'two arguments matched all',
@@ -466,7 +466,7 @@ describe('format directive config', () => {
466466
minLength: `1`,
467467
}),
468468
},
469-
want: [`.required("message")`, `.min(1)`],
469+
want: `.required("message").min(1)`,
470470
},
471471
{
472472
name: 'argument matches validation schema api',
@@ -480,7 +480,7 @@ describe('format directive config', () => {
480480
format: `"uri"`,
481481
}),
482482
},
483-
want: [`.url()`],
483+
want: `.url()`,
484484
},
485485
{
486486
name: 'argument matched argument but doesn\'t match api',
@@ -494,7 +494,7 @@ describe('format directive config', () => {
494494
format: `"uuid"`,
495495
}),
496496
},
497-
want: [``],
497+
want: ``,
498498
},
499499
{
500500
name: 'complex',
@@ -510,7 +510,7 @@ describe('format directive config', () => {
510510
format: `"uri"`,
511511
}),
512512
},
513-
want: [`.required("message")`, `.url()`],
513+
want: `.required("message").url()`,
514514
},
515515
{
516516
name: 'complex 2',
@@ -526,7 +526,7 @@ describe('format directive config', () => {
526526
format: `"uuid"`,
527527
}),
528528
},
529-
want: [`.required("message")`, ``],
529+
want: `.required("message")`,
530530
},
531531
];
532532
for (const tc of cases) {

0 commit comments

Comments
 (0)