Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions src/valibot/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,25 +211,26 @@ function generateFieldValibotSchema(config: ValidationSchemaPluginConfig, visito
return indent(`${field.name.value}: ${gen}`, indentCount);
}

function generateFieldTypeValibotSchema(config: ValidationSchemaPluginConfig, visitor: Visitor, field: InputValueDefinitionNode | FieldDefinitionNode, type: TypeNode, parentType?: TypeNode): string {
function generateFieldTypeValibotSchema(config: ValidationSchemaPluginConfig, visitor: Visitor, field: InputValueDefinitionNode | FieldDefinitionNode, type: TypeNode, parentType?: TypeNode, isRoot = true): string {
if (isListType(type)) {
const gen = generateFieldTypeValibotSchema(config, visitor, field, type.type, type);
const gen = generateFieldTypeValibotSchema(config, visitor, field, type.type, type, false);
const arrayGen = `v.array(${gen})`;
const maybeDirectivesGen = isRoot ? pipeSchemaAndActions(arrayGen, actionsFromDirectives(config, field)) : arrayGen;
if (!isNonNullType(parentType))
return `v.nullish(${arrayGen})`;
return `v.nullish(${maybeDirectivesGen})`;

return arrayGen;
return maybeDirectivesGen;
}
if (isNonNullType(type)) {
const gen = generateFieldTypeValibotSchema(config, visitor, field, type.type, type);
const gen = generateFieldTypeValibotSchema(config, visitor, field, type.type, type, isRoot);
return gen;
}
if (isNamedType(type)) {
const gen = generateNameNodeValibotSchema(config, visitor, type.name);
if (isListType(parentType))
return `v.nullable(${maybeLazy(visitor, type, gen)})`;

const actions = actionsFromDirectives(config, field);
const actions = isRoot ? actionsFromDirectives(config, field) : [];
const schema = maybeLazy(visitor, type, pipeSchemaAndActions(gen, actions));

if (isNonNullType(parentType)) {
Expand Down
39 changes: 38 additions & 1 deletion tests/valibot.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -736,7 +736,44 @@ describe('valibot', () => {
"
`)
});
it.todo('list field')
it('list field', async () => {
const schema = buildSchema(/* GraphQL */ `
input UserCreateInput {
tags: [String!]! @constraint(minLength: 1, maxLength: 10)
labels: [String!] @constraint(minLength: 1, maxLength: 10)
ids: [[String!]!]! @constraint(minLength: 1, maxLength: 10)
}
directive @constraint(minLength: Int!, maxLength: Int!) on INPUT_FIELD_DEFINITION
`);
const result = await plugin(
schema,
[],
{
schema: 'valibot',
notAllowEmptyString: true,
directives: {
constraint: {
minLength: ['minLength', '$1', 'Please input more than $1'],
maxLength: ['maxLength', '$1', 'Please input less than $1'],
},
},
},
{},
);

expect(result.content).toMatchInlineSnapshot(`
"

export function UserCreateInputSchema(): v.GenericSchema<UserCreateInput> {
return v.object({
tags: v.pipe(v.array(v.pipe(v.string(), v.minLength(1))), v.minLength(1, "Please input more than 1"), v.maxLength(10, "Please input less than 10")),
labels: v.nullish(v.pipe(v.array(v.pipe(v.string(), v.minLength(1))), v.minLength(1, "Please input more than 1"), v.maxLength(10, "Please input less than 10"))),
ids: v.pipe(v.array(v.array(v.pipe(v.string(), v.minLength(1)))), v.minLength(1, "Please input more than 1"), v.maxLength(10, "Please input less than 10"))
})
}
"
`)
});
describe('pR #112', () => {
it.todo('with notAllowEmptyString')
it.todo('without notAllowEmptyString')
Expand Down