Skip to content

Implement some unfinished tests for valibot #755

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

Merged
merged 4 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 7 additions & 2 deletions src/valibot/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)})`;
}
Expand Down
169 changes: 164 additions & 5 deletions tests/valibot.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<NestedInput> {
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 {
Expand Down Expand Up @@ -301,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<PrimitiveInput> {
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<InputOne> {
return v.object({
field: v.lazy(() => InputNestedSchema())
})
}

export function InputNestedSchema(): v.GenericSchema<InputNested> {
return v.object({
field: v.pipe(v.string(), v.minLength(1))
})
}
"
`)
})
it('with scalarSchemas', async () => {
const schema = buildSchema(/* GraphQL */ `
input ScalarsInput {
Expand Down Expand Up @@ -338,8 +433,72 @@ describe('valibot', () => {
"
`)
});
it.todo('with typesPrefix')
it.todo('with typesSuffix')
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<ISay> {
return v.object({
phrase: v.string()
})
}
"
`)
})
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<SayI> {
return v.object({
phrase: v.string()
})
}
"
`)
})
it.todo('with default input values')
describe('issues #19', () => {
it('string field', async () => {
Expand Down
Loading