From a7a7b97261d73611095b204f3faaf7c0f77ae9c9 Mon Sep 17 00:00:00 2001 From: MH4GF Date: Wed, 31 Jul 2024 08:45:53 +0900 Subject: [PATCH 1/4] test: support nested input object --- tests/valibot.spec.ts | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/tests/valibot.spec.ts b/tests/valibot.spec.ts index 61f5a535..1d196365 100644 --- a/tests/valibot.spec.ts +++ b/tests/valibot.spec.ts @@ -129,7 +129,28 @@ describe('valibot', () => { " `); }) - it.todo('nested input object') + it('nested input object', async () => { + const schema = buildSchema(/* GraphQL */ ` + input NestedInput { + child: NestedInput + childrens: [NestedInput] + } + `); + const scalars = undefined + const result = await plugin(schema, [], { schema: 'valibot', scalars }, {}); + + expect(result.content).toMatchInlineSnapshot(` + " + + export function NestedInputSchema(): v.GenericSchema { + return v.object({ + child: v.lazy(() => v.nullish(NestedInputSchema())), + childrens: v.nullish(v.array(v.lazy(() => v.nullable(NestedInputSchema())))) + }) + } + " + `) + }) it('enum', async () => { const schema = buildSchema(/* GraphQL */ ` enum PageType { From 59bd054ac65b21837142772149af05a39b74f7bf Mon Sep 17 00:00:00 2001 From: MH4GF Date: Wed, 31 Jul 2024 08:58:16 +0900 Subject: [PATCH 2/4] feat: support notAllowEmptyString --- src/valibot/index.ts | 9 +++-- tests/valibot.spec.ts | 78 +++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 83 insertions(+), 4 deletions(-) diff --git a/src/valibot/index.ts b/src/valibot/index.ts index 187d8e76..327f540b 100644 --- a/src/valibot/index.ts +++ b/src/valibot/index.ts @@ -221,8 +221,13 @@ function generateFieldTypeValibotSchema(config: ValidationSchemaPluginConfig, vi const actions = actionsFromDirectives(config, field); - if (isNonNullType(parentType)) - return pipeSchemaAndActions(gen, actions); ; + if (isNonNullType(parentType)) { + if (visitor.shouldEmitAsNotAllowEmptyString(type.name.value)) { + actions.push('v.minLength(1)'); + } + + return pipeSchemaAndActions(gen, actions); + } return `v.nullish(${pipeSchemaAndActions(gen, actions)})`; } diff --git a/tests/valibot.spec.ts b/tests/valibot.spec.ts index 1d196365..ba451a38 100644 --- a/tests/valibot.spec.ts +++ b/tests/valibot.spec.ts @@ -322,8 +322,82 @@ describe('valibot', () => { " `); }); - it.todo('with notAllowEmptyString') - it.todo('with notAllowEmptyString issue #386') + 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(): v.GenericSchema { + return v.object({ + a: v.pipe(v.string(), v.minLength(1)), + b: v.pipe(v.string(), v.minLength(1)), + c: v.boolean(), + d: v.number(), + e: v.number() + }) + } + " + `) + }) + it('with notAllowEmptyString issue #386', async () => { + const schema = buildSchema(/* GraphQL */ ` + input InputOne { + field: InputNested! + } + + input InputNested { + field: String! + } + `); + const result = await plugin( + schema, + [], + { + schema: 'valibot', + notAllowEmptyString: true, + scalars: { + ID: 'string', + }, + }, + {}, + ); + expect(result.content).toMatchInlineSnapshot(` + " + + export function InputOneSchema(): v.GenericSchema { + return v.object({ + field: v.lazy(() => InputNestedSchema()) + }) + } + + export function InputNestedSchema(): v.GenericSchema { + return v.object({ + field: v.pipe(v.string(), v.minLength(1)) + }) + } + " + `) + }) it('with scalarSchemas', async () => { const schema = buildSchema(/* GraphQL */ ` input ScalarsInput { From e4c7b4dbf4aeb0ef1c9d5aacec63b9d94be2fa1e Mon Sep 17 00:00:00 2001 From: MH4GF Date: Wed, 31 Jul 2024 09:00:24 +0900 Subject: [PATCH 3/4] test: support typesPrefix --- tests/valibot.spec.ts | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/tests/valibot.spec.ts b/tests/valibot.spec.ts index ba451a38..92ad0069 100644 --- a/tests/valibot.spec.ts +++ b/tests/valibot.spec.ts @@ -433,7 +433,39 @@ describe('valibot', () => { " `) }); - it.todo('with typesPrefix') + it('with typesPrefix', async () => { + const schema = buildSchema(/* GraphQL */ ` + input Say { + phrase: String! + } + `); + const result = await plugin( + schema, + [], + { + schema: 'valibot', + typesPrefix: 'I', + importFrom: './types', + }, + {}, + ); + expect(result.prepend).toMatchInlineSnapshot(` + [ + "import * as v from 'valibot'", + "import { ISay } from './types'", + ] + `) + expect(result.content).toMatchInlineSnapshot(` + " + + export function ISaySchema(): v.GenericSchema { + return v.object({ + phrase: v.string() + }) + } + " + `) + }) it.todo('with typesSuffix') it.todo('with default input values') describe('issues #19', () => { From 4646de028ed601207a7f85fc2d006b5d529855a4 Mon Sep 17 00:00:00 2001 From: MH4GF Date: Wed, 31 Jul 2024 09:02:21 +0900 Subject: [PATCH 4/4] test: support typeSuffix --- tests/valibot.spec.ts | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/tests/valibot.spec.ts b/tests/valibot.spec.ts index 92ad0069..ad77d5f6 100644 --- a/tests/valibot.spec.ts +++ b/tests/valibot.spec.ts @@ -466,7 +466,39 @@ describe('valibot', () => { " `) }) - it.todo('with typesSuffix') + it('with typesSuffix', async () => { + const schema = buildSchema(/* GraphQL */ ` + input Say { + phrase: String! + } + `); + const result = await plugin( + schema, + [], + { + schema: 'valibot', + typesSuffix: 'I', + importFrom: './types', + }, + {}, + ); + expect(result.prepend).toMatchInlineSnapshot(` + [ + "import * as v from 'valibot'", + "import { SayI } from './types'", + ] + `) + expect(result.content).toMatchInlineSnapshot(` + " + + export function SayISchema(): v.GenericSchema { + return v.object({ + phrase: v.string() + }) + } + " + `) + }) it.todo('with default input values') describe('issues #19', () => { it('string field', async () => {