Skip to content

Commit 59bd054

Browse files
committed
feat: support notAllowEmptyString
1 parent a7a7b97 commit 59bd054

File tree

2 files changed

+83
-4
lines changed

2 files changed

+83
-4
lines changed

src/valibot/index.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,8 +221,13 @@ function generateFieldTypeValibotSchema(config: ValidationSchemaPluginConfig, vi
221221

222222
const actions = actionsFromDirectives(config, field);
223223

224-
if (isNonNullType(parentType))
225-
return pipeSchemaAndActions(gen, actions); ;
224+
if (isNonNullType(parentType)) {
225+
if (visitor.shouldEmitAsNotAllowEmptyString(type.name.value)) {
226+
actions.push('v.minLength(1)');
227+
}
228+
229+
return pipeSchemaAndActions(gen, actions);
230+
}
226231

227232
return `v.nullish(${pipeSchemaAndActions(gen, actions)})`;
228233
}

tests/valibot.spec.ts

Lines changed: 76 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -322,8 +322,82 @@ describe('valibot', () => {
322322
"
323323
`);
324324
});
325-
it.todo('with notAllowEmptyString')
326-
it.todo('with notAllowEmptyString issue #386')
325+
it('with notAllowEmptyString', async () => {
326+
const schema = buildSchema(/* GraphQL */ `
327+
input PrimitiveInput {
328+
a: ID!
329+
b: String!
330+
c: Boolean!
331+
d: Int!
332+
e: Float!
333+
}
334+
`);
335+
const result = await plugin(
336+
schema,
337+
[],
338+
{
339+
schema: 'valibot',
340+
notAllowEmptyString: true,
341+
scalars: {
342+
ID: 'string',
343+
},
344+
},
345+
{},
346+
);
347+
expect(result.content).toMatchInlineSnapshot(`
348+
"
349+
350+
export function PrimitiveInputSchema(): v.GenericSchema<PrimitiveInput> {
351+
return v.object({
352+
a: v.pipe(v.string(), v.minLength(1)),
353+
b: v.pipe(v.string(), v.minLength(1)),
354+
c: v.boolean(),
355+
d: v.number(),
356+
e: v.number()
357+
})
358+
}
359+
"
360+
`)
361+
})
362+
it('with notAllowEmptyString issue #386', async () => {
363+
const schema = buildSchema(/* GraphQL */ `
364+
input InputOne {
365+
field: InputNested!
366+
}
367+
368+
input InputNested {
369+
field: String!
370+
}
371+
`);
372+
const result = await plugin(
373+
schema,
374+
[],
375+
{
376+
schema: 'valibot',
377+
notAllowEmptyString: true,
378+
scalars: {
379+
ID: 'string',
380+
},
381+
},
382+
{},
383+
);
384+
expect(result.content).toMatchInlineSnapshot(`
385+
"
386+
387+
export function InputOneSchema(): v.GenericSchema<InputOne> {
388+
return v.object({
389+
field: v.lazy(() => InputNestedSchema())
390+
})
391+
}
392+
393+
export function InputNestedSchema(): v.GenericSchema<InputNested> {
394+
return v.object({
395+
field: v.pipe(v.string(), v.minLength(1))
396+
})
397+
}
398+
"
399+
`)
400+
})
327401
it('with scalarSchemas', async () => {
328402
const schema = buildSchema(/* GraphQL */ `
329403
input ScalarsInput {

0 commit comments

Comments
 (0)