Skip to content

Commit 48b2aa7

Browse files
committed
feat: support directives
1 parent 3c3f73a commit 48b2aa7

File tree

2 files changed

+92
-2
lines changed

2 files changed

+92
-2
lines changed

src/valibot/index.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import type {
1212
import type { ValidationSchemaPluginConfig } from '../config';
1313
import { BaseSchemaVisitor } from '../schema_visitor';
1414
import type { Visitor } from '../visitor';
15+
import { buildApiForValibot, formatDirectiveConfig } from '../directive';
1516
import {
1617
isInput,
1718
isListType,
@@ -114,15 +115,33 @@ function generateFieldTypeValibotSchema(config: ValidationSchemaPluginConfig, vi
114115
if (isListType(parentType))
115116
return `v.nullable(${gen})`;
116117

118+
const actions = actionsFromDirectives(config, field);
119+
117120
if (isNonNullType(parentType))
118-
return gen;
121+
return pipeSchemaAndActions(gen, actions); ;
119122

120-
return `v.nullish(${gen})`;
123+
return `v.nullish(${pipeSchemaAndActions(gen, actions)})`;
121124
}
122125
console.warn('unhandled type:', type);
123126
return '';
124127
}
125128

129+
function actionsFromDirectives(config: ValidationSchemaPluginConfig, field: InputValueDefinitionNode | FieldDefinitionNode): string[] {
130+
if (config.directives && field.directives) {
131+
const formatted = formatDirectiveConfig(config.directives);
132+
return buildApiForValibot(formatted, field.directives);
133+
}
134+
135+
return [];
136+
}
137+
138+
function pipeSchemaAndActions(schema: string, actions: string[]): string {
139+
if (actions.length === 0)
140+
return schema;
141+
142+
return `v.pipe(${schema}, ${actions.join(', ')})`;
143+
}
144+
126145
function generateNameNodeValibotSchema(config: ValidationSchemaPluginConfig, visitor: Visitor, node: NameNode): string {
127146
const converter = visitor.getNameNodeConverter(node);
128147

tests/valibot.spec.ts

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,4 +338,75 @@ describe('valibot', () => {
338338
"
339339
`)
340340
});
341+
it.todo('with typesPrefix')
342+
it.todo('with typesSuffix')
343+
it.todo('with default input values')
344+
describe('issues #19', () => {
345+
it('string field', async () => {
346+
const schema = buildSchema(/* GraphQL */ `
347+
input UserCreateInput {
348+
profile: String @constraint(minLength: 1, maxLength: 5000)
349+
}
350+
directive @constraint(minLength: Int!, maxLength: Int!) on INPUT_FIELD_DEFINITION
351+
`);
352+
const result = await plugin(
353+
schema,
354+
[],
355+
{
356+
schema: 'valibot',
357+
directives: {
358+
constraint: {
359+
minLength: ['minLength', '$1', 'Please input more than $1'],
360+
maxLength: ['maxLength', '$1', 'Please input less than $1'],
361+
},
362+
},
363+
},
364+
{},
365+
);
366+
expect(result.content).toMatchInlineSnapshot(`
367+
"
368+
369+
export function UserCreateInputSchema(): v.GenericSchema<UserCreateInput> {
370+
return v.object({
371+
profile: v.nullish(v.pipe(v.string(), v.minLength(1, "Please input more than 1"), v.maxLength(5000, "Please input less than 5000")))
372+
})
373+
}
374+
"
375+
`)
376+
});
377+
378+
it('not null field', async () => {
379+
const schema = buildSchema(/* GraphQL */ `
380+
input UserCreateInput {
381+
profile: String! @constraint(minLength: 1, maxLength: 5000)
382+
}
383+
directive @constraint(minLength: Int!, maxLength: Int!) on INPUT_FIELD_DEFINITION
384+
`);
385+
const result = await plugin(
386+
schema,
387+
[],
388+
{
389+
schema: 'valibot',
390+
directives: {
391+
constraint: {
392+
minLength: ['minLength', '$1', 'Please input more than $1'],
393+
maxLength: ['maxLength', '$1', 'Please input less than $1'],
394+
},
395+
},
396+
},
397+
{},
398+
);
399+
400+
expect(result.content).toMatchInlineSnapshot(`
401+
"
402+
403+
export function UserCreateInputSchema(): v.GenericSchema<UserCreateInput> {
404+
return v.object({
405+
profile: v.pipe(v.string(), v.minLength(1, "Please input more than 1"), v.maxLength(5000, "Please input less than 5000"))
406+
})
407+
}
408+
"
409+
`)
410+
});
411+
})
341412
})

0 commit comments

Comments
 (0)