Skip to content

Commit 71b6308

Browse files
authored
Merge pull request #755 from MH4GF/implement-valibot
Implement some unfinished tests for valibot
2 parents d05453f + 4646de0 commit 71b6308

File tree

2 files changed

+171
-7
lines changed

2 files changed

+171
-7
lines changed

src/valibot/index.ts

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

225225
const actions = actionsFromDirectives(config, field);
226226

227-
if (isNonNullType(parentType))
228-
return pipeSchemaAndActions(gen, actions); ;
227+
if (isNonNullType(parentType)) {
228+
if (visitor.shouldEmitAsNotAllowEmptyString(type.name.value)) {
229+
actions.push('v.minLength(1)');
230+
}
231+
232+
return pipeSchemaAndActions(gen, actions);
233+
}
229234

230235
return `v.nullish(${pipeSchemaAndActions(gen, actions)})`;
231236
}

tests/valibot.spec.ts

Lines changed: 164 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,28 @@ describe('valibot', () => {
129129
"
130130
`);
131131
})
132-
it.todo('nested input object')
132+
it('nested input object', async () => {
133+
const schema = buildSchema(/* GraphQL */ `
134+
input NestedInput {
135+
child: NestedInput
136+
childrens: [NestedInput]
137+
}
138+
`);
139+
const scalars = undefined
140+
const result = await plugin(schema, [], { schema: 'valibot', scalars }, {});
141+
142+
expect(result.content).toMatchInlineSnapshot(`
143+
"
144+
145+
export function NestedInputSchema(): v.GenericSchema<NestedInput> {
146+
return v.object({
147+
child: v.lazy(() => v.nullish(NestedInputSchema())),
148+
childrens: v.nullish(v.array(v.lazy(() => v.nullable(NestedInputSchema()))))
149+
})
150+
}
151+
"
152+
`)
153+
})
133154
it('enum', async () => {
134155
const schema = buildSchema(/* GraphQL */ `
135156
enum PageType {
@@ -301,8 +322,82 @@ describe('valibot', () => {
301322
"
302323
`);
303324
});
304-
it.todo('with notAllowEmptyString')
305-
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+
})
306401
it('with scalarSchemas', async () => {
307402
const schema = buildSchema(/* GraphQL */ `
308403
input ScalarsInput {
@@ -338,8 +433,72 @@ describe('valibot', () => {
338433
"
339434
`)
340435
});
341-
it.todo('with typesPrefix')
342-
it.todo('with typesSuffix')
436+
it('with typesPrefix', async () => {
437+
const schema = buildSchema(/* GraphQL */ `
438+
input Say {
439+
phrase: String!
440+
}
441+
`);
442+
const result = await plugin(
443+
schema,
444+
[],
445+
{
446+
schema: 'valibot',
447+
typesPrefix: 'I',
448+
importFrom: './types',
449+
},
450+
{},
451+
);
452+
expect(result.prepend).toMatchInlineSnapshot(`
453+
[
454+
"import * as v from 'valibot'",
455+
"import { ISay } from './types'",
456+
]
457+
`)
458+
expect(result.content).toMatchInlineSnapshot(`
459+
"
460+
461+
export function ISaySchema(): v.GenericSchema<ISay> {
462+
return v.object({
463+
phrase: v.string()
464+
})
465+
}
466+
"
467+
`)
468+
})
469+
it('with typesSuffix', async () => {
470+
const schema = buildSchema(/* GraphQL */ `
471+
input Say {
472+
phrase: String!
473+
}
474+
`);
475+
const result = await plugin(
476+
schema,
477+
[],
478+
{
479+
schema: 'valibot',
480+
typesSuffix: 'I',
481+
importFrom: './types',
482+
},
483+
{},
484+
);
485+
expect(result.prepend).toMatchInlineSnapshot(`
486+
[
487+
"import * as v from 'valibot'",
488+
"import { SayI } from './types'",
489+
]
490+
`)
491+
expect(result.content).toMatchInlineSnapshot(`
492+
"
493+
494+
export function SayISchema(): v.GenericSchema<SayI> {
495+
return v.object({
496+
phrase: v.string()
497+
})
498+
}
499+
"
500+
`)
501+
})
343502
it.todo('with default input values')
344503
describe('issues #19', () => {
345504
it('string field', async () => {

0 commit comments

Comments
 (0)