Skip to content

support valibot #660

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 18 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
feat: support notAllowEmptyString option
  • Loading branch information
MH4GF committed May 26, 2024
commit 87470d09794c5928550adaf8dd8e2341108ef7f2
6 changes: 5 additions & 1 deletion src/valibot/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,12 @@ function generateFieldTypeValibotSchema(config: ValidationSchemaPluginConfig, vi
if (isListType(parentType))
return `v.nullable(${gen})`;

if (isNonNullType(parentType))
if (isNonNullType(parentType)) {
if (visitor.shouldEmitAsNotAllowEmptyString(type.name.value))
return "v.string([v.minLength(1)])"; // TODO

return gen;
}

return `v.nullish(${gen})`;
}
Expand Down
37 changes: 37 additions & 0 deletions tests/valibot.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,4 +209,41 @@ describe('valibot', () => {
`);
});

it('with notAllowEmptyString', async () => {
const schema = buildSchema(/* GraphQL */ `
input PrimitiveInput {
a: ID!
b: String!
c: Boolean!
d: Int!
e: Float!
}
`);
const result = await plugin(
schema,
[],
{
schema: 'valibot',
notAllowEmptyString: true,
scalars: {
ID: 'string',
},
},
{},
);
expect(result.content).toMatchInlineSnapshot(`
"

export function PrimitiveInputSchema() {
return v.object({
a: v.string([v.minLength(1)]),
b: v.string([v.minLength(1)]),
c: v.boolean(),
d: v.number(),
e: v.number()
})
}
"
`)
});
})