Skip to content

Commit 4eb0760

Browse files
committed
fix: Change the actions setting method to v.pipe()
1 parent a82746a commit 4eb0760

File tree

2 files changed

+62
-15
lines changed

2 files changed

+62
-15
lines changed

src/valibot/index.ts

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -119,36 +119,45 @@ function generateFieldTypeValibotSchema(config: ValidationSchemaPluginConfig, vi
119119
if (isListType(parentType))
120120
return `v.nullable(${gen})`;
121121

122-
let appliedDirectivesGen = applyDirectives(config, field, gen);
123-
124122
if (field.kind === Kind.INPUT_VALUE_DEFINITION) {
125123
const { defaultValue } = field;
126124
if (defaultValue?.kind === Kind.INT || defaultValue?.kind === Kind.FLOAT || defaultValue?.kind === Kind.BOOLEAN)
127-
appliedDirectivesGen = `v.optional(${appliedDirectivesGen}, ${defaultValue.value})`;
125+
gen = `v.optional(${gen}, ${defaultValue.value})`;
128126

129127
if (defaultValue?.kind === Kind.STRING || defaultValue?.kind === Kind.ENUM)
130-
appliedDirectivesGen = `v.optional(${appliedDirectivesGen}, "${defaultValue.value}")`;
131-
128+
gen = `v.optional(${gen}, "${defaultValue.value}")`;
132129
}
130+
131+
const actions = actionsFromDirectives(config, field);
132+
133133
if (isNonNullType(parentType)) {
134-
if (visitor.shouldEmitAsNotAllowEmptyString(type.name.value))
135-
return "v.string([v.minLength(1)])"; // TODO
134+
if (visitor.shouldEmitAsNotAllowEmptyString(type.name.value)) {
135+
actions.push('v.minLength(1)');
136+
}
136137

137-
return appliedDirectivesGen;
138+
return pipeSchemaAndActions(gen, actions);
138139
}
139140

140-
return `v.nullish(${appliedDirectivesGen})`;
141+
return `v.nullish(${pipeSchemaAndActions(gen, actions)})`;
141142
}
142143
console.warn('unhandled type:', type);
143144
return '';
144145
}
145146

146-
function applyDirectives(config: ValidationSchemaPluginConfig, field: InputValueDefinitionNode | FieldDefinitionNode, gen: string): string {
147+
function actionsFromDirectives(config: ValidationSchemaPluginConfig, field: InputValueDefinitionNode | FieldDefinitionNode): string[] {
147148
if (config.directives && field.directives) {
148149
const formatted = formatDirectiveConfig(config.directives);
149-
return `v.pipe(${gen}, ${buildApiForValibot(formatted, field.directives).join(', ')})`;
150+
return buildApiForValibot(formatted, field.directives);
150151
}
151-
return gen;
152+
153+
return [];
154+
}
155+
156+
function pipeSchemaAndActions(schema: string, actions: string[]): string {
157+
if (actions.length === 0)
158+
return schema;
159+
160+
return `v.pipe(${schema}, ${actions.join(', ')})`;
152161
}
153162

154163
function generateNameNodeValibotSchema(config: ValidationSchemaPluginConfig, visitor: Visitor, node: NameNode): string {

tests/valibot.spec.ts

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -236,8 +236,8 @@ describe('valibot', () => {
236236
237237
export function PrimitiveInputSchema() {
238238
return v.object({
239-
a: v.string([v.minLength(1)]),
240-
b: v.string([v.minLength(1)]),
239+
a: v.pipe(v.string(), v.minLength(1)),
240+
b: v.pipe(v.string(), v.minLength(1)),
241241
c: v.boolean(),
242242
d: v.number(),
243243
e: v.number()
@@ -280,7 +280,7 @@ describe('valibot', () => {
280280
281281
export function InputNestedSchema() {
282282
return v.object({
283-
field: v.string([v.minLength(1)])
283+
field: v.pipe(v.string(), v.minLength(1))
284284
})
285285
}
286286
"
@@ -439,4 +439,42 @@ describe('valibot', () => {
439439

440440
it.todo('list field');
441441
})
442+
443+
describe('pR #112', () => {
444+
it('with notAllowEmptyString', async () => {
445+
const schema = buildSchema(/* GraphQL */ `
446+
input UserCreateInput {
447+
profile: String! @constraint(maxLength: 5000)
448+
age: Int!
449+
}
450+
451+
directive @constraint(maxLength: Int!) on INPUT_FIELD_DEFINITION
452+
`);
453+
const result = await plugin(
454+
schema,
455+
[],
456+
{
457+
schema: 'valibot',
458+
notAllowEmptyString: true,
459+
directives: {
460+
constraint: {
461+
maxLength: ['maxLength', '$1', 'Please input less than $1'],
462+
},
463+
},
464+
},
465+
{},
466+
);
467+
expect(result.content).toMatchInlineSnapshot(`
468+
"
469+
470+
export function UserCreateInputSchema() {
471+
return v.object({
472+
profile: v.pipe(v.string(), v.maxLength(5000, "Please input less than 5000"), v.minLength(1)),
473+
age: v.number()
474+
})
475+
}
476+
"
477+
`)
478+
});
479+
});
442480
})

0 commit comments

Comments
 (0)