Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
feat: support notAllowEmptyString
  • Loading branch information
MH4GF committed Jul 31, 2024
commit 59bd054ac65b21837142772149af05a39b74f7bf
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
78 changes: 76 additions & 2 deletions tests/valibot.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<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