Skip to content

Commit 3b0c467

Browse files
committed
feat: support notAllowEmptyString option
1 parent 056596b commit 3b0c467

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

src/valibot/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,12 @@ function generateFieldTypeValibotSchema(config: ValidationSchemaPluginConfig, vi
117117
if (isListType(parentType))
118118
return `v.nullable(${gen})`;
119119

120-
if (isNonNullType(parentType))
120+
if (isNonNullType(parentType)) {
121+
if (visitor.shouldEmitAsNotAllowEmptyString(type.name.value))
122+
return "v.string([v.minLength(1)])"; // TODO
123+
121124
return gen;
125+
}
122126

123127
return `v.nullish(${gen})`;
124128
}

tests/valibot.spec.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,4 +209,41 @@ describe('valibot', () => {
209209
`);
210210
});
211211

212+
it('with notAllowEmptyString', async () => {
213+
const schema = buildSchema(/* GraphQL */ `
214+
input PrimitiveInput {
215+
a: ID!
216+
b: String!
217+
c: Boolean!
218+
d: Int!
219+
e: Float!
220+
}
221+
`);
222+
const result = await plugin(
223+
schema,
224+
[],
225+
{
226+
schema: 'valibot',
227+
notAllowEmptyString: true,
228+
scalars: {
229+
ID: 'string',
230+
},
231+
},
232+
{},
233+
);
234+
expect(result.content).toMatchInlineSnapshot(`
235+
"
236+
237+
export function PrimitiveInputSchema() {
238+
return v.object({
239+
a: v.string([v.minLength(1)]),
240+
b: v.string([v.minLength(1)]),
241+
c: v.boolean(),
242+
d: v.number(),
243+
e: v.number()
244+
})
245+
}
246+
"
247+
`)
248+
});
212249
})

0 commit comments

Comments
 (0)