Skip to content

Commit 58b8451

Browse files
authored
Merge pull request #612 from Code-Hex/add/enum-support
added support for URI format in directive config
2 parents 190056d + 898f824 commit 58b8451

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,8 @@ generates:
241241
# Replace $1 with specified `startsWith` argument value of the constraint directive
242242
startsWith: [matches, /^$1/]
243243
format:
244+
# This example means `validation-schema: directive-arg`
245+
# directive-arg is supported String and Enum.
244246
email: email
245247
```
246248
@@ -279,6 +281,8 @@ generates:
279281
# Replace $1 with specified `startsWith` argument value of the constraint directive
280282
startsWith: [regex, /^$1/, message]
281283
format:
284+
# This example means `validation-schema: directive-arg`
285+
# directive-arg is supported String and Enum.
282286
email: email
283287
```
284288

src/directive.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ function buildApiFromDirectiveArguments(config: FormattedDirectiveArguments, arg
146146
}
147147

148148
function buildApiFromDirectiveObjectArguments(config: FormattedDirectiveObjectArguments, argValue: ConstValueNode): string {
149-
if (argValue.kind !== Kind.STRING)
149+
if (argValue.kind !== Kind.STRING && argValue.kind !== Kind.ENUM)
150150
return '';
151151

152152
const validationSchema = config[argValue.value];

tests/directive.spec.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,25 @@ describe('format directive config', () => {
575575
},
576576
want: `.required("message").min(100).email()`,
577577
},
578+
{
579+
name: 'enum',
580+
args: {
581+
config: {
582+
constraint: {
583+
format: {
584+
URI: ['uri'],
585+
},
586+
},
587+
},
588+
args: [
589+
// @constraint(format: EMAIL)
590+
buildConstDirectiveNodes('constraint', {
591+
format: 'URI',
592+
}),
593+
],
594+
},
595+
want: `.uri()`,
596+
},
578597
];
579598
for (const tc of cases) {
580599
it(tc.name, () => {

0 commit comments

Comments
 (0)